結果

問題 No.1536 仕切り直し
ユーザー 57tggx57tggx
提出日時 2021-03-23 20:46:57
言語 Rust
(1.72.1)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,559 bytes
コンパイル時間 3,905 ms
コンパイル使用メモリ 153,520 KB
実行使用メモリ 46,824 KB
最終ジャッジ日時 2023-08-16 16:55:11
合計ジャッジ時間 4,404 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,500 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 18 ms
15,672 KB
testcase_05 AC 37 ms
31,732 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 14 ms
13,292 KB
testcase_09 AC 20 ms
17,256 KB
testcase_10 AC 54 ms
46,824 KB
testcase_11 AC 9 ms
8,808 KB
testcase_12 AC 38 ms
32,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let input = Input::read();
    assert!(1 <= input.n && input.n <= 2000);
    assert!(1 <= input.m && input.m <= 2000);
    assert_eq!(input.a.len(), input.n);
    assert!(input
        .a
        .iter()
        .all(|&a| -1_000_000_000 <= a && a <= 1_000_000_000));
    let s = {
        let mut v = vec![0; input.n + 1];
        for (i, a) in input.a.iter().enumerate() {
            v[i + 1] = v[i] + a;
        }
        v
    };
    let mut x = vec![vec![0; input.n + 1]; input.m];
    let mut y = vec![vec![0; input.n + 1]; input.m];
    let mut sign = 1;
    for i in 0..input.m {
        for j in 0..=input.n {
            x[i][j] = if i == 0 { 0 } else { y[i - 1][j] } + sign * s[j];
        }
        for j in 1..=input.n {
            y[i][j] = y[i][j - 1].max(x[i][j]);
        }
        sign = -sign;
    }
    let mut index = input.n;
    for i in (0..input.m).rev() {
        for j in (0..=index).rev() {
            if x[i][j] == y[i][index] {
                println!("{}", j);
                index = j;
                break;
            }
        }
    }
}

struct Input {
    n: usize,
    m: usize,
    a: Vec<i64>,
}

impl Input {
    fn read() -> Input {
        let stdin = std::io::stdin();
        let (n, m) = {
            let mut s = String::new();
            stdin.read_line(&mut s).unwrap();
            if let [n, m] = *my_split(&s).unwrap() {
                (n.parse().unwrap(), m.parse().unwrap())
            } else {
                panic!();
            }
        };
        let a = {
            let mut s = String::new();
            stdin.read_line(&mut s).unwrap();
            let a = my_split(&s).unwrap();
            assert_eq!(a.len(), n);
            a.iter().map(|x| x.parse().unwrap()).collect()
        };
        assert_eq!(stdin.read_line(&mut String::new()).unwrap(), 0);
        Input { n: n, m: m, a: a }
    }
}

fn my_split(s: &str) -> Option<Vec<&str>> {
    enum State {
        Word(usize),
        Space,
        End,
    }
    let mut state = State::Word(0);
    let mut ret = Vec::new();
    for (i, c) in s.char_indices() {
        let prev = match state {
            State::End => return None,
            State::Word(i) => i,
            State::Space => {
                state = State::Word(i);
                i
            }
        };
        if c == ' ' || c == '\n' {
            ret.push(&s[prev..i]);
            state = if c == ' ' { State::Space } else { State::End };
        }
    }
    match state {
        State::End => Some(ret),
        _ => None,
    }
}
0