結果

問題 No.702 中央値を求めよ LIMITED
ユーザー koba-e964koba-e964
提出日時 2022-01-04 17:09:19
言語 Rust
(1.77.0)
結果
AC  
実行時間 411 ms / 5,000 ms
コード長 1,892 bytes
コンパイル時間 733 ms
コンパイル使用メモリ 150,656 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 12:57:43
合計ジャッジ時間 13,011 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 411 ms
5,248 KB
testcase_01 AC 406 ms
5,376 KB
testcase_02 AC 409 ms
5,376 KB
testcase_03 AC 395 ms
5,376 KB
testcase_04 AC 390 ms
5,376 KB
testcase_05 AC 406 ms
5,376 KB
testcase_06 AC 404 ms
5,376 KB
testcase_07 AC 396 ms
5,376 KB
testcase_08 AC 401 ms
5,376 KB
testcase_09 AC 404 ms
5,376 KB
testcase_10 AC 402 ms
5,376 KB
testcase_11 AC 395 ms
5,376 KB
testcase_12 AC 401 ms
5,376 KB
testcase_13 AC 399 ms
5,376 KB
testcase_14 AC 411 ms
5,376 KB
testcase_15 AC 395 ms
5,376 KB
testcase_16 AC 402 ms
5,376 KB
testcase_17 AC 399 ms
5,376 KB
testcase_18 AC 393 ms
5,376 KB
testcase_19 AC 399 ms
5,376 KB
testcase_20 AC 404 ms
5,376 KB
testcase_21 AC 400 ms
5,376 KB
testcase_22 AC 405 ms
5,376 KB
testcase_23 AC 396 ms
5,376 KB
testcase_24 AC 394 ms
5,376 KB
testcase_25 AC 396 ms
5,376 KB
testcase_26 AC 393 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;

#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

struct XorShift {
    x: u32,
    y: u32,
    z: u32,
    w: u32,
}

impl XorShift {
    fn new(x: u32) -> Self {
        XorShift {
            x: x,
            y: 1,
            z: 2,
            w: 3,
        }
    }
    fn generate(&mut self) -> u32 {
        let t = self.x ^ (self.x << 11);
        self.x = self.y;
        self.y = self.z;
        self.z = self.w;
        let w = self.w;
        self.w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); 
        self.w
    }
}

fn main() {
    let seed = get();
    let mut pass = 3u32 << 30;
    let mut fail = 1u32 << 30;
    let n = 10_000_001;
    while pass - fail > 1 {
        let mid = fail + (pass - fail) / 2;
        let mut rng = XorShift::new(seed);
        let mut le = 0;
        for _ in 0..n {
            let a = rng.generate();
            if a <= mid {
                le += 1;
            }
        }
        if le >= (n + 1) / 2 {
            pass = mid;
        } else {
            fail = mid;
        }
    }
    println!("{}", pass);
}
0