結果

問題 No.1248 Kth Sum
ユーザー koba-e964koba-e964
提出日時 2023-07-14 17:26:45
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,869 bytes
コンパイル時間 1,341 ms
コンパイル使用メモリ 161,944 KB
実行使用メモリ 9,020 KB
最終ジャッジ日時 2023-10-14 07:13:26
合計ジャッジ時間 5,417 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1 ms
4,348 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 45 ms
4,348 KB
testcase_14 WA -
testcase_15 AC 44 ms
4,348 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
4,356 KB
testcase_24 WA -
testcase_25 AC 122 ms
6,384 KB
testcase_26 WA -
testcase_27 AC 44 ms
4,348 KB
testcase_28 AC 43 ms
4,348 KB
testcase_29 AC 150 ms
9,020 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 -
権限があれば一括ダウンロードができます

ソースコード

diff #

// 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 ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

#[derive(Debug)]
pub struct KSmallest {
    hi: std::collections::BTreeSet<(i64, usize)>,
    lo: std::collections::BTreeSet<(i64, usize)>,
    k: usize,
    losum: i64,
}

impl KSmallest {
    pub 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.
    pub fn add(&mut self, x: (i64, usize)) {
        self.lo.insert(x);
        self.losum += x.0;
        self.adjust();
    }
    pub 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() {
    input! {
        n: usize, k: usize,
        a: [i64; n],
    }
    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 = std::cmp::min(ans, dat.losum() + a[i * k - 1]);
        dat.hi.clear();
    }
    println!("{}", ans);
}
0