結果

問題 No.1369 交換門松列・竹
ユーザー StrorkisStrorkis
提出日時 2021-02-01 19:58:57
言語 Rust
(1.77.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,030 bytes
コンパイル時間 677 ms
コンパイル使用メモリ 99,236 KB
最終ジャッジ日時 2023-08-28 08:24:23
合計ジャッジ時間 1,246 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error: format argument must be a string literal
  --> Main.rs:24:42
   |
24 |                         Err(e) => panic!(e), 
   |                                          ^
   |
help: you might be missing a string literal to format with
   |
24 |                         Err(e) => panic!("{}", e), 
   |                                          +++++

error: aborting due to previous error

ソースコード

diff #

mod my {
    #[macro_export]
    macro_rules! scan {
        ($sc:expr, [$t:tt; $n:expr]) => (
            (0..$n).map(|_| scan!($sc, $t)).collect::<Vec<_>>()
        );
        ($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::<Vec<_>>());
        ($sc:expr, $t:ty) => ($sc.next::<$t>());
    }

    pub mod io {
        use std::io::{BufRead, ErrorKind};
        use std::str;

        fn scan<R: BufRead>(r: &mut R, buf: &mut Vec<u8>) {
            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: R, buf: Vec<u8> }

        impl<R: BufRead> Scanner<R> {
            pub fn new(r: R) -> Self {
                Self { r, buf: Vec::new() }
            }

            pub fn next<T: str::FromStr>(&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<BTreeSet<usize>> {
    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<R: BufRead, W: Write>(sc: &mut Scanner<R>, 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);
}
0