結果
問題 | No.1248 Kth Sum |
ユーザー | koba-e964 |
提出日時 | 2023-07-14 17:20:20 |
言語 | Rust (1.83.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,944 bytes |
コンパイル時間 | 12,746 ms |
コンパイル使用メモリ | 378,112 KB |
実行使用メモリ | 10,752 KB |
最終ジャッジ日時 | 2024-09-16 02:12:49 |
合計ジャッジ時間 | 17,024 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | AC | 22 ms
5,376 KB |
testcase_14 | WA | - |
testcase_15 | AC | 21 ms
5,376 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | AC | 1 ms
5,376 KB |
testcase_24 | WA | - |
testcase_25 | AC | 114 ms
8,960 KB |
testcase_26 | WA | - |
testcase_27 | AC | 22 ms
5,376 KB |
testcase_28 | AC | 22 ms
5,376 KB |
testcase_29 | AC | 110 ms
9,216 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
コンパイルメッセージ
warning: unused macro definition: `puts` --> src/main.rs:116:18 | 116 | macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} | ^^^^ | = note: `#[warn(unused_macros)]` on by default warning: unused import: `Write` --> src/main.rs:5:15 | 5 | use std::io::{Write, BufWriter}; | ^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused variable: `out` --> src/main.rs:115:13 | 115 | let mut out = BufWriter::new(out.lock()); | ^^^ help: if this is intentional, prefix it with an underscore: `_out` | = note: `#[warn(unused_variables)]` on by default warning: variable does not need to be mutable --> src/main.rs:115:9 | 115 | let mut out = BufWriter::new(out.lock()); | ----^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: method `remove` is never used --> src/main.rs:75:8 | 60 | impl KSmallest { | -------------- method in this implementation ... 75 | fn remove(&mut self, x: (i64, usize)) { | ^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: constant `INF` is never used --> src/main.rs:129:11 | 129 | const INF: i64 = 1 << 60; | ^^^
ソースコード
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{Write, BufWriter}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes.by_ref().map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr,) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); } impl<T: PartialOrd> Change for T { fn chmax(&mut self, x: T) { if *self < x { *self = x; } } fn chmin(&mut self, x: T) { if *self > x { *self = x; } } } #[derive(Debug)] struct KSmallest { hi: std::collections::BTreeSet<(i64, usize)>, lo: std::collections::BTreeSet<(i64, usize)>, k: usize, losum: i64, } impl KSmallest { fn new(k: usize) -> Self { KSmallest { hi: std::collections::BTreeSet::new(), lo: std::collections::BTreeSet::new(), k: k, losum: 0, } } // x.1 should be unique. fn add(&mut self, x: (i64, usize)) { self.lo.insert(x); self.losum += x.0; self.adjust(); } fn remove(&mut self, x: (i64, usize)) { if self.lo.remove(&x) { self.losum -= x.0; self.adjust(); return; } self.hi.remove(&x); } fn adjust(&mut self) { if self.lo.len() < self.k && !self.hi.is_empty() { let &mi = self.hi.iter().next().unwrap(); self.hi.remove(&mi); self.lo.insert(mi); self.losum += mi.0; } if self.lo.len() > self.k { let &ma = self.lo.iter().rev().next().unwrap(); self.lo.remove(&ma); self.losum -= ma.0; self.hi.insert(ma); } } pub fn losum(&self) -> i64 { self.losum } pub fn set_k(&mut self, k: usize) { while self.k < k { self.k += 1; self.adjust(); } while self.k > k { self.k -= 1; self.adjust(); } } } // Tags: data-structure fn main() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} #[allow(unused)] macro_rules! putvec { ($v:expr) => { for i in 0..$v.len() { puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "}); } } } input! { n: usize, k: usize, a: [i64; n], } const INF: i64 = 1 << 60; let mut ans = a[k - 1]; let mut dat = KSmallest::new(0); for i in 2..=n / k { for j in i * k - k - 1..i * k - 1 { dat.add((a[j], j)); } dat.set_k(i - 1); ans = min(ans, dat.losum() + a[i * k - 1]); } println!("{}", ans); }