|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|2 초||32 MB||5||5||5||100.000%|
While lost on the long walk from the college to the UQ Centre, you have stumbled across the entrance to a secret cave system running deep under the university. The entrance is blocked by a security system consisting of N consecutive doors, each door behind the previous; and N switches, with each switch connected to a different door.
The doors are numbered 0, 1, …, (N -1) in order, with door 0 being closest to you. The switches are also numbered 0, 1, …, (N - 1) , though you do not know which switch is connected to which door.
The switches are all located at the entrance to the cave. Each switch can either be in an up or down position. Only one of these positions is correct for each switch. If a switch is in the correct position then the door it is connected to will be open, and if the switch is in the incorrect position then the door it is connected to will be closed. The correct position may be different for different switches, and you do not know which positions are the correct ones.
You would like to understand this security system. To do this, you can set the switches to any combination, and then walk into the cave to see which is the first closed door. Doors are not transparent: once you encounter the first closed door, you cannot see any of the doors behind it.
You have time to try 70,000 combinations of switches, but no more. Your task is to determine the correct position for each switch, and also which door each switch is connected to.
You should submit a file that implements the procedure
exploreCave(). This may call the grader function
tryCombination() up to 70,000 times, and must finish by calling the grader procedure
answer(). These functions and procedures are described below.
int tryCombination(int S);
-1. This function runs in O(N) time; that is, the running time is at worst proportional to N.
S: An array of length N, indicating the position of each switch. The element
S[i]corresponds to switch i . A value of
0indicates that the switch is up, and a value of
1indicates that the switch is down.
-1if all doors are open.
void answer(int S, int D);
S: An array of length N, indicating the correct position of each switch. The format matches that of the function
D: An array of length N, indicating the door each switch is connected to. Specifically, element
D[i]should contain the door number that switch
iis connected to.
void exploreCave(int N);
tryCombination()to determine the correct position for each switch and the door each switch is connected to, and must call
answer()once it has determined this information.
N: The number of switches and doors in the cave.
Suppose the doors and switches are arranged as in the picture above:
||This corresponds to the picture. Switches 0, 2 and 3 are down, while switch 1 is up. The function returns
||Doors 0, 1 and 2 are all opened, while door 3 is closed.|
||Moving switch 0 down causes all doors to be opened, indicated by the return value of
||(Program exits)||We guess that the correct combination is
For each i, switch i is connected to door i . Your task is simply to determine the correct combination.
The correct combination will always be
N ≤ 100
N ≤ 2,000
The sample grader on your computer will read input from the file cave.in, which must be in the following format:
S S … S[N - 1]
D D … D[N-1]
N is the number of doors and switches,
S[i] is the correct position for switch
D[i] is the door that switch
i is connected to.
For instance, the example above would be provided in the following format:
4 1 1 1 0 3 1 0 2
C++17, C++14, C++20, C++14 (Clang), C++17 (Clang), C++20 (Clang)