Finding the Stop Key
Time limit1sMemory limit1024 MB
Given a sequence of commands that swap pairs of 8 keys (invalid commands are skipped), find the final point index of the stop key.
- Level
Easy2 of 10
- Topics
- Simulation, Bit manipulation
- Solved
- No attempts yet
Problem
A factory robot has malfunctioned and is running out of control. To stop it, you must find which of the 8 keys is the stop key. The robot has also started shuffling the keys, and it moves them too fast for the human eye to follow. Fortunately, you know the algorithm the robot uses to shuffle the keys and the starting position of the stop key.
Each key is numbered from to , and the keys sit on distinct points . The robot reads a sequence of length given in advance, one command at a time from the front, and shuffles the keys with each command. A command that encodes the integer with swaps the positions of key and key . That is, if key is at and key is at , then key moves to and key moves to .
The sequence may contain invalid commands. A command is invalid if no pair of integers satisfies . When the robot recognizes an invalid command, it ignores it and moves on to the next command.
Input
The first line contains (), the length of the command sequence.
The second line contains the commands separated by spaces ().
The third line contains (), the number of the stop key.
Output
Print (), the index of the point where the stop key is located after all commands are applied.