結果

問題 No.253 ロウソクの長さ
ユーザー codershifthcodershifth
提出日時 2016-05-05 23:17:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,836 bytes
コンパイル時間 1,106 ms
コンパイル使用メモリ 144,620 KB
実行使用メモリ 24,384 KB
平均クエリ数 32.28
最終ジャッジ日時 2023-09-23 23:50:42
合計ジャッジ時間 4,633 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
24,000 KB
testcase_01 AC 25 ms
23,796 KB
testcase_02 AC 27 ms
23,616 KB
testcase_03 AC 24 ms
24,012 KB
testcase_04 AC 25 ms
23,388 KB
testcase_05 AC 26 ms
23,616 KB
testcase_06 AC 25 ms
23,508 KB
testcase_07 AC 25 ms
23,616 KB
testcase_08 AC 24 ms
23,628 KB
testcase_09 AC 24 ms
24,024 KB
testcase_10 AC 25 ms
24,336 KB
testcase_11 AC 25 ms
23,376 KB
testcase_12 AC 25 ms
23,520 KB
testcase_13 AC 25 ms
23,952 KB
testcase_14 AC 23 ms
23,400 KB
testcase_15 AC 24 ms
23,628 KB
testcase_16 AC 25 ms
23,616 KB
testcase_17 AC 25 ms
24,312 KB
testcase_18 AC 26 ms
23,820 KB
testcase_19 AC 25 ms
23,604 KB
testcase_20 AC 26 ms
23,580 KB
testcase_21 AC 26 ms
24,204 KB
testcase_22 AC 25 ms
24,036 KB
testcase_23 AC 25 ms
24,252 KB
testcase_24 AC 26 ms
24,360 KB
testcase_25 AC 25 ms
24,252 KB
testcase_26 AC 25 ms
24,036 KB
testcase_27 AC 25 ms
24,384 KB
testcase_28 AC 24 ms
23,352 KB
testcase_29 AC 26 ms
24,360 KB
testcase_30 AC 25 ms
23,388 KB
testcase_31 AC 25 ms
24,360 KB
testcase_32 AC 25 ms
23,808 KB
testcase_33 AC 24 ms
23,628 KB
testcase_34 AC 25 ms
23,520 KB
testcase_35 AC 25 ms
23,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class LengthOfCandle
{
public:
    int ask(int Y){
	cout << "? " << Y << endl;
	int res;
	cin >> res;
	return res;
    }
    void solve(void)
    {
        int t = 0;

        // X <= 10^9 なので最大 30 回ほどクエリを投げることになる
        // X = 30 以下のときは 30 回のクエリ施行中にろうそくの長さが 0 に
        // なってしまうことがあるので最初に特別なケースをつぶす
        int x = ask(90);
        ++t;

        // 90 以下なので 0 と比較しながら答えをたしかめればよい
        if ( x == 0 )
        {
            cout<<"! "<<90<<endl;
            return;
        }
        if ( x < 0 )
        {
            for (; t < 90; ++t)
            {
                x = ask(0);
                if ( x == 0 )
                {
                    cout<<"! "<<t<<endl;
                    return;
                }
            }
        }

        // 後は二分探索
        ll u = 1E+9+1;
        ll l = 90;
        for (; t < 100; ++t)
        {
            // X = Y + t
            ll mid = (l+u)/2;
            int res = ask(mid-t);
            if ( res == 0 )
            {
                u = mid;
                break;
            }
            if ( res > 0 )
                l = mid;
            else
                u = mid;

            if (u == l)
                break;
        }
        cout<<"! "<<u<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new LengthOfCandle();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0