結果

問題 No.952 危険な火薬庫
ユーザー akakimidoriakakimidori
提出日時 2019-12-15 00:48:27
言語 Rust
(1.72.1)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,918 bytes
コンパイル時間 3,557 ms
コンパイル使用メモリ 158,476 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 18:11:13
合計ジャッジ時間 5,518 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 95 ms
4,376 KB
testcase_04 AC 94 ms
4,380 KB
testcase_05 AC 75 ms
4,380 KB
testcase_06 AC 114 ms
4,380 KB
testcase_07 AC 38 ms
4,376 KB
testcase_08 AC 130 ms
4,380 KB
testcase_09 AC 133 ms
4,380 KB
testcase_10 AC 51 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 53 ms
4,380 KB
testcase_13 AC 57 ms
4,380 KB
testcase_14 AC 13 ms
4,376 KB
testcase_15 AC 84 ms
4,380 KB
testcase_16 AC 17 ms
4,380 KB
testcase_17 AC 13 ms
4,380 KB
testcase_18 AC 24 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 49 ms
4,380 KB
testcase_22 AC 10 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 5 ms
4,380 KB
testcase_25 AC 101 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;
use std::io::Write;

fn run() {
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let mut s: Vec<i128> = (0..n).map(|_| it.next().unwrap().parse().unwrap()).collect();
    s.push(0);
    s.reverse();
    s.push(0);
    for i in 1..=(n + 1) {
        s[i] += s[i - 1];
    }
    let mut ans = vec![std::i128::MAX; n + 1];
    let mut dp = vec![std::i64::MAX as i128 ; n + 2];
    dp[0] = 0;
    for k in 1..=n {
        let mut next = dp.clone();
        let mut deq = std::collections::VecDeque::<(i128, i128)>::new();
        deq.push_back((-2 * s[k - 1], dp[k - 1] + s[k - 1] * s[k - 1]));
        for i in k..=(n + 1) {
            let x = s[i - 1];
            while deq.len() > 1 {
                let (a, b) = *deq.get(0).unwrap();
                let (c, d) = *deq.get(1).unwrap();
                if a * x + b >= c * x + d {
                    deq.pop_front();
                } else {
                    break;
                }
            }
            let (a, b) = *deq.front().unwrap();
            next[i] = a * x + b + x * x;
            let (a, b) = (-2 * s[i], dp[i] + s[i] * s[i]);
            while deq.len() > 1 {
                let len = deq.len();
                let (c, d) = *deq.get(len - 1).unwrap();
                let (e, f) = *deq.get(len - 2).unwrap();
                if (b - d) * (e - a) <= (b - f) * (c - a) {
                    deq.pop_back();
                } else {
                    break;
                }
            }
            deq.push_back((a, b));
        }
        dp = next;
        ans[n + 1 - k] = dp[n + 1];
    }
    for i in 1..=n {
        writeln!(out, "{}", ans[i]).ok();
    }
}

fn main() {
    run();
}
0