結果

問題 No.1117 数列分割
ユーザー koba-e964koba-e964
提出日時 2021-10-03 16:26:36
言語 Rust
(1.77.0)
結果
AC  
実行時間 201 ms / 3,000 ms
コード長 2,678 bytes
コンパイル時間 2,266 ms
コンパイル使用メモリ 153,624 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 15:57:52
合計ジャッジ時間 4,189 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 14 ms
4,376 KB
testcase_04 AC 10 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 12 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 14 ms
4,380 KB
testcase_11 AC 53 ms
4,376 KB
testcase_12 AC 58 ms
4,380 KB
testcase_13 AC 74 ms
4,376 KB
testcase_14 AC 81 ms
4,380 KB
testcase_15 AC 95 ms
4,380 KB
testcase_16 AC 116 ms
4,380 KB
testcase_17 AC 19 ms
4,380 KB
testcase_18 AC 142 ms
4,380 KB
testcase_19 AC 201 ms
4,376 KB
testcase_20 AC 100 ms
4,376 KB
testcase_21 AC 193 ms
4,376 KB
testcase_22 AC 191 ms
4,380 KB
testcase_23 AC 196 ms
4,376 KB
testcase_24 AC 188 ms
4,376 KB
testcase_25 AC 187 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 9 ms
4,380 KB
testcase_28 AC 10 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::*;
use std::cmp::*;
// 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"));
}

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; } }
}

const INF: i64 = 1 << 50;
fn prog(n: usize, acc: &[i64], m: usize, dp: &[i64], ep: &mut [i64]) {
    let mut st = VecDeque::new();
    st.push_back((0, dp[0]));
    for i in 1..n + 1 {
        let pre = st[0];
        ep[i] = pre.1 + acc[i];
        if pre.0 + m <= i {
            st.pop_front();
        }
        while let Some(v) = st.pop_back() {
            if v.1 <= dp[i] - acc[i] {
                continue;
            }
            st.push_back(v);
            break;
        }
        st.push_back((i, dp[i] - acc[i]));
    }
    let mut st = VecDeque::new();
    st.push_back((0, dp[0]));
    for i in 1..n + 1 {
        let pre = st[0];
        ep[i].chmax(pre.1 - acc[i]);
        if pre.0 + m <= i {
            st.pop_front();
        }
        while let Some(v) = st.pop_back() {
            if v.1 <= dp[i] + acc[i] {
                continue;
            }
            st.push_back(v);
            break;
        }
        st.push_back((i, dp[i] + acc[i]));
    }
}

// Tags: sliding-window-technique
fn main() {
    // Lagrange relaxation seems not to work.
    input! {
        n: usize, k: usize, m: usize,
        a: [i64; n],
    }
    let mut acc = vec![0; n + 1];
    for i in 0..n {
        acc[i + 1] = acc[i] + a[i];
    }
    let mut dp = vec![-INF; n + 1];
    dp[0] = 0;
    for _ in 0..k {
        let mut ep = vec![-INF; n + 1];
        prog(n, &acc, m, &dp, &mut ep);
        dp = ep;
    }
    println!("{}", dp[n]);
}
0