Question: PLEASE USE THE MOST PERFORMANCE OPTIMIZED VERSION OF YOUR CODE, OTHERWISE THERE WILL BE TIMEOUT ERRORS. FEEL FREE TO ALSO ANSWER IN PYTHON 3 You

 PLEASE USE THE MOST PERFORMANCE OPTIMIZED VERSION OF YOUR CODE, OTHERWISE

THERE WILL BE TIMEOUT ERRORS. FEEL FREE TO ALSO ANSWER IN PYTHON

3 You are given a Chain of n virtual blocks, in ascending

order according to their values bi. The first half of the chain

PLEASE USE THE MOST PERFORMANCE OPTIMIZED VERSION OF YOUR CODE, OTHERWISE THERE WILL BE TIMEOUT ERRORS.

FEEL FREE TO ALSO ANSWER IN PYTHON 3

You are given a Chain of n virtual blocks, in ascending order according to their values bi. The first half of the chain is refered to as the Tail, which contains the least valuable blocks, while the remaining section is the Head, which contains the most valuable blocks. If the number of blocks is odd, then the odd block is considered to be a part of the Head. The chain is guaranteed to always have at least 2 blocks. Random commands will be given; which can be onr of the following 4 types: 1. - h x remove the least valuable block(s) with a total value no greater than r from the Head of the chain, 2. - tx remove the most valuable block(s) with a total value no greater than from the Tail of the chain 3. x place a block of value into the correct position in the chain, 4 p print the current most valuable block at the tail and the least valuable block at the head. (Only the least valuable block(s) can be removed from the head, while only the most valuable blockfs) can be removed from the tail at any time.) Print the respective values for each p command Input Format The first line contains the integer: n. The second line contains n space separated integers denoting: bi, b2, , bn The third line contains the number of commands: c Each of the next c lines contains either of the commands: p, x,-h x, or t x. Constraints 2 n s 10 2 c105 Output Format On a new line, for each p command, print two space separated integers indicating t h -the current most valuable block in the tail and the least valuable block in the head repectively The next command is to add a block of value 5 to the chain. 5 is less than the 7 at the tail so it is placed before the 7. The chain is then balanced which makes 5 the new tall and 7 8 the head bie block at the tal (which s 5)and the least valuable block at the head (which is 7). So 5 7 is printed. The next command is to add a block of value 6 to the chain. 6 is less than the 7 at the and greater than 5, so it is placed between those to blocks. The chain is then balanced which makes 5 6 the new tail and 7 8 the head: The last command is to print the most valuable block at the tail (which is 6) and the least valuable block at the head (which is 7). So 6 7 is printed. 1 import java.io. 2 import java.math.*; 3 import java.security.*; 4 import java.text.; 5 import java.util.; 6 import java.util.concurrent.*; 7 import java.util.function. 8 import java.util.regex.; 9 import java.util.stream. 10 import static java.util.stream.Collectors.joining; 11 import static java.util.stream.Collectors.toList; 13 public class Solution t public static void main(String[] args) throws IOException t 19 20 21 BufferedReader bufferedReader new BufferedReader (new InputStreamReader (System.in)); int n Integer.parseInt (bufferedReader.readLine).trim)); ListInteger blocks = Stream . of (bufferedReader. read Line(). replaceALL("\\s+S", ""). split(" ")) map (Integer::parseInt) collect (tolist)); 25 26 int qInteger.parseInt (bufferedReader.readLine).trim)) 28 IntStream.range (0, q).forEach(qItr -> t try t 30 31 32 String command = bufferedReader.read Line(); catch (IOException ex) throw new RuntimeException (ex); 34 35 bufferedReader.close; 37 You are given a Chain of n virtual blocks, in ascending order according to their values bi. The first half of the chain is refered to as the Tail, which contains the least valuable blocks, while the remaining section is the Head, which contains the most valuable blocks. If the number of blocks is odd, then the odd block is considered to be a part of the Head. The chain is guaranteed to always have at least 2 blocks. Random commands will be given; which can be onr of the following 4 types: 1. - h x remove the least valuable block(s) with a total value no greater than r from the Head of the chain, 2. - tx remove the most valuable block(s) with a total value no greater than from the Tail of the chain 3. x place a block of value into the correct position in the chain, 4 p print the current most valuable block at the tail and the least valuable block at the head. (Only the least valuable block(s) can be removed from the head, while only the most valuable blockfs) can be removed from the tail at any time.) Print the respective values for each p command Input Format The first line contains the integer: n. The second line contains n space separated integers denoting: bi, b2, , bn The third line contains the number of commands: c Each of the next c lines contains either of the commands: p, x,-h x, or t x. Constraints 2 n s 10 2 c105 Output Format On a new line, for each p command, print two space separated integers indicating t h -the current most valuable block in the tail and the least valuable block in the head repectively The next command is to add a block of value 5 to the chain. 5 is less than the 7 at the tail so it is placed before the 7. The chain is then balanced which makes 5 the new tall and 7 8 the head bie block at the tal (which s 5)and the least valuable block at the head (which is 7). So 5 7 is printed. The next command is to add a block of value 6 to the chain. 6 is less than the 7 at the and greater than 5, so it is placed between those to blocks. The chain is then balanced which makes 5 6 the new tail and 7 8 the head: The last command is to print the most valuable block at the tail (which is 6) and the least valuable block at the head (which is 7). So 6 7 is printed. 1 import java.io. 2 import java.math.*; 3 import java.security.*; 4 import java.text.; 5 import java.util.; 6 import java.util.concurrent.*; 7 import java.util.function. 8 import java.util.regex.; 9 import java.util.stream. 10 import static java.util.stream.Collectors.joining; 11 import static java.util.stream.Collectors.toList; 13 public class Solution t public static void main(String[] args) throws IOException t 19 20 21 BufferedReader bufferedReader new BufferedReader (new InputStreamReader (System.in)); int n Integer.parseInt (bufferedReader.readLine).trim)); ListInteger blocks = Stream . of (bufferedReader. read Line(). replaceALL("\\s+S", ""). split(" ")) map (Integer::parseInt) collect (tolist)); 25 26 int qInteger.parseInt (bufferedReader.readLine).trim)) 28 IntStream.range (0, q).forEach(qItr -> t try t 30 31 32 String command = bufferedReader.read Line(); catch (IOException ex) throw new RuntimeException (ex); 34 35 bufferedReader.close; 37

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!