結果

問題 No.702 中央値を求めよ LIMITED
ユーザー frozenlibfrozenlib
提出日時 2018-06-16 00:39:01
言語 Rust
(1.72.1)
結果
AC  
実行時間 520 ms / 5,000 ms
コード長 1,936 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 147,672 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-14 21:20:25
合計ジャッジ時間 17,747 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 517 ms
4,380 KB
testcase_01 AC 516 ms
4,380 KB
testcase_02 AC 517 ms
4,384 KB
testcase_03 AC 516 ms
4,384 KB
testcase_04 AC 516 ms
4,380 KB
testcase_05 AC 516 ms
4,380 KB
testcase_06 AC 516 ms
4,380 KB
testcase_07 AC 517 ms
4,380 KB
testcase_08 AC 519 ms
4,380 KB
testcase_09 AC 518 ms
4,384 KB
testcase_10 AC 516 ms
4,380 KB
testcase_11 AC 518 ms
4,380 KB
testcase_12 AC 517 ms
4,384 KB
testcase_13 AC 518 ms
4,384 KB
testcase_14 AC 519 ms
4,380 KB
testcase_15 AC 520 ms
4,384 KB
testcase_16 AC 519 ms
4,384 KB
testcase_17 AC 518 ms
4,380 KB
testcase_18 AC 518 ms
4,380 KB
testcase_19 AC 515 ms
4,384 KB
testcase_20 AC 518 ms
4,384 KB
testcase_21 AC 518 ms
4,380 KB
testcase_22 AC 516 ms
4,380 KB
testcase_23 AC 515 ms
4,380 KB
testcase_24 AC 517 ms
4,384 KB
testcase_25 AC 515 ms
4,504 KB
testcase_26 AC 518 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = CpReader::new(i);
    let seed = i.read::<u32>();
    writeln!(o, "{}", solve(seed)).unwrap();
}
struct Gen {
    x: u32,
    y: u32,
    z: u32,
    w: u32,
}
impl Gen {
    fn new(seed: u32) -> Self {
        Gen {
            x: seed,
            y: 1,
            z: 2,
            w: 3,
        }
    }
    fn next(&mut self) -> u32 {
        let t = self.x ^ (self.x << 11);
        self.x = self.y;
        self.y = self.z;
        self.z = self.w;
        self.w = (self.w ^ (self.w >> 19)) ^ (t ^ (t >> 8));
        return self.w;
    }
}

fn solve(seed: u32) -> u32 {
    fn is_over_mid(seed: u32, mid: u32) -> bool {
        let mut c = 0;
        let mut g = Gen::new(seed);
        for _ in 0..10000001 {
            let n = g.next();
            if n <= mid {
                c += 1;
            }
        }
        c >= 5000001
    }

    let mut low = 0;
    let mut high = u32::max_value();

    while low < high {
        let mid = ((low as u64 + high as u64) / 2) as u32;
        if is_over_mid(seed, mid) {
            high = mid;
        } else {
            low = mid + 1;
        }
    }
    low
}

mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        s: String,
    }

    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
    }
}
0