problems / 273

Dance Partners on the Pier (easy version)

easy linked-list simulation arrays 1000 ms · 256 MB

Dance Partners on the Pier (easy version)

n dancers stand in a line along the pier, numbered 1 to n from left to right. Dancer i has skill level a_i.

While at least two dancers are still in the line, the following happens:

  • Among all pairs of dancers who currently stand next to each other, pick the pair whose skill levels differ the least, that is, with the smallest |a_x − a_y|. If several pairs share the smallest difference, pick the leftmost one.
  • Those two dancers leave the line to dance together, and the dancers on either side of the gap step closer, so they become neighbours.

If n is odd, one dancer is left standing at the end.

Print the pairs in the order they leave the line.

Input

The first line contains an integer n (2 ≤ n ≤ 2000), the number of dancers.

The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9), the skill levels from left to right.

Output

Print ⌊n/2⌋ lines. The k-th line describes the k-th pair to leave: the original numbers of its two dancers, the left one first, separated by a space.

Notes

In the first example, the neighbouring differences are 4 3 2 3 2. The smallest, 2, appears twice, so the leftmost pair, dancers 3 and 4, leaves first. Dancers 2 and 5 (skills 1 and 9) are now neighbours. The line is 5 1 9 7, and the closest pair is dancers 5 and 6. Finally dancers 1 and 2 leave.

In the second example, dancers 3 and 4 leave first. Their departure makes dancers 2 and 5 neighbours, and with a difference of 1 they are the next pair. Dancer 1 is left standing.

Example 1

Input

6
5 1 4 6 9 7

Output

3 4
5 6
1 2

Example 2

Input

5
10 3 8 7 2

Output

3 4
2 5

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

Hint 1

With n ≤ 2000 there are at most 1000 rounds, and each round only needs to look at fewer than 2000 neighbouring pairs. Just do exactly what the statement says.

Hint 2

After a pair leaves, the dancers on either side become new neighbours, so the closest pair can change every round: sorting the original differences once doesn't work. Keep the original numbers of the dancers still standing, and recheck the neighbouring pairs each round, taking care that ties go to the leftmost pair.

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

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