結果

問題 No.934 Explosive energy drink
ユーザー fukafukatanifukafukatani
提出日時 2019-11-29 22:56:13
言語 Rust
(1.77.0 + proconio)
結果
RE  
実行時間 -
コード長 2,756 bytes
コンパイル時間 16,129 ms
コンパイル使用メモリ 378,836 KB
実行使用メモリ 25,604 KB
平均クエリ数 1.00
最終ジャッジ日時 2024-07-16 18:44:51
合計ジャッジ時間 23,029 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `ok`
  --> src/main.rs:45:14
   |
45 |         let (ok, ng) = binary_search(-1, not_decided.len() as i32 + 1, |x: i32| {
   |              ^^ help: if this is intentional, prefix it with an underscore: `_ok`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: function `read_vec` is never used
  --> src/main.rs:89:4
   |
89 | fn read_vec<T: std::str::FromStr>() -> Vec<T> {
   |    ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn print_array<T: std::string::ToString>(array: &Vec<T>) {
    println!(
        "{}",
        array
            .iter()
            .map(|ref x| x.to_string())
            .collect::<Vec<_>>()
            .join(" ")
    );
}

fn query(a: &BTreeSet<usize>) -> i32 {
    let a = a.into_iter().map(|x| x + 1).collect::<Vec<_>>();
    print!("? ");
    debug!(a);
    print_array(&a);
    read::<i32>()
}

fn main() {
    let n = read::<usize>();
    let mut decided: BTreeSet<usize> = vec![0, 1].into_iter().collect::<BTreeSet<_>>();
    let mut not_decided = (2..n).collect::<Vec<_>>();
    loop {
        if query(&decided) == 1 {
            break;
        }
        let (ok, ng) = binary_search(-1, not_decided.len() as i32 + 1, |x: i32| {
            if x == decided.len() as i32 + 1 {
                return true;
            }
            if x == decided.len() as i32 + 1 {
                return false;
            }
            let mut query_numbers = not_decided
                .iter()
                .cloned()
                .take(x as usize + 1)
                .collect::<BTreeSet<_>>();
            for &num in decided.iter() {
                query_numbers.insert(num);
            }
            let ret = query(&query_numbers) == 0;
            ret
        });
        decided.insert(not_decided[ng as usize]);
        not_decided.remove(ng as usize);
        if not_decided.is_empty() {
            break;
        }
    }
    decided.remove(&0);
    if query(&decided) == 0 {
        decided.insert(0);
    }
    decided.remove(&1);
    if query(&decided) == 1 {
        decided.insert(1);
    }

    let decided = decided.iter().map(|&x| x + 1).collect::<Vec<_>>();
    println!("! {}", decided.len());
    print_array(&decided);
}

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 read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
fn binary_search<F>(lb: i32, ub: i32, criterion: F) -> (i32, i32)
where
    F: Fn(i32) -> bool,
{
    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