結果
問題 | No.703 ゴミ拾い Easy |
ユーザー |
![]() |
提出日時 | 2019-10-07 02:49:54 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 45 ms / 1,500 ms |
コード長 | 1,801 bytes |
コンパイル時間 | 12,923 ms |
コンパイル使用メモリ | 400,344 KB |
実行使用メモリ | 18,612 KB |
最終ジャッジ日時 | 2025-01-02 14:20:36 |
合計ジャッジ時間 | 14,914 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 46 |
ソースコード
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; } let ans = dp[n - 1]; println!("{}", ans); } fn main() { run(); }