結果

問題 No.1536 仕切り直し
ユーザー 57tggx57tggx
提出日時 2021-03-23 20:46:57
言語 Rust
(1.77.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,559 bytes
コンパイル時間 14,394 ms
コンパイル使用メモリ 378,196 KB
実行使用メモリ 46,720 KB
最終ジャッジ日時 2024-05-04 00:45:09
合計ジャッジ時間 13,511 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 3 ms
5,248 KB
testcase_04 AC 15 ms
15,744 KB
testcase_05 AC 35 ms
31,872 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 13 ms
13,056 KB
testcase_09 AC 16 ms
17,408 KB
testcase_10 AC 48 ms
46,720 KB
testcase_11 AC 9 ms
8,832 KB
testcase_12 AC 32 ms
32,768 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