結果

問題 No.1369 交換門松列・竹
ユーザー cympfhcympfh
提出日時 2021-01-29 22:18:23
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,698 bytes
コンパイル時間 1,220 ms
コンパイル使用メモリ 158,976 KB
実行使用メモリ 5,256 KB
最終ジャッジ日時 2023-09-09 15:46:19
合計ジャッジ時間 2,902 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 12 ms
4,944 KB
testcase_16 AC 20 ms
4,972 KB
testcase_17 AC 27 ms
4,976 KB
testcase_18 AC 22 ms
4,944 KB
testcase_19 AC 12 ms
5,044 KB
testcase_20 AC 66 ms
5,044 KB
testcase_21 AC 15 ms
4,944 KB
testcase_22 AC 16 ms
5,036 KB
testcase_23 AC 23 ms
5,028 KB
testcase_24 AC 83 ms
5,024 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 21 ms
5,164 KB
testcase_29 AC 32 ms
5,256 KB
testcase_30 AC 13 ms
4,940 KB
testcase_31 AC 12 ms
5,052 KB
testcase_32 WA -
testcase_33 AC 25 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports, unused_macros, dead_code)]
use std::{cmp::*, collections::*};

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    for _ in 0..n {
        let m: usize = sc.cin();
        let mut a: Vec<u64> = sc.vec(m);

        let mut doubt = HashSet::new();
        for i in 2..m {
            if !is_kadomatsu(&vec![a[i - 2], a[i - 1], a[i]]) {
                doubt.insert(i - 2);
                doubt.insert(i - 1);
                doubt.insert(i);
            }
        }
        let doubt: Vec<_> = doubt.iter().collect();
        let n = doubt.len();
        trace!(&a);
        trace!(&doubt);

        let mut ans = "No";
        if n < 20 {
            for ii in 0..n {
                for jj in 0..ii {
                    let i = *doubt[ii];
                    let j = *doubt[jj];
                    let tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;

                    let mut ok = true;
                    for k in 2..m {
                        if !is_kadomatsu(&vec![a[k - 2], a[k - 1], a[k]]) {
                            ok = false;
                            break;
                        }
                    }
                    if ok {
                        trace!("OK", &a);
                        ans = "Yes";
                        break;
                    }

                    let tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;
                }
            }
        }
        put!(ans);
    }
}

fn is_kadomatsu(a: &Vec<u64>) -> bool {
    (a[1] < a[0] && a[1] < a[2] && a[0] != a[2]) || (a[1] > a[0] && a[1] > a[2] && a[0] != a[2])
}

// {{{
use std::io::{self, Write};
use std::str::FromStr;

struct Scanner {
    stdin: io::Stdin,
    buffer: VecDeque<String>,
}
impl Scanner {
    fn new() -> Self {
        Self {
            stdin: io::stdin(),
            buffer: VecDeque::new(),
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.is_empty() {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> {
        self.cin::<String>().chars().collect()
    }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.cin()).collect()
    }
}
fn flush() {
    std::io::stdout().flush().unwrap();
}
#[macro_export]
macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
#[macro_export]
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
#[macro_export]
macro_rules! trace {
    ($x:expr) => {
        #[cfg(debug_assertions)]
        eprintln!(">>> {} = {:?}", stringify!($x), $x)
    };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
#[macro_export]
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}
// }}}
0