Hiker Safety
Time limit4sMemory limit512 MB
Hikers on markers along a route must take turns stepping forward so that neighbours stay within distance B and everyone keeps their personal space; output the lexicographically smallest order in which all reach the end, or impossible.
- Level
Hard8 of 10
- Topics
- Greedy, Simulation, Implementation
- Solved
- No attempts yet
Problem
A hiking club runs a time trial along a single route. The route has marker points, marker sits metres from the start, and . The slow part of this sport is the pathfinding, so once a hiker knows where to go next, the walk itself takes no time at all.
hikers are on the route. Hiker stands on marker and needs metres of personal space. Two rules hold at every moment.
- Two hikers that are neighbours on the route, meaning no other hiker stands between them, are at most metres apart.
- Hiker keeps everyone else at least metres away, so the distance between hiker and hiker is at least .
The hikers move one at a time. A single move takes one hiker from the marker they stand on to the next marker and finishes instantly, so nobody is ever between two markers. Both rules hold again after every move.
A hiker who reaches marker has finished the route and leaves it at once. From that moment the two rules ignore that hiker, so a move onto marker is always allowed. A hiker who stands on marker at the start has already finished and never moves.
Work out the order in which the hikers move so that all of them finish the route.
Input
- One line with the integer (), the largest distance allowed between two neighbouring hikers.
- One line with the integer (), the number of marker points.
- One line with integers (), the distance of each marker from the start.
- One line with the integer (), the number of hikers.
- lines, the -th of which holds two integers and (, ), the personal space and the current marker of hiker . The hikers are listed from the start of the route onwards, so .
The starting arrangement obeys both rules. A hiker who starts on marker has already finished and is left out of that check.
Output
If the hikers cannot all reach marker without breaking a rule, print impossible.
Otherwise print the hiker numbers in the order they move, separated by single spaces, on one line. Every correct answer has the same length , so print the lexicographically smallest one. Compare two answers one position at a time; the answer with the smaller number at the first position where they differ comes first.