結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 0 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 90 ms
5,376 KB
testcase_04 AC 89 ms
5,376 KB
testcase_05 AC 71 ms
5,376 KB
testcase_06 AC 108 ms
5,376 KB
testcase_07 AC 37 ms
5,376 KB
testcase_08 AC 123 ms
5,376 KB
testcase_09 AC 129 ms
5,376 KB
testcase_10 AC 48 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 52 ms
5,376 KB
testcase_13 AC 54 ms
5,376 KB
testcase_14 AC 13 ms
5,376 KB
testcase_15 AC 79 ms
5,376 KB
testcase_16 AC 17 ms
5,376 KB
testcase_17 AC 13 ms
5,376 KB
testcase_18 AC 23 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 46 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 86 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).div_euclid(c - a) <= (d - f).div_euclid(e - c) {
                    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