Gondola sequence check
Time limit1sMemory limit256 MB
Decide whether n observed gondola numbers could appear as consecutive passings on a circle where broken gondolas are replaced in order by numbered spares.
- Level
Medium5 of 10
- Topics
- Simulation, Hash map, Linked list
- Solved
- No attempts yet
Problem
The Maokong gondola in Taipei runs on a circular track with one station. Initially gondolas through sit in order, all moving the same way. After gondola passes the station, gondola passes next (after comes ).
Gondolas can break. Spare units are numbered and the smallest unused spare replaces a broken gondola in the same slot. With , if gondola breaks it becomes gondola .
Write down the numbers of consecutive passings at the station in order to form a gondola sequence. Some breaks may already have happened before you start writing, but no gondola breaks while you write.
The same physical layout can yield different sequences depending on when you start. With no breaks, and are valid but is not.
If only gondola broke, is possible. If gondola then breaks and becomes , works. If later breaks and becomes , is also possible.
The replacement sequence lists broken gondolas in break order; here it is .
Decide whether the input sequence could appear after some replacement history.
Input
The first line contains .
The second line contains integers.
Output
Print if the sequence is possible, otherwise .
Hint
Keep the next-gondola map on the circle. For each original id missing from the sequence, replace it with the next spare id, chaining further replacements when a spare is also absent. Finally verify every consecutive pair in the input follows the successor links.