結果
問題 | No.409 ダイエット |
ユーザー | ngtkana |
提出日時 | 2021-12-19 03:24:30 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 400 ms / 2,000 ms |
コード長 | 7,011 bytes |
コンパイル時間 | 15,065 ms |
コンパイル使用メモリ | 378,124 KB |
実行使用メモリ | 24,576 KB |
最終ジャッジ日時 | 2024-09-15 14:24:14 |
合計ジャッジ時間 | 28,100 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 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 | 1 ms
5,376 KB |
testcase_15 | AC | 1 ms
5,376 KB |
testcase_16 | AC | 1 ms
5,376 KB |
testcase_17 | AC | 1 ms
5,376 KB |
testcase_18 | AC | 1 ms
5,376 KB |
testcase_19 | AC | 1 ms
5,376 KB |
testcase_20 | AC | 1 ms
5,376 KB |
testcase_21 | AC | 1 ms
5,376 KB |
testcase_22 | AC | 1 ms
5,376 KB |
testcase_23 | AC | 1 ms
5,376 KB |
testcase_24 | AC | 1 ms
5,376 KB |
testcase_25 | AC | 1 ms
5,376 KB |
testcase_26 | AC | 1 ms
5,376 KB |
testcase_27 | AC | 1 ms
5,376 KB |
testcase_28 | AC | 1 ms
5,376 KB |
testcase_29 | AC | 1 ms
5,376 KB |
testcase_30 | AC | 1 ms
5,376 KB |
testcase_31 | AC | 1 ms
5,376 KB |
testcase_32 | AC | 1 ms
5,376 KB |
testcase_33 | AC | 1 ms
5,376 KB |
testcase_34 | AC | 1 ms
5,376 KB |
testcase_35 | AC | 3 ms
5,376 KB |
testcase_36 | AC | 4 ms
5,376 KB |
testcase_37 | AC | 4 ms
5,376 KB |
testcase_38 | AC | 3 ms
5,376 KB |
testcase_39 | AC | 4 ms
5,376 KB |
testcase_40 | AC | 4 ms
5,376 KB |
testcase_41 | AC | 4 ms
5,376 KB |
testcase_42 | AC | 4 ms
5,376 KB |
testcase_43 | AC | 3 ms
5,376 KB |
testcase_44 | AC | 4 ms
5,376 KB |
testcase_45 | AC | 4 ms
5,376 KB |
testcase_46 | AC | 4 ms
5,376 KB |
testcase_47 | AC | 4 ms
5,376 KB |
testcase_48 | AC | 4 ms
5,376 KB |
testcase_49 | AC | 4 ms
5,376 KB |
testcase_50 | AC | 4 ms
5,376 KB |
testcase_51 | AC | 4 ms
5,376 KB |
testcase_52 | AC | 4 ms
5,376 KB |
testcase_53 | AC | 4 ms
5,376 KB |
testcase_54 | AC | 4 ms
5,376 KB |
testcase_55 | AC | 184 ms
12,288 KB |
testcase_56 | AC | 21 ms
5,376 KB |
testcase_57 | AC | 400 ms
23,936 KB |
testcase_58 | AC | 159 ms
11,264 KB |
testcase_59 | AC | 244 ms
12,928 KB |
testcase_60 | AC | 142 ms
10,112 KB |
testcase_61 | AC | 349 ms
21,248 KB |
testcase_62 | AC | 362 ms
22,400 KB |
testcase_63 | AC | 347 ms
19,712 KB |
testcase_64 | AC | 179 ms
11,136 KB |
testcase_65 | AC | 362 ms
15,616 KB |
testcase_66 | AC | 397 ms
22,400 KB |
testcase_67 | AC | 291 ms
18,048 KB |
testcase_68 | AC | 236 ms
12,800 KB |
testcase_69 | AC | 345 ms
19,072 KB |
testcase_70 | AC | 319 ms
19,840 KB |
testcase_71 | AC | 169 ms
12,160 KB |
testcase_72 | AC | 392 ms
24,576 KB |
testcase_73 | AC | 372 ms
19,200 KB |
testcase_74 | AC | 216 ms
14,976 KB |
testcase_75 | AC | 375 ms
20,992 KB |
testcase_76 | AC | 255 ms
16,896 KB |
testcase_77 | AC | 171 ms
9,728 KB |
testcase_78 | AC | 186 ms
13,312 KB |
testcase_79 | AC | 152 ms
10,624 KB |
testcase_80 | AC | 368 ms
23,168 KB |
testcase_81 | AC | 373 ms
19,968 KB |
testcase_82 | AC | 261 ms
17,024 KB |
testcase_83 | AC | 293 ms
17,792 KB |
testcase_84 | AC | 235 ms
12,416 KB |
testcase_85 | AC | 23 ms
5,376 KB |
testcase_86 | AC | 228 ms
12,800 KB |
testcase_87 | AC | 321 ms
20,352 KB |
testcase_88 | AC | 128 ms
8,448 KB |
testcase_89 | AC | 260 ms
8,064 KB |
testcase_90 | AC | 116 ms
5,760 KB |
testcase_91 | AC | 278 ms
8,448 KB |
ソースコード
use cht_integer::ConvexHullTrick; #[allow(unused_imports)] #[cfg(feature = "dbg")] use dbg::lg; use proconio::{fastout, input}; use std::iter::once; #[fastout] fn main() { input! { n: usize, a: i64, b: i64, w: i64, d: [i64; n], } let mut cht = ConvexHullTrick::new(); cht.insert(0, -(2 * w)); for (i, d) in once(0).chain(d.iter().copied()).enumerate() { let x0 = i as i64; let y0 = (-cht.eval(x0) + b * x0 * x0) / 2 + d; let tilt = -2 * a + (-2 * x0 - 1) * b; let intercept = 2 * y0 + 2 * (x0 + 1) * a + b * x0 * (x0 + 1); cht.insert(-tilt, -intercept); } let end = n as i64 + 1; let ans = (-cht.eval(end) + b * end * end) / 2; println!("{}", ans); } // cht_integer {{{ #[allow(dead_code)] mod cht_integer { use std::{ borrow::Borrow, collections::BTreeSet, fmt::Debug, i64::{MAX, MIN}, }; #[derive(Clone, Debug, Default, Hash, PartialEq)] pub struct ConvexHullTrick { set: BTreeSet<Segment>, } impl ConvexHullTrick { pub fn new() -> Self { Self::default() } pub fn multieval(&self, xs: impl Iterator<Item = i64>) -> Vec<i64> { xs.map(|x| self.eval(x)).collect() } pub fn collect_lines(&self) -> Vec<(i64, i64)> { self.set .iter() .map(|&seg| (seg.line.p, -seg.line.q)) .collect() } pub fn eval(&self, x: i64) -> i64 { assert!( !self.set.is_empty(), "empty maximum is the negative infinity" ); self.set.range(Max(x)..).next().unwrap().line.eval(x) } pub fn lave(&self, p: i64) -> Option<i64> { assert!( !self.set.is_empty(), "empty maximum is the negative infinity" ); let &Segment { line: Line { p: p1, q: q1 }, min: Min(min), max: _, } = self.set.range(p..).next()?; if min == MIN { if p1 == p { Some(q1) } else { None } } else { Some(q1 - (p1 - p) * min) } } pub fn insert(&mut self, tilt: i64, intercept: i64) -> bool { let q = -intercept; let p = tilt; if !self.set.is_empty() && self.lave(p).map_or(false, |c| c <= q) { return false; } self.set.take(&p); let line = Line { p, q }; while let Some(&seg1) = self.set.range(..p).next_back() { if seg1.min.0 == MIN || line.eval(seg1.min.0) < seg1.line.eval(seg1.min.0) { break; } self.set.remove(&seg1); } while let Some(&seg1) = self.set.range(p..).next() { if seg1.max.0 == MAX || line.eval(seg1.max.0) < seg1.line.eval(seg1.max.0) { break; } self.set.remove(&seg1); } if let Some(&seg1) = self.set.range(..p).next_back() { self.set.remove(&seg1); match seg1.line.brace(line) { Err(x) => { debug_assert!(seg1.min.0 < x); self.set.insert(Segment { max: Max(x), ..seg1 }); } Ok(brace) => { if seg1.min.0 < brace.min.0 { self.set.insert(Segment { max: Max(brace.min.0), ..seg1 }); } self.set.insert(brace); } } } if let Some(&seg1) = self.set.range(p..).next() { self.set.remove(&seg1); match line.brace(seg1.line) { Err(x) => { debug_assert!(x < seg1.max.0); self.set.insert(Segment { min: Min(x), ..seg1 }); } Ok(brace) => { if brace.max.0 < seg1.max.0 { self.set.insert(Segment { min: Min(brace.max.0), ..seg1 }); } self.set.insert(brace); } } } let min = Min(self .set .range(..p) .next_back() .map_or(MIN, |seg1| seg1.max.0)); let max = Max(self.set.range(p..).next().map_or(MAX, |seg1| seg1.min.0)); if min.0 < max.0 { self.set.insert(Segment { line, min, max }); } true } } #[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Copy)] struct Min(i64); #[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Copy)] struct Max(i64); #[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Copy)] struct Line { p: i64, q: i64, } impl Line { fn eval(&self, x: i64) -> i64 { self.p * x - self.q } fn brace(self, other: Self) -> Result<Segment, i64> { let Self { p: p0, q: q0 } = self; let Self { p: p1, q: q1 } = other; debug_assert!(p0 < p1); let x0 = (q1 - q0).div_euclid(p1 - p0); if x0 * (p1 - p0) == (q1 - q0) { return Err(x0); } let x1 = x0 + 1; let p = (p1 * x1 - p0 * x0) - (q1 - q0); let q = (p1 - p0) * x0 * x1 - q1 * x0 + q0 * x1; debug_assert_eq!(p * x0 - q, p0 * x0 - q0); debug_assert_eq!(p * x1 - q, p1 * x1 - q1); Ok(Segment { line: Self { p, q }, min: Min(x0), max: Max(x1), }) } } #[derive(Clone, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Copy)] struct Segment { line: Line, min: Min, max: Max, } impl Borrow<i64> for Segment { fn borrow(&self) -> &i64 { &self.line.p } } impl Borrow<Min> for Segment { fn borrow(&self) -> &Min { &self.min } } impl Borrow<Max> for Segment { fn borrow(&self) -> &Max { &self.max } } } // }}} // template {{{ #[cfg(not(feature = "dbg"))] #[allow(unused_macros)] #[macro_export] macro_rules! lg { ($($expr:expr),*) => {}; } // }}}