結果
| 問題 |
No.703 ゴミ拾い Easy
|
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2019-10-07 02:49:08 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,045 bytes |
| コンパイル時間 | 13,169 ms |
| コンパイル使用メモリ | 378,096 KB |
| 実行使用メモリ | 23,392 KB |
| 最終ジャッジ日時 | 2024-10-12 01:40:22 |
| 合計ジャッジ時間 | 19,229 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 20 TLE * 1 -- * 25 |
ソースコード
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();
}
akakimidori