A mechanical doll is a doll which automatically repeats a specific sequence of motions. In Japan, many mechanical dolls have been created since ancient times.
The motions of a mechanical doll are controlled by a circuit that consists of devices. The devices are connected with tubes. Each device has one or two exits, and can have arbitrarily many (possibly zero) entrances. Each tube connects an exit of a device to an entrance of the same or another device. Exactly one tube is connected to each entrance, and exactly one tube is connected to each exit.
To describe how the doll makes motions, consider a ball that is placed on one of the devices. The ball travels through the circuit. At each step of the travel, the ball leaves the device using one of its exits, travels along the tube connected to the exit and enters the device at the other end of the tube.
There are three types of devices: origin, trigger, and switch. There is exactly one origin, triggers, and switches ( can be zero). You must decide the value of . Each device has a unique serial number.
The origin is the device where the ball is initially placed. It has one exit. Its serial number is .
A trigger causes the doll to make a specific motion whenever the ball enters it. Every trigger has one exit. The serial numbers of the triggers are from through .
Each switch has two exits, which are called 'X' and 'Y'. The state of a switch is either 'X' or 'Y'. After the ball enters a switch, it leaves the switch using the exit given by the current state of the switch. After that, the switch changes its state to the opposite one. Initially, the state of every switch is 'X'. The serial numbers of the switches are from through .
You are given the number of triggers . You are also given a sequence of length , each of whose elements is the serial number of a trigger. Each trigger might appear some (possibly zero) times in the sequence . Your task is to create a circuit that satisfies the following conditions:
- The ball returns to the origin after some steps.
- When the ball first returns to the origin, the state of every switch is 'X'.
- The ball first returns to the origin after entering triggers exactly times. The serial numbers of the triggers, in the order that they are entered, are .
- Let be the total number of state changes of all switches caused by the ball before the ball first returns to the origin. The value of doesn't exceed .
At the same time, you don't want to use too many switches.
Implementation Details
You should implement the following procedure:
void create_circuit(int M, std::vector<int> A)
- : the number of triggers.
- : an array of length , giving the serial numbers of the triggers the ball needs to enter, in the order they are to be entered.
- This procedure is called exactly once.
- Note that the value of can be obtained by looking at the length of .
Your program should call the following procedure to answer:
void answer(std::vector<int> C, std::vector<int> X, std::vector<int> Y)
- : an array of length . The exit of device is connected to device .
- , : arrays of the same length. The length of these arrays is the number of the switches. For the switch , its exit 'X' is connected to the device and its exit 'Y' is connected to the device .
- Every element of , , and must be an integer between and , inclusive.
- must be at most .
- This procedure must be called exactly once.
- The circuit represented by , , and must satisfy the conditions in the problem statement.
If some of the above conditions are not satisfied, your program is judged as Wrong Answer. Otherwise, your program is judged as Accepted and your score is calculated by (see Subtasks).
Example
Let , , and .
The grader calls create_circuit(4, [1, 2, 1, 3])
.
The above figure shows a circuit, which is described by a call answer([1, -1, -2, 0, 2], [2, -2], [3, 1])
.
The numbers in the figure are the serial numbers of the devices.
Two switches are used. Thus .
Initially, the states of the switches and are both 'X'.
The ball travels as follows:
- When the ball first enters the switch , its state is 'X'. Hence, the ball travels to the trigger . Then the state of the switch is changed to 'Y'.
- When the ball enters the switch for the second time, its state is 'Y'. Hence, the ball travels to the trigger . Then the state of the switch is changed to 'X'.
The ball first returns to the origin, having entered the triggers . The states of the switches and are both 'X'. The value of is . Therefore, this circuit satisfies the conditions.
The file sample-01-in.txt
in the zipped attachment package corresponds to this example.
Other sample inputs are also available in the package.
Constraints
Subtasks
The score and the constraints for each subtask are as follows:
- (2 points) For each , the integer appears at most once in the sequence .
- (4 points) For each , the integer appears at most twice in the sequence .
- (10 points) For each , the integer appears at most times in the sequence .
- (10 points)
- (18 points)
- (56 points) No additional constraints.
For each test case, if your program is judged as Accepted, your score is calculated according to the value of :
- If , you gain the full score for the test case.
- For each test case in Subtasks 5 and 6, if , you gain a partial score. The score for the test case is , multiplied by the score assigned to the subtask.
- Otherwise, the score is .
Note that your score for each subtask is the minimum of the scores for the test cases in the subtask.
Sample Grader
The sample grader reads the input from the standard input in the following format:
- line :
- line :
The sample grader produces three outputs.
First, the sample grader outputs your answer to a file named out.txt
in the following format:
- line :
- line :
- line :
Second, the sample grader simulates the moves of the ball.
It outputs the serial numbers of the devices the ball entered, in order, to a file named log.txt
.
Third, the sample grader prints the evaluation of your answer to the standard output.
- If your program is judged as Accepted, the sample grader prints and in the format
Accepted: S P
. - If your program is judged as Wrong Answer, it prints
Wrong Answer: MSG
. The meaning ofMSG
is as follows:answered not exactly once
: The procedureanswer
is called not exactly once.wrong array length
: The length of is not , or the lengths of and are different.over 400000 switches
: is larger than .wrong serial number
: There is an element of , , or which is smaller than or larger than .over 20000000 inversions
: The ball doesn't return to the origin within state changes of the switches.state 'Y'
: There is a switch whose state is 'Y' when the ball first returns to the origin.wrong motion
: The triggers which cause motions are different from the sequence .
Note that the sample grader might not create out.txt
and/or log.txt
when your program is judged as Wrong Answer.
Attachment Package
The sample grader along with sample cases is available here.
Comments