// ---------- begin SegmentTree Point update Range query ---------- mod segment_tree { pub struct PURQ T> { n: usize, a: Vec, id: T, op: F, } #[allow(dead_code)] impl T> PURQ { pub fn new(n: usize, id: T, op: F) -> PURQ { let mut k = 1; while k < n { k *= 2; } PURQ { n: k, a: vec![id.clone(); 2 * k], id: id, op: op, } } pub fn update(&mut self, x: usize, v: T) { let mut k = self.n + x; let a = &mut self.a; a[k] = v; k >>= 1; while k > 0 { a[k] = (self.op)(a[2 * k].clone(), a[2 * k + 1].clone()); k >>= 1; } } pub fn update_tmp(&mut self, x: usize, v: T) { self.a[x + self.n] = v; } pub fn update_all(&mut self) { for k in (1..(self.n)).rev() { self.a[k] = (self.op)(self.a[2 * k].clone(), self.a[2 * k + 1].clone()); } } pub fn find(&self, mut l: usize, mut r: usize) -> T { let mut p = self.id.clone(); let mut q = self.id.clone(); l += self.n; r += self.n; while l < r { if (l & 1) == 1 { p = (self.op)(p, self.a[l].clone()); l += 1; } if (r & 1) == 1 { r -= 1; q = (self.op)(self.a[r].clone(), q); } l >>= 1; r >>= 1; } (self.op)(p, q) } } } // ---------- end SegmentTree Point update Range query ---------- //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::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // fn run() { input! { n: usize, a: [u32; n], } let mut b = a.clone(); b.sort(); b.dedup(); let mut a = a .into_iter() .map(|a| b.binary_search(&a).unwrap()) .collect::>(); let mut ans = 1; for _ in 0..2 { let mut left = vec![0; n]; let mut seg = segment_tree::PURQ::new(n, 0, |a, b| std::cmp::max(a, b)); for (l, a) in left.iter_mut().zip(a.iter()) { let v = if *a > 0 { seg.find(0, *a) } else { 0 }; *l = v; seg.update(*a, v + 1); } let mut seg = segment_tree::PURQ::new(n, 0, |a, b| std::cmp::max(a, b)); for (l, a) in left.iter().zip(a.iter()).rev() { let v = if *a > 0 { seg.find(0, *a) } else { 0 }; ans = std::cmp::max(ans, std::cmp::min(*l, v) + 1); seg.update(*a, v + 1); } for a in a.iter_mut() { *a = b.len() - 1 - *a; } } println!("{}", ans - 1); } fn main() { run(); }