Magic Swords
Time limit2sMemory limit512 MB
Given n ages, build a forest where each node has at most two children and every child is at least k years younger than its parent, or report that none exists.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Tree, Implementation
- Solved
- No attempts yet
Problem
The archaeology department of NIICHAVO decided to study the ancient Flatland magic swords. After examining every available specimen, they found that almost all the swords are in fact copies of one another.
Specifically, the first magic sword was made in the distant past. After that, from time to time, craftsmen would take one of the existing magic swords and make a copy of it. Naturally, the copy differed from the original, but overall it inherited some of the original's traits.
Since making a copy of a magic sword reduces its magical power, the scientists established that at most two copies were made from each sword. They also established that a copy could be made no earlier than years after the original was made.
The scientists have swords, and they know the age of each one. They want to find out which sword was made first, and for every other sword, from which sword it was copied. Unfortunately, the age information may not be enough to reconstruct this uniquely, but the scientists will accept any valid option.
Input
The first line of the input contains two numbers and : the number of swords the scientists have, and the minimum age required for a copy to be made from a sword (, ). The next line contains numbers , where () is the age of the -th sword.
Output
For each sword, print the number of the sword it was copied from. Note that at most two copies could be made from each sword.
If a sword is the first one made, print 0 for that sword.
If there are several possible solutions, print any of them.
If the scientists are wrong and the required sequence of sword copies does not exist, print the single number .