問題一覧 > 通常問題

No.253 Candle Length

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 256 MB / リアクティブ問題 (詳しくはこちら)
タグ : (AC するまで非表示) / 解いたユーザー数 140
作問者 : kroton
お気に入りにしたユーザー ProblemId : 687 / 自分の提出
問題文最終更新日: 2026-06-25 04:24:03
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder 1周年記念 復習コンテスト R1 (順位表) の他の問題:

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.

  1. Yukiko lights the candle.
  2. For some non-negative integer $Y$, you can ask Yukiko: "Is the current length of the candle $Y$ cm?"
  3. Yukiko tells you whether the current length of the candle is "shorter" than, "longer" than, or "equal" to $Y$ cm.
  4. 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.
  5. 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もしくは右上の雲マークをクリックしてアカウントを作成してください。