1-Color Coloring
Time limit1sMemory limit512 MB
Using at most 7300 Color instructions and 200 GetColor queries, make every spirit color 1 without knowing the initial arrangement.
- Level
Medium7 of 10
- Topics
- Binary search, Simulation, Implementation
- Solved
- No attempts yet
Problem
N spirits sit in a circle and play a coloring game. They are numbered 1, 2, , N, and the color of spirit is . 
Looking clockwise, each spirit faces the spirit in the next position and always colors the spirit it faces. When a spirit with color colors another spirit, that spirit takes the same color as spirit . In the figure above, when spirit 1 colors spirit 4, spirit 4 turns yellow (color 1).
Yuhyeon is with the spirits, and Jaeyul is somewhere else. Using messenger, Jaeyul talks with Yuhyeon and wants to color all spirits with color 1.
Jaeyul can tell the spirits, through Yuhyeon, to color the spirit in front of them. Each spirit then paints the spirit in front of it with its own color. However, Yuhyeon can give an instruction to at most one spirit at a time. 

From the previous state, an instruction to spirit 3 produces the left figure. Then instructions to spirits 4 and 5 produce the right figure.
Jaeyul can ask Yuhyeon whether a spirit of a certain color exists. Yuhyeon looks at how the spirits are gathered and answers either 'yes' or 'no'. Yuhyeon is not clever enough to count several colors at once, so he can count only one color. Counting takes a while, and in the meantime the spirits return to their own original colors. In the situation above, if Jaeyul asks whether a yellow spirit exists, Yuhyeon answers 'a yellow spirit exists.' After the count, spirits 1 to 5 turn yellow, purple, green, blue, and red. The colors do not change until Yuhyeon counts, so his answer cannot be wrong.
Yuhyeon could photograph the arrangement and send it to Jaeyul, but his mobile data is almost gone, so he cannot. Jaeyul must turn all spirits to color 1 without knowing the arrangement.
Write a program that helps Jaeyul turn all spirits to color 1.
Input
You must write the function void ColoringSame(int N) that colors all spirits with color 1. The function is in coloring.c or coloring.cpp.
The argument N is the number of spirits ().
Inside ColoringSame, you can call Color(A) and GetColor(C) many times.
void Color(int A) instructs spirit A to color the spirit in front of it.
A: the number of the spirit to instruct ()
int GetColor(int C) asks whether a spirit of color C exists. Calling this function resets the colors of all spirits.
C: the color number to ask about ()
Return value: 0 if no spirit has color C, 1 if one does.
When ColoringSame(N) ends, all spirits must have color 1. Otherwise the grader prints Wrong[1]. If an argument is out of range, the grader prints Wrong[2] and ends immediately. If Color or GetColor is called more than the allowed number of times, the grader prints Wrong[3] and ends immediately.
Color can be called up to 7300 times, and GetColor up to 200 times.
The grader fixes the initial arrangement before your calls and does not change it in response to your calls (non-adaptive). Using abnormal methods such as memory access or system calls can be judged wrong.
Hint
Sample code can be downloaded from the problem page. If you use an IDE such as Visual Studio, Eclipse, or Code::Blocks, put coloring.cpp, coloring.h, and grader.cpp (or coloring.c, coloring.h, and grader.c) in one project and compile it. From a terminal, use the commands below. C / gcc: gcc -Wall -lm -static -o coloring -O2 coloring.c grader.c C++ / G++: g++ -Wall -lm -static -o coloring -O2 coloring.cpp grader.cpp Submit only the code in coloring.c or coloring.cpp.