problems / 103

Priority Printer Queue

medium queue sorting simulation 1000 ms · 256 MB

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.

Example 1

Input

5
1 5 5 3 5

Output

2 3 5 4 1

Example 2

Input

3
2 2 2

Output

1 2 3

Hints · reveal one at a time if you're stuck

Hint 1

Jobs still waiting never reorder among themselves, since a job sent to the back keeps its place relative to the others. So the only question is when each job prints. What must happen before any job of a lower priority can print?

Hint 2

Handle the priority levels from highest to lowest: every job at the current top level prints before any lower one. Within a level, the jobs print in the order the printer reaches them walking around the circle, so think about where the printer is when a level starts.

Hint 3

Store each level's positions in sorted order and binary search for the first position at or after the printer's current spot, wrapping around the circle if needed. The printer's spot afterwards is just past the last job it printed from that level.

report an issue Prefer your own editor? sooks prep 103 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.