結果

問題 No.1187 皇帝ペンギン
ユーザー fukafukatani
提出日時 2020-08-30 17:01:49
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 24 ms / 1,000 ms
コード長 917 bytes
コンパイル時間 11,710 ms
コンパイル使用メモリ 401,508 KB
実行使用メモリ 25,460 KB
平均クエリ数 18.26
最終ジャッジ日時 2024-07-17 06:52:20
合計ジャッジ時間 15,961 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 54
権限があれば一括ダウンロードができます

ソースコード

diff #

fn query(x: i64) -> bool {
    println!("? {}", x);
    let s = read::<String>();
    s == "safe"
}

fn main() {
    if !query(1) {
        println!("! 0");
        return;
    }
    let (ok, _) = binary_search(1, 1000, |mid| query(mid) || query(mid + 1));
    if query(ok + 1) {
        println!("! {}", ok + 1);
    } else {
        println!("! {}", ok);
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn binary_search<F>(lb: i64, ub: i64, criterion: F) -> (i64, i64)
where
    F: Fn(i64) -> bool,
{
    // assert_eq!(criterion(lb), true);
    // assert_eq!(criterion(ub), false);
    let mut ok = lb;
    let mut ng = ub;
    while ng - ok > 1 {
        let mid = (ng + ok) / 2;
        if criterion(mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    (ok, ng)
}
0