No.253 Candle Length
Problem Statement
Yukiko and you play a game using a candle.
The candle is initially $X$ cm long. $X$ is some integer between $10$ and $10^{9}$, inclusive.
During the game, Yukiko can see the candle, but you cannot see it and you do not know its initial length either.
The game proceeds as follows.
- Yukiko lights the candle.
- For some non-negative integer $Y$, you can ask Yukiko: "Is the current length of the candle $Y$ cm?"
- Yukiko tells you whether the current length of the candle is "shorter" than, "longer" than, or "equal" to $Y$ cm.
- The candle becomes 1 cm shorter. However, if it has already burned out, that is, if it is 0 cm, it stays at 0 cm.
- If you still want to ask a question, return to step 2. Otherwise, finish.
Determine the initial length $X$ of the candle using at most 100 questions.
Input and Output
The program you submit (hereafter, the answering program) must print two kinds of queries to standard output.
The queries are a question query, which asks whether the current length of the candle is some non-negative integer $Y$, and an answer query, which answers that the initial length $X$ of the candle is some non-negative integer $Y$.
The format of a question query is
"$?$ $Y$",
and the format of an answer query is
"$!$ $Y$".
In both question queries and answer queries, if $Y$ is not an integer, $Y$ is negative, or $Y$ is out of the range of a 32-bit signed integer, the verdict will be Wrong Answer.
Print a newline at the end of each query.
Flush the output after printing. If you do not flush, the responding program may be unable to process the input and exceed the time limit, which may result in Wrong Answer.
When you make a question query, the result is returned from the responding program to the standard input of the answering program.
If the current length of the candle is shorter than $Y$, -1 is given as input; if it is longer, 1; if it is equal, 0.
Also, be sure to receive the result after executing a question query. Otherwise, the responding program may be unable to process the input, which may cause Wrong Answer.
After executing an answer query, terminate the program immediately. Otherwise, the judge's time limit may be exceeded, which may result in Wrong Answer.
Your submission is considered correct if it terminates within the judge's time limit, the number of questions is at most 100, and the value $Y$ printed by the answer query matches $X$.
Notes
- The judge's time limit is 2 seconds, and this includes the response time of the responding program.
- Printing anything other than the two kinds of queries results in Wrong Answer.
- Output to standard error is regarded as neither a question query, an answer query, nor invalid output.
Example Program
int ask(int Y){
cout << "? " << Y << endl;
int res;
cin >> res;
return res;
}
int main(){
int res = 123;
for(int i=0;i<100;i++){
int Y = rand() % 10000;
if(ask(Y) == 0){
res = 456;
break;
}
}
cout << "! " << res << endl;
return 0;
}提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。