結果

問題 No.702 中央値を求めよ LIMITED
ユーザー frozenlibfrozenlib
提出日時 2018-06-15 23:56:00
言語 Rust
(1.77.0)
結果
MLE  
実行時間 -
コード長 1,988 bytes
コンパイル時間 4,838 ms
コンパイル使用メモリ 148,628 KB
実行使用メモリ 43,008 KB
最終ジャッジ日時 2023-09-13 05:29:51
合計ジャッジ時間 7,859 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 {
    let mut ns = Vec::new();
    let mut g = Gen::new(seed);
    for _ in 0..10000001 + 1 {
        ns.push(g.next());
    }

    fn is_over_mid(ns: &[u32], mid: u32) -> bool {
        let mut c = 0;
        for &n in ns {
            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(&ns, 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