#K61097. Surging Energy Order
Surging Energy Order
Surging Energy Order
You are given n artifacts, each with an energy level. Your task is to reorder these energy levels into their surging order. The surging order is defined as follows: first, sort the energy levels in ascending order. Then, construct the surging sequence by selecting the largest energy, then the smallest, then the second largest, then the second smallest, and so on. In addition to the final energy order, you must also output the original positions (1-indexed) of these energies as they appeared in the input list.
In summary, given an array of energy levels:
- Sort the array in ascending order (keeping track of original positions).
- Build the surging order by alternating between the largest remaining and the smallest remaining energy.
- Output the surging energy levels and the corresponding original positions. </p>
The formula for the surging order can be interpreted as:
$$\text{surging}\_E = \langle E_{\max}, E_{\min}, E_{\max-1}, E_{\min+1}, \dots \rangle $$where each $E$ is taken from the sorted order of the input energy levels.
inputFormat
The input is read from standard input and has the following format:
n E1 E2 E3 ... En
Here, n
is an integer that represents the number of artifacts, and E1, E2, ..., En
represent the energy levels (space-separated integers).
outputFormat
Print the surging order of energies on the first line and the corresponding original positions on the second line. Both lines should be printed to standard output and the numbers on each line should be separated by a space.
SurgingEnergies (space-separated) SurgingPositions (space-separated)## sample
6
3 1 4 1 5 9
9 1 5 1 4 3
6 2 5 4 3 1
</p>