結果

問題 No.1117 数列分割
ユーザー koba-e964koba-e964
提出日時 2021-10-03 16:25:26
言語 Rust
(1.77.0)
結果
AC  
実行時間 200 ms / 3,000 ms
コード長 3,093 bytes
コンパイル時間 2,890 ms
コンパイル使用メモリ 161,464 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 15:56:46
合計ジャッジ時間 6,025 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 14 ms
4,380 KB
testcase_04 AC 11 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 12 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 14 ms
4,376 KB
testcase_11 AC 52 ms
4,380 KB
testcase_12 AC 58 ms
4,376 KB
testcase_13 AC 73 ms
4,380 KB
testcase_14 AC 81 ms
4,380 KB
testcase_15 AC 94 ms
4,380 KB
testcase_16 AC 114 ms
4,376 KB
testcase_17 AC 17 ms
4,376 KB
testcase_18 AC 142 ms
4,376 KB
testcase_19 AC 200 ms
4,380 KB
testcase_20 AC 99 ms
4,376 KB
testcase_21 AC 191 ms
4,380 KB
testcase_22 AC 190 ms
4,376 KB
testcase_23 AC 195 ms
4,380 KB
testcase_24 AC 187 ms
4,380 KB
testcase_25 AC 186 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 9 ms
4,380 KB
testcase_28 AC 10 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: function `calc` is never used
  --> Main.rs:40:4
   |
40 | fn calc(n: usize, acc: &[i64], m: usize, x: i64) -> (i64, usize) {
   |    ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

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

fn calc(n: usize, acc: &[i64], m: usize, x: i64) -> (i64, usize) {
    let mut dp = vec![(0, 0); n + 1];
    for i in 1..n + 1 {
        let mut me = (dp[i - 1].0 + acc[i] - acc[i - 1] - x, dp[i - 1].1 + 1);
        for j in 1..min(i, m) + 1 {
            me.chmax((dp[i - j].0 + (acc[i] - acc[i - j]).abs() - x, dp[i - j].1 + 1));
        }
        dp[i] = me;
    }
    dp[n]
}

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;
        // eprintln!("{:?}", dp);
    }
    println!("{}", dp[n]);
}
0