結果
問題 | No.1435 Mmm...... |
ユーザー | akakimidori |
提出日時 | 2021-03-19 22:03:33 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 38 ms / 2,000 ms |
コード長 | 5,465 bytes |
コンパイル時間 | 14,902 ms |
コンパイル使用メモリ | 396,800 KB |
実行使用メモリ | 30,036 KB |
最終ジャッジ日時 | 2024-11-18 22:23:41 |
合計ジャッジ時間 | 16,886 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,820 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,820 KB |
testcase_05 | AC | 1 ms
6,820 KB |
testcase_06 | AC | 15 ms
6,816 KB |
testcase_07 | AC | 18 ms
6,816 KB |
testcase_08 | AC | 21 ms
6,816 KB |
testcase_09 | AC | 20 ms
6,816 KB |
testcase_10 | AC | 16 ms
6,816 KB |
testcase_11 | AC | 17 ms
6,816 KB |
testcase_12 | AC | 16 ms
6,820 KB |
testcase_13 | AC | 16 ms
6,816 KB |
testcase_14 | AC | 11 ms
6,816 KB |
testcase_15 | AC | 22 ms
6,820 KB |
testcase_16 | AC | 17 ms
6,816 KB |
testcase_17 | AC | 12 ms
6,816 KB |
testcase_18 | AC | 22 ms
6,816 KB |
testcase_19 | AC | 15 ms
6,816 KB |
testcase_20 | AC | 21 ms
6,820 KB |
testcase_21 | AC | 38 ms
30,036 KB |
testcase_22 | AC | 29 ms
21,292 KB |
testcase_23 | AC | 29 ms
6,816 KB |
testcase_24 | AC | 29 ms
6,820 KB |
testcase_25 | AC | 28 ms
6,816 KB |
testcase_26 | AC | 29 ms
6,820 KB |
testcase_27 | AC | 28 ms
6,820 KB |
ソースコード
// ---------- begin Foldable Deque ---------- pub struct FoldableDeque<T, F> { front: Vec<(T, T)>, back: Vec<(T, T)>, op: F, } impl<T, F> FoldableDeque<T, F> where T: Clone, F: Fn(&T, &T) -> T, { pub fn new(op: F) -> Self { FoldableDeque { front: Vec::new(), back: Vec::new(), op: op, } } pub fn find(&self) -> Option<T> { match (self.front.last(), self.back.last()) { (Some(a), Some(b)) => Some((self.op)(&a.1, &b.1)), (Some(a), None) => Some(a.1.clone()), (None, Some(b)) => Some(b.1.clone()), (None, None) => None, } } pub fn clear(&mut self) { self.front.clear(); self.back.clear(); } pub fn len(&self) -> usize { self.front.len() + self.back.len() } pub fn push_back(&mut self, val: T) { let sum = self .back .last() .map_or_else(|| val.clone(), |p| (self.op)(&p.1, &val)); self.back.push((val, sum)); } pub fn push_front(&mut self, val: T) { let sum = self .front .last() .map_or_else(|| val.clone(), |p| (self.op)(&val, &p.1)); self.front.push((val, sum)); } pub fn pop_front(&mut self) -> Option<T> { if self.front.is_empty() { let a = self.back.clone(); let m = (self.back.len() + 1) / 2; self.back.clear(); let (f, b) = a.split_at(m); for v in f.iter().rev() { self.push_front(v.0.clone()); } for v in b.iter() { self.push_back(v.0.clone()); } } self.front.pop().map(|p| p.0) } pub fn pop_back(&mut self) -> Option<T> { if self.back.is_empty() { let a = self.front.clone(); let m = (self.front.len() + 1) / 2; self.front.clear(); let (f, b) = a.split_at(m); for v in b.iter() { self.push_front(v.0.clone()); } for v in f.iter().rev() { self.push_back(v.0.clone()); } } self.back.pop().map(|p| p.0) } pub fn front(&self) -> Option<&T> { self.front .last() .map_or_else(|| self.back.get(0).map(|p| &p.0), |p| Some(&p.0)) } pub fn back(&self) -> Option<&T> { self.back .last() .map_or_else(|| self.front.get(0).map(|p| &p.0), |p| Some(&p.0)) } pub fn get(&self, k: usize) -> Option<&T> { if let Some(po) = self.front.get(self.front.len() - 1 - k) { return Some(&po.0); } let k = k - self.front.len(); self.back.get(k).map(|po| &po.0) } } // ---------- end Foldable Deque ---------- // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::<Vec<u8>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- fn run() { input! { n: usize, a: [u32; n], } let mut p = vec![(0, 0); n]; for (i, a) in a.iter().enumerate() { p[i] = (*a, i as u32); } type T = (u32, u32); type E = (T, T, T); let merge = |a: &E, b: &E| -> E { let (x, y) = if a.0 < b.0 { (a.0, a.1.min(b.0)) } else { (b.0, a.0.min(b.1)) }; (x, y, a.2.max(b.2)) }; let mut deq = FoldableDeque::new(merge); let mut ans = 0; let mut r = 0; let inf = std::u32::MAX / 2; for i in 0..(n - 1) { while r <= i + 1 { let a = p[r]; r += 1; deq.push_back((a, (inf, inf), a)); } while r < n { let (x, y, z) = deq.find().unwrap(); if x.0 + y.0 < z.0 { break; } let a = p[r]; r += 1; deq.push_back((a, (inf, inf), a)); } let (x, y, z) = deq.find().unwrap(); if x.0 + y.0 >= z.0 { ans += n - i - 1; } else { ans += r - i - 2; } deq.pop_front(); } println!("{}", ans); } fn main() { run(); }