結果

問題 No.952 危険な火薬庫
ユーザー akakimidoriakakimidori
提出日時 2021-01-09 02:02:40
言語 Rust
(1.77.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,985 bytes
コンパイル時間 11,390 ms
コンパイル使用メモリ 378,476 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 08:00:20
合計ジャッジ時間 13,200 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 0 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 52 ms
5,376 KB
testcase_04 AC 47 ms
5,376 KB
testcase_05 AC 38 ms
5,376 KB
testcase_06 AC 56 ms
5,376 KB
testcase_07 AC 18 ms
5,376 KB
testcase_08 AC 66 ms
5,376 KB
testcase_09 AC 67 ms
5,376 KB
testcase_10 AC 25 ms
5,376 KB
testcase_11 AC 0 ms
5,376 KB
testcase_12 AC 26 ms
5,376 KB
testcase_13 AC 28 ms
5,376 KB
testcase_14 AC 7 ms
5,376 KB
testcase_15 AC 42 ms
5,376 KB
testcase_16 AC 9 ms
5,376 KB
testcase_17 AC 8 ms
5,376 KB
testcase_18 AC 13 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 26 ms
5,376 KB
testcase_22 AC 6 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 47 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn read() -> Vec<i64> {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    it.next();
    it.flat_map(|s| s.parse()).collect()
}

fn main() {
    let mut a = read();
    let n = a.len();
    a.push(0);
    for i in (0..n).rev() {
        a[i] += a[i + 1];
    }
    let inf = (3000 * 100_000i64).pow(2);
    let mut dp = vec![inf; n + 1];
    dp[n] = 0;
    let mut deq = std::collections::VecDeque::<(i64, i64)>::new();
    let mut ans = vec![0; n];
    for i in 0..n {
        deq.clear();
        deq.push_back((-2 * a[n - i], dp[n - i] + a[n - i].pow(2)));
        for j in (0..(n - i)).rev() {
            let x = a[j + 1];
            while deq.len() > 1 {
                let (a, b) = deq[0];
                let (c, d) = deq[1];
                if a * x + b >= c * x + d {
                    deq.pop_front();
                } else {
                    break;
                }
            }
            let val = deq.front().map(|p| p.0 * x + p.1).unwrap() + x.pow(2);
            let (a, b) = (-2 * a[j], dp[j] + a[j].pow(2));
            dp[j] = val;
            while deq.len() > 1 {
                let len = deq.len();
                let (c, d) = deq[len - 1];
                let (e, f) = deq[len - 2];
                if (b - d) as i128 * (e - c) as i128 <= (d - f) as i128 * (c - a) as i128 {
                    deq.pop_back();
                } else {
                    break;
                }
            }
            deq.push_back((a, b));
        }
        let x = a[0];
        while deq.len() > 1 {
            let (a, b) = deq[0];
            let (c, d) = deq[1];
            if a * x + b >= c * x + d {
                deq.pop_front();
            } else {
                break;
            }
        }
        ans[n - 1 - i] = deq.front().map(|p| p.0 * x + p.1).unwrap() + x.pow(2);
    }
    for a in ans {
        println!("{}", a);
    }
}
0