結果
問題 |
No.952 危険な火薬庫
|
ユーザー |
![]() |
提出日時 | 2021-01-09 02:02:40 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 63 ms / 2,000 ms |
コード長 | 1,985 bytes |
コンパイル時間 | 12,801 ms |
コンパイル使用メモリ | 379,764 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-16 21:37:55 |
合計ジャッジ時間 | 14,389 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 23 |
ソースコード
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) as i128 * (e - c) as i128 <= (d - f) as i128 * (c - a) as i128 { 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); } }