結果

問題 No.1187 皇帝ペンギン
ユーザー Mister
提出日時 2020-08-22 13:55:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 36 ms / 1,000 ms
コード長 808 bytes
コンパイル時間 1,004 ms
コンパイル使用メモリ 72,032 KB
最終ジャッジ日時 2025-01-13 08:13:44
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 54
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

constexpr int X = 1000;

bool query(int n) {
    if (n > X) return false;
    static std::vector<int> memo(X + 1, -1);
    if (memo[n] != -1) return memo[n];

    std::cout << "? " << n << std::endl;

    std::string res;
    std::cin >> res;
    return memo[n] = (res == "safe" ? 1 : 0);
}

void answer(int n) {
    std::cout << "! " << n << std::endl;
}

void solve() {
    int ok = 0, ng = X;
    while (ng - ok > 1) {
        int mid = (ok + ng) / 2;

        if (query(mid)) {
            ok = mid;
        } else if (query(mid + 1)) {
            ok = mid + 1;
        } else {
            ng = mid;
        }
    }

    answer(ok);
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0