High Tide, Low Tide
Time limit1sMemory limit512 MB
Given N distinct measurements taken alternately low, high, low, high..., with rising highs and falling lows, reconstruct the original order.
- Level
Easy3 of 10
- Topics
- Sorting, Greedy, Implementation
- Solved
- No attempts yet
Problem
Joe Coder is camping near the Bay of Fundy, between Nova Scotia and New Brunswick. When he arrived at the bay, someone told him that the difference between high tide and low tide there is the largest in the world. Joe is a skeptic, so he decided to check it himself. He picked a reference point, learned the times of high tide and low tide from the radio, then took a boat out to his reference point and measured the depth of the water. On the last day of the trip a strong wind scattered his records.
Joe recovered every measurement, but not the order they were taken in. He does remember a few things about them.
- The first measurement was the water level at low tide and the second was the water level at high tide. After that the measurements kept alternating between low tide and high tide.
- Every high tide measurement is larger than every low tide measurement.
- As time passed, the high tide levels only rose and the low tide levels only fell.
Given the measurements in scrambled order, reconstruct the order in which Joe took them.
Input
The first line contains an integer ().
The second line contains distinct positive integers separated by spaces. Each integer is at most .
Output
Print the integers on one line, separated by spaces, in the order Joe took the measurements. Exactly one order satisfies the conditions.
Hint
The measurements always start at low tide. So if is odd, there is one more low tide value than high tide value, and the last measurement is also a low tide value.