Priority Printer Queue
The office printer serves a circular job queue holding n print jobs, numbered 1 to n in the order they currently sit in the queue (job 1 is at the front). Each job has an integer priority.
The printer repeats the following step until the queue is empty:
- Look at the job at the front of the queue.
- If every other job still waiting has priority less than or equal to the front job's priority (no waiting job is strictly more urgent), the printer prints the front job and removes it from the queue.
- Otherwise, the front job is sent to the back of the queue, unprinted, and the printer looks at the new front job.
Given the initial queue, determine the order in which the n jobs get printed.
Direct simulation with an actual queue can take Θ(n) work per printed job on adversarial inputs (for example one very low-priority job stuck behind many higher-priority ones), so it is too slow for the largest inputs; find an O(n log n) way to compute the order.
Input
The first line contains one integer n (1 ≤ n ≤ 2·10^5), the number of jobs.
The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 10^9), where p_i is the priority of the job that starts at position i in the queue (job i).
Output
Print n integers separated by single spaces: the job numbers in the order the printer prints them.
Notes
In the first example the jobs have priorities 1 5 5 3 5. Job 1 (priority 1) is sent to the back immediately, since other jobs have priority 5. Job 2 (priority 5) then has no strictly more urgent job waiting (the remaining priorities are 5, 3, 5, 1), so it prints. The printer continues around the queue the same way, printing job 3, then job 5, then job 4, and finally job 1.
In the second example all three jobs share the same priority, so no job is ever strictly less urgent than another and they print in their original order.