結果

問題 No.703 ゴミ拾い Easy
ユーザー akakimidoriakakimidori
提出日時 2019-10-07 02:49:08
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 2,045 bytes
コンパイル時間 1,043 ms
コンパイル使用メモリ 160,768 KB
実行使用メモリ 21,888 KB
最終ジャッジ日時 2024-04-20 06:32:40
合計ジャッジ時間 6,727 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,624 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn run() {
    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 a: Vec<i64> = (0..n).map(|_| it.next().unwrap().parse().unwrap()).collect();
    let x: Vec<i64> = (0..n).map(|_| it.next().unwrap().parse().unwrap()).collect();
    let y: Vec<i64> = (0..n).map(|_| it.next().unwrap().parse().unwrap()).collect();
    let mut dp = vec![0; n];
    let mut deq = std::collections::VecDeque::<(i64, i64)>::new();
    dp[0] = y[0] * y[0] + (a[0] - x[0]) * (a[0] - x[0]);
    deq.push_back((-2 * x[0], y[0] * y[0] + x[0] * x[0]));
    for i in 1..n {
        let (a, x, y) = (a[i], x[i], y[i]);
        let (p, q) = (-2 * x, dp[i - 1] + y * y + x * x);
        let (c, d) = *deq.back().unwrap();
        if p < c || (p == c && q < d ) {
            if p == c {
                deq.pop_back();
            }
            while deq.len() >= 2 {
                let len = deq.len();
                let (c, d) = *deq.get(len - 1).unwrap();
                let (e, f) = *deq.get(len - 2).unwrap();
                if (e - p) * (q - d) <= (c - p) * (q - f) {
                    deq.pop_back();
                } else {
                    break;
                }
            }
            deq.push_back((p, q));
        }
        while deq.len() >= 2 {
            let (c, d) = *deq.get(0).unwrap();
            let (e, f) = *deq.get(1).unwrap();
            if c * a + d >= e * a + f {
                deq.pop_front();
            } else {
                break;
            }
        }
        let (c, d) = *deq.front().unwrap();
        dp[i] = c * a + d + a * a;
    }
    for i in 1..n {
        let mut ans = y[0] * y[0] + (x[0] - a[i]) * (x[0] - a[i]);
        for j in 1..=i {
            ans = std::cmp::min(ans, dp[j - 1] + y[j] * y[j] + (x[j] - a[i]) * (x[j] - a[i]));
        }
        dp[i] = ans;
    }
    let ans = dp[n - 1];
    println!("{}", ans);
}

fn main() {
    run();
}
0