Sorting Game
Time limit1sMemory limit128 MB
Apply K sets of operations, each sorting a prefix of length A ascending then a prefix of length B descending, and print the final sequence.
- Level
Hard8 of 10
- Topics
- Sorting, Implementation, Simulation, Greedy
- Solved
- No attempts yet
Problem
In this game you repeatedly sort a prefix of a sequence in ascending order, then sort a prefix again in descending order. One ascending sort followed by one descending sort is one set.
For example, take the sequence [4, 1, 2, 3] and a set that sorts elements 1 through 3 in ascending order and then elements 1 through 2 in descending order. The ascending sort gives [1, 2, 4, 3], and the descending sort that follows gives [2, 1, 4, 3]. That is the sequence after the set.
You are given a sequence and sets. Write a program that prints the sequence after all sets have been carried out in the given order.
Input
The first line contains the length of the sequence, . ()
The second line contains the elements of the sequence, separated by spaces. Each element is an integer between and , inclusive.
The third line contains the number of sets, . ()
Each of the next lines contains two integers and describing one set, separated by a space. (, ) The set sorts elements 1 through in ascending order, then sorts elements 1 through in descending order. Carry out the sets in the order given in the input.
Output
Print the elements of the sequence after all sets, separated by spaces, on one line.