結果

問題 No.1117 数列分割
ユーザー koba-e964koba-e964
提出日時 2021-10-03 15:31:57
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,208 bytes
コンパイル時間 3,654 ms
コンパイル使用メモリ 155,136 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-28 15:01:28
合計ジャッジ時間 10,136 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 6 ms
4,376 KB
testcase_04 AC 35 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 96 ms
4,380 KB
testcase_09 AC 20 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 319 ms
4,380 KB
testcase_12 AC 87 ms
4,376 KB
testcase_13 AC 227 ms
4,376 KB
testcase_14 AC 360 ms
4,376 KB
testcase_15 AC 48 ms
4,380 KB
testcase_16 AC 195 ms
4,376 KB
testcase_17 WA -
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 8 ms
4,380 KB
testcase_20 WA -
testcase_21 AC 577 ms
4,380 KB
testcase_22 AC 575 ms
4,380 KB
testcase_23 AC 670 ms
4,376 KB
testcase_24 AC 575 ms
4,376 KB
testcase_25 AC 574 ms
4,376 KB
testcase_26 WA -
testcase_27 AC 21 ms
4,376 KB
testcase_28 AC 23 ms
4,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
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]
}

fn main() {
    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 fail = 3i64 << 40;
    let mut pass = 0;
    while fail - pass > 1 {
        let mid = (fail + pass) / 2;
        let val = calc(n, &acc, m, mid);
        if val.1 >= k {
            pass = mid;
        } else {
            fail = mid;
        }
    }
    eprintln!("pass = {:?}", pass);
    let val = calc(n, &acc, m, pass);
    eprintln!("val = {:?}", val);
    println!("{}", val.0 + pass * k as i64);
}
0