結果

問題 No.1707 Simple Range Reverse Problem
ユーザー StrorkisStrorkis
提出日時 2021-10-16 10:37:33
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 5,809 bytes
コンパイル時間 13,543 ms
コンパイル使用メモリ 396,280 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-17 19:18:54
合計ジャッジ時間 12,306 ms
ジャッジサーバーID
(参考情報)
judge1 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod input {
    pub trait ReadIntHelper: PartialOrd + Copy {
        fn min_value() -> Self;
        fn from_u32(u: u32) -> Self;
        fn checked_mul(&self, other: u32) -> Option<Self>;
        fn checked_sub(&self, other: u32) -> Option<Self>;
        fn checked_add(&self, other: u32) -> Option<Self>;
    }

    macro_rules! impl_read_int_helper {
        ($($t:ident)*) => ($(impl ReadIntHelper for $t {
            #[inline]
            fn min_value() -> Self { std::$t::MIN }
            #[inline]
            fn from_u32(u: u32) -> Self { u as Self }
            #[inline]
            fn checked_mul(&self, other: u32) -> Option<Self> {
                Self::checked_mul(*self, other as Self)
            }
            #[inline]
            fn checked_sub(&self, other: u32) -> Option<Self> {
                Self::checked_sub(*self, other as Self)
            }
            #[inline]
            fn checked_add(&self, other: u32) -> Option<Self> {
                Self::checked_add(*self, other as Self)
            }
        })*)
    }
    impl_read_int_helper! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }

    #[inline]
    pub fn read_int<R: std::io::BufRead, T: ReadIntHelper>(r: &mut R) -> T {
        let is_signed_ty = T::from_u32(0) > T::min_value();

        let mut is_positive = true;
        let mut is_empty = true;
        let mut is_only_sign = true;
        let mut result = T::from_u32(0);
        loop {
            let buf = match r.fill_buf() {
                Ok(buf) => buf,
                Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
                Err(e) => panic!("{}", e), 
            };
            let (done, used, mut buf) = match buf.iter().position(u8::is_ascii_whitespace) {
                Some(i) => (i > 0 || !is_empty, i + 1, &buf[..i]),
                None => (buf.is_empty(), buf.len(), buf),
            };

            if is_empty && buf.len() > 0 {
                is_empty = false;
                buf = match buf[0] {
                    b'+' => &buf[1..],
                    b'-' if is_signed_ty => {
                        is_positive = false;
                        &buf[1..]
                    }
                    _ => buf,
                };
            }
            if buf.len() > 0 {
                is_only_sign = false;
            }

            if is_positive {
                for &c in buf {
                    let x = (c as char).to_digit(10).expect("InvalidDigit");
                    result = result.checked_mul(10).expect("PosOverflow");
                    result = result.checked_add(x).expect("PosOverflow");
                }
            } else {
                for &c in buf {
                    let x = (c as char).to_digit(10).expect("InvalidDigit");
                    result = result.checked_mul(10).expect("NegOverflow");
                    result = result.checked_sub(x).expect("NegOverflow");
                }
            }

            r.consume(used);

            if done {
                if is_empty {
                    panic!("Empty");
                }
                if is_only_sign {
                    panic!("InvalidDigit");
                }        
                return result;
            }
        }
    }

    #[inline]
    pub fn read_bytes<R: std::io::BufRead>(r: &mut R) -> Vec<u8> {
        let mut result = Vec::new();
        loop {
            let buf = match r.fill_buf() {
                Ok(buf) => buf,
                Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
                Err(e) => panic!("{}", e), 
            };
            let (done, used, buf) = match buf.iter().position(u8::is_ascii_whitespace) {
                Some(i) => (i > 0 || !result.is_empty(), i + 1, &buf[..i]),
                None => (buf.is_empty(), buf.len(), buf),
            };
            result.extend_from_slice(buf);
            r.consume(used);
            if done { return result; }
        }
    }

    #[macro_export]
    macro_rules! read {
        ($r:expr, [$t:tt; $n:expr]) => ((0..$n).map(|_| read!($r, $t)).collect::<Vec<_>>());
        ($r:expr, [$t:tt]) => (read!($r, [$t; read!($r, usize)]));
        ($r:expr, ($($t:tt),*)) => (($(read!($r, $t)),*));
        ($r:expr, Usize1) => (read!($r, usize) - 1);
        ($r:expr, String) => (String::from_utf8(read!($r, Bytes)).unwrap());
        ($r:expr, Bytes) => (input::read_bytes($r));
        ($r:expr, $t:ty) => (input::read_int::<_, $t>($r));
    }

    #[macro_export]
    macro_rules! input {
        ($r:expr, $($($v:ident)* : $t:tt),* $(,)?) => {
            $(let $($v)* = read!($r, $t);)*
        };
    }
}

fn run<R: std::io::BufRead, W: std::io::Write>(reader: &mut R, writer: &mut W) {
    't: for _ in 0..read!(reader, usize) {
        input! {
            reader,
            n: usize,
            mut a: [usize; 2 * n],
        }

        let x: Vec<_> = (1..=n).cycle().take(2 * n).collect();

        macro_rules! done {
            ($s:tt) => {
                writeln!(writer, "{}", $s).ok();
                continue 't;
            };
        }

        if a == x {
            done!("Yes");
        }

        for i in 1..=n {
            if let Some(l) = a.iter().position(|a| *a == i) {
                let r = a.iter().rposition(|a| *a == i).unwrap();
                a[l..=r].reverse();
                if a == x {
                    done!("Yes");
                }
                a[l..=r].reverse();
            } else {
                done!("No");
            }
        }
        done!("No");
    }
}

fn main() {
    let (stdin, stdout) = (std::io::stdin(), std::io::stdout());
    let ref mut reader = std::io::BufReader::new(stdin.lock());
    let ref mut writer = std::io::BufWriter::new(stdout.lock());
    run(reader, writer);
}
0