mod my { #[macro_export] macro_rules! scan { ($sc:expr, [$t:tt; $n:expr]) => ( (0..$n).map(|_| scan!($sc, $t)).collect::>() ); ($sc:expr, ($($t:tt),*)) => (($(scan!($sc, $t)),*)); ($sc:expr, Usize1) => (scan!($sc, usize) - 1); ($sc:expr, Bytes) => (scan!($sc, String).into_bytes()); ($sc:expr, Chars) => (scan!($sc, String).chars().collect::>()); ($sc:expr, $t:ty) => ($sc.next::<$t>()); } pub mod io { use std::io::{BufRead, ErrorKind}; use std::str; fn scan(r: &mut R, buf: &mut Vec) { loop { let (done, used) = { let available = match r.fill_buf() { Ok(n) => n, Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => panic!(e), }; match available.iter().position(u8::is_ascii_whitespace) { Some(i) => { buf.extend_from_slice(&available[..i]); (buf.len() > 0, i + 1) } None => { buf.extend_from_slice(available); (false, available.len()) } } }; r.consume(used); if done || used == 0 { return; } } } pub struct Scanner { r: R, buf: Vec } impl Scanner { pub fn new(r: R) -> Self { Self { r, buf: Vec::new() } } pub fn next(&mut self) -> T { self.buf.clear(); scan(&mut self.r, &mut self.buf); str::from_utf8(&self.buf).unwrap().parse().ok().unwrap() } } } } use std::io::{BufRead, Write}; use std::collections::BTreeSet; use my::io::Scanner; fn is_kadomatsu(a: &[usize]) -> bool { a[0] != a[2] && (a[0] > a[1] && a[1] < a[2] || a[0] < a[1] && a[1] > a[2]) } fn update(a: &[usize], i: usize) -> Option> { let mut set = BTreeSet::new(); let left = i.saturating_sub(2); let right = (i + 3).min(a.len() - 2); let len = right - left; for (i, a) in a.windows(3).enumerate().skip(left).take(len) { if is_kadomatsu(a) { set.insert(i); } else { return None; } } Some(set) } fn run(sc: &mut Scanner, wr: &mut W) { 't: for _ in 0..scan!(sc, usize) { let n = scan!(sc, usize); let mut a = scan!(sc, [usize; n]); let pos: BTreeSet<_> = { a.windows(3).enumerate() .filter_map(|(i, a)| if !is_kadomatsu(a) { Some(i) } else { None }) .collect() }; if pos.len() > 6 { writeln!(wr, "No").ok(); continue 't; } let bads: BTreeSet<_> = { pos.iter().flat_map(|&i| i..(i + 3).min(a.len())).collect() }; for &i in &bads { for j in 0..n { if i == j { continue; } a.swap(i, j); if let Some(mut s) = update(&a, i) { if let Some(s1) = update(&a, j) { s = s.union(&s1).cloned().collect(); if pos.difference(&s).count() == 0 { writeln!(wr, "Yes").ok(); continue 't; } } } a.swap(i, j); } } writeln!(wr, "No").ok(); } } fn main() { let (stdin, stdout) = (std::io::stdin(), std::io::stdout()); let sc = &mut Scanner::new(std::io::BufReader::new(stdin.lock())); let wr = &mut std::io::BufWriter::new(stdout.lock()); run(sc, wr); }