#![allow(dead_code, unused_mut)] #![allow(non_snake_case)] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let mut 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, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } macro_rules ! max {($ a : expr $ (, ) * ) => {{$ a } } ; ($ a : expr , $ b : expr $ (, ) * ) => {{std :: cmp :: max ($ a , $ b ) } } ; ($ a : expr , $ ($ rest : expr ) ,+ $ (, ) * ) => {{std :: cmp :: max ($ a , max ! ($ ($ rest ) ,+ ) ) } } ; } macro_rules ! chmax {($ base : expr , $ ($ cmps : expr ) ,+ $ (, ) * ) => {{let cmp_max = max ! ($ ($ cmps ) ,+ ) ; if $ base < cmp_max {$ base = cmp_max ; true } else {false } } } ; } const MOD: usize = 998244353; fn main() { input!{ n:usize, a:[u32; n] } let mut ne = vec![0; n]; let mut se = vec![0; n]; let mut nw = vec![0; n]; let mut sw = vec![0; n]; let mut buf1 = Vec::new(); let mut buf2 = Vec::new(); for i in 0..n { while buf1.last().unwrap_or(&1_000_000_01) <= &a[i] { buf1.pop(); } se[i] = buf1.len(); while buf2.last().unwrap_or(&0) >= &a[i] { buf2.pop(); } ne[i] = buf2.len(); buf1.push(a[i]); buf2.push(a[i]); } let mut buf1 = Vec::new(); let mut buf2 = Vec::new(); for i in (0..n).rev() { while buf1.last().unwrap_or(&1_000_000_01) <= &a[i] { buf1.pop(); } sw[i] = buf1.len(); while buf2.last().unwrap_or(&0) >= &a[i] { buf2.pop(); } nw[i] = buf2.len(); buf1.push(a[i]); buf2.push(a[i]); } // println!("{:?}", ne); // println!("{:?}", nw); // println!("{:?}", se); // println!("{:?}", sw); let mut ans = 0; for i in 0..n { chmax!(ans, std::cmp::min(se[i], sw[i])); chmax!(ans, std::cmp::min(ne[i], nw[i])); } println!("{}", ans); }