結果
問題 | No.702 中央値を求めよ LIMITED |
ユーザー | hatoo |
提出日時 | 2018-08-05 15:03:34 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 1,216 ms / 5,000 ms |
コード長 | 9,512 bytes |
コンパイル時間 | 12,332 ms |
コンパイル使用メモリ | 386,244 KB |
実行使用メモリ | 21,504 KB |
最終ジャッジ日時 | 2024-11-22 22:35:03 |
合計ジャッジ時間 | 46,613 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,170 ms
21,248 KB |
testcase_01 | AC | 1,170 ms
21,376 KB |
testcase_02 | AC | 1,192 ms
21,376 KB |
testcase_03 | AC | 1,191 ms
21,376 KB |
testcase_04 | AC | 1,213 ms
21,340 KB |
testcase_05 | AC | 1,186 ms
21,440 KB |
testcase_06 | AC | 1,199 ms
21,376 KB |
testcase_07 | AC | 1,148 ms
21,504 KB |
testcase_08 | AC | 1,192 ms
21,376 KB |
testcase_09 | AC | 1,216 ms
21,376 KB |
testcase_10 | AC | 1,183 ms
21,376 KB |
testcase_11 | AC | 1,204 ms
21,376 KB |
testcase_12 | AC | 1,190 ms
21,360 KB |
testcase_13 | AC | 1,210 ms
21,248 KB |
testcase_14 | AC | 1,198 ms
21,360 KB |
testcase_15 | AC | 1,194 ms
21,356 KB |
testcase_16 | AC | 1,187 ms
21,376 KB |
testcase_17 | AC | 1,180 ms
21,376 KB |
testcase_18 | AC | 1,194 ms
21,376 KB |
testcase_19 | AC | 1,206 ms
21,376 KB |
testcase_20 | AC | 1,151 ms
21,300 KB |
testcase_21 | AC | 1,167 ms
21,368 KB |
testcase_22 | AC | 1,181 ms
21,428 KB |
testcase_23 | AC | 1,179 ms
21,376 KB |
testcase_24 | AC | 1,164 ms
21,376 KB |
testcase_25 | AC | 1,170 ms
21,408 KB |
testcase_26 | AC | 1,176 ms
21,316 KB |
ソースコード
#[doc = " https://github.com/hatoo/competitive-rust-snippets"] #[allow(unused_imports)] use std::cmp::{max, min, Ordering}; #[allow(unused_imports)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; #[allow(unused_imports)] use std::io::{stdin, stdout, BufWriter, Write}; #[allow(unused_imports)] use std::iter::FromIterator; mod util { use std::fmt::Debug; use std::io::{stdin, stdout, BufWriter, StdoutLock}; use std::str::FromStr; #[allow(dead_code)] pub fn line() -> String { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().to_string() } #[allow(dead_code)] pub fn chars() -> Vec<char> { line().chars().collect() } #[allow(dead_code)] pub fn gets<T: FromStr>() -> Vec<T> where <T as FromStr>::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse().unwrap()) .collect() } #[allow(dead_code)] pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) { let out = stdout(); let writer = BufWriter::new(out.lock()); f(writer) } } #[allow(unused_macros)] macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; } #[allow(unused_macros)] macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } } const BIG_STACK_SIZE: bool = false; #[allow(dead_code)] fn main() { use std::thread; if BIG_STACK_SIZE { thread::Builder::new() .stack_size(64 * 1024 * 1024) .name("solve".into()) .spawn(solve) .unwrap() .join() .unwrap(); } else { solve(); } } struct XorShift { x: u32, y: u32, z: u32, w: u32, } impl XorShift { fn new(x: u32) -> XorShift { XorShift { x, 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)); self.w } } #[doc = " IntervalHeap"] #[derive(Clone, Debug)] struct IntervalHeap<T: Ord + Eq> { data: Vec<T>, } impl<T: Ord + Eq> IntervalHeap<T> { #[allow(dead_code)] fn new() -> IntervalHeap<T> { IntervalHeap { data: Vec::new() } } #[allow(dead_code)] fn with_capacity(n: usize) -> IntervalHeap<T> { IntervalHeap { data: Vec::with_capacity(n), } } #[allow(dead_code)] #[inline] fn len(&self) -> usize { self.data.len() } #[allow(dead_code)] #[inline] fn is_empty(&self) -> bool { self.data.is_empty() } #[allow(dead_code)] #[inline] fn push(&mut self, x: T) { let i = self.data.len(); self.data.push(x); self.up(i); } #[allow(dead_code)] #[inline] fn peek_min(&self) -> Option<&T> { self.data.first() } #[allow(dead_code)] #[inline] fn peek_max(&self) -> Option<&T> { if self.data.len() > 1 { self.data.get(1) } else { self.data.first() } } #[allow(dead_code)] #[inline] fn pop_min(&mut self) -> Option<T> { if self.data.len() == 1 { return self.data.pop(); } if self.data.is_empty() { return None; } let len = self.data.len(); self.data.swap(0, len - 1); let res = self.data.pop(); self.down(0); res } #[allow(dead_code)] #[inline] fn pop_max(&mut self) -> Option<T> { if self.data.len() <= 2 { return self.data.pop(); } if self.data.is_empty() { return None; } let len = self.data.len(); self.data.swap(1, len - 1); let res = self.data.pop(); self.down(1); res } #[allow(dead_code)] #[inline] fn parent(i: usize) -> usize { ((i >> 1) - 1) & !1 } #[allow(dead_code)] #[inline] fn down(&mut self, i: usize) { let mut i = i; let n = self.data.len(); if i & 1 == 0 { while (i << 1) + 2 < n { let mut k = (i << 1) + 2; if k + 2 < n && unsafe { self.data.get_unchecked(k + 2) } < unsafe { self.data.get_unchecked(k) } { k = k + 2; } if unsafe { self.data.get_unchecked(i) } > unsafe { self.data.get_unchecked(k) } { self.data.swap(i, k); i = k; if i + 1 < self.data.len() && unsafe { self.data.get_unchecked(i) } > unsafe { self.data.get_unchecked(i + 1) } { self.data.swap(i, i + 1); } } else { break; } } } else { while (i << 1) + 1 < n { let mut k = (i << 1) + 1; if k + 2 < n && unsafe { self.data.get_unchecked(k + 2) } > unsafe { self.data.get_unchecked(k) } { k = k + 2; } if unsafe { self.data.get_unchecked(i) } < unsafe { self.data.get_unchecked(k) } { self.data.swap(i, k); i = k; if i > 0 && unsafe { self.data.get_unchecked(i) } < unsafe { self.data.get_unchecked(i - 1) } { self.data.swap(i, i - 1); } } else { break; } } } } #[allow(dead_code)] #[inline] fn up(&mut self, i: usize) { let mut i = i; if i & 1 == 1 && unsafe { self.data.get_unchecked(i) } < unsafe { self.data.get_unchecked(i - 1) } { self.data.swap(i, i - 1); i -= 1; } while i > 1 && unsafe { self.data.get_unchecked(i) } < unsafe { self.data.get_unchecked(Self::parent(i)) } { let p = Self::parent(i); self.data.swap(i, p); i = p; } while i > 1 && unsafe { self.data.get_unchecked(i) } > unsafe { self.data.get_unchecked(Self::parent(i) + 1) } { let p = Self::parent(i) + 1; self.data.swap(i, p); i = p; } } #[allow(dead_code)] #[inline] fn clear(&mut self) { self.data.clear(); } } #[derive(Clone, Debug)] struct LimitedIntervalHeap<T: Ord + Eq> { heap: IntervalHeap<T>, limit: usize, } impl<T: Ord + Eq> LimitedIntervalHeap<T> { #[allow(dead_code)] fn new(limit: usize) -> LimitedIntervalHeap<T> { LimitedIntervalHeap { heap: IntervalHeap::with_capacity(limit), limit: limit, } } #[allow(dead_code)] #[inline] fn is_empty(&self) -> bool { self.heap.is_empty() } #[allow(dead_code)] #[inline] fn push(&mut self, x: T) -> Option<T> { if self.heap.len() < self.limit { self.heap.push(x); None } else { if self.heap.data[0] < x { let mut x = x; std::mem::swap(&mut x, &mut self.heap.data[0]); if self.heap.len() >= 2 && self.heap.data[0] > self.heap.data[1] { self.heap.data.swap(0, 1); } self.heap.down(0); Some(x) } else { Some(x) } } } #[allow(dead_code)] #[inline] fn pop(&mut self) -> Option<T> { self.heap.pop_max() } #[allow(dead_code)] #[inline] fn clear(&mut self) { self.heap.clear(); } } fn solve() { let seed = get!(u32); let mut rng = XorShift::new(seed); let mut heap = LimitedIntervalHeap::new(5000001); for _ in 0..10000001 { let v = rng.next(); heap.push(v); } println!("{}", heap.heap.pop_min().unwrap()); }