結果

問題 No.2740 Old Maid
ユーザー nautnaut
提出日時 2024-04-24 00:41:12
言語 Rust
(1.77.0)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 2,738 bytes
コンパイル時間 1,919 ms
コンパイル使用メモリ 173,056 KB
実行使用メモリ 24,824 KB
最終ジャッジ日時 2024-04-24 00:41:23
合計ジャッジ時間 8,850 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
21,876 KB
testcase_01 AC 135 ms
21,880 KB
testcase_02 AC 119 ms
21,868 KB
testcase_03 AC 119 ms
21,888 KB
testcase_04 AC 127 ms
21,892 KB
testcase_05 AC 120 ms
21,892 KB
testcase_06 AC 127 ms
21,876 KB
testcase_07 AC 119 ms
21,888 KB
testcase_08 AC 138 ms
21,988 KB
testcase_09 AC 121 ms
21,876 KB
testcase_10 AC 112 ms
23,928 KB
testcase_11 AC 121 ms
20,736 KB
testcase_12 AC 135 ms
18,432 KB
testcase_13 AC 146 ms
19,216 KB
testcase_14 AC 25 ms
5,448 KB
testcase_15 AC 36 ms
6,912 KB
testcase_16 AC 102 ms
14,400 KB
testcase_17 AC 25 ms
5,504 KB
testcase_18 AC 168 ms
17,880 KB
testcase_19 AC 52 ms
9,216 KB
testcase_20 AC 172 ms
21,460 KB
testcase_21 AC 30 ms
6,400 KB
testcase_22 AC 93 ms
14,208 KB
testcase_23 AC 17 ms
5,376 KB
testcase_24 AC 48 ms
8,704 KB
testcase_25 AC 177 ms
22,784 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 12 ms
5,376 KB
testcase_28 AC 98 ms
14,464 KB
testcase_29 AC 117 ms
13,696 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 199 ms
24,824 KB
testcase_32 AC 57 ms
9,728 KB
testcase_33 AC 187 ms
23,416 KB
testcase_34 AC 91 ms
13,004 KB
testcase_35 AC 47 ms
8,464 KB
testcase_36 AC 51 ms
9,216 KB
testcase_37 AC 70 ms
11,292 KB
testcase_38 AC 40 ms
8,064 KB
testcase_39 AC 23 ms
5,376 KB
testcase_40 AC 142 ms
16,448 KB
testcase_41 AC 169 ms
21,504 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 1 ms
5,376 KB
testcase_45 AC 1 ms
5,376 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 1 ms
5,376 KB
testcase_49 AC 0 ms
5,376 KB
testcase_50 AC 0 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 1 ms
5,376 KB
testcase_53 AC 1 ms
5,376 KB
testcase_54 AC 1 ms
5,376 KB
testcase_55 AC 1 ms
5,376 KB
testcase_56 AC 1 ms
5,376 KB
testcase_57 AC 1 ms
5,376 KB
testcase_58 AC 1 ms
5,376 KB
testcase_59 AC 1 ms
5,376 KB
testcase_60 AC 1 ms
5,376 KB
testcase_61 AC 1 ms
5,376 KB
testcase_62 AC 1 ms
5,376 KB
testcase_63 AC 1 ms
5,376 KB
testcase_64 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let N = input!(usize);
    let P = input!(usize, N);

    let mut pos_to_val = std::collections::BTreeMap::new();

    for i in 0..N {
        pos_to_val.insert(i, P[i]);
    }

    let mut val_to_pos = std::collections::BTreeMap::new();

    for i in 0..N {
        val_to_pos.insert(P[i], i);
    }

    let mut hq = std::collections::BinaryHeap::new();

    for i in 0..N {
        hq.push(std::cmp::Reverse(P[i]));
    }

    let mut ans = vec![];

    while !hq.is_empty() {
        let m = hq.pop().unwrap().0;

        match val_to_pos.get(&m) {
            Some(&p1) => {
                if p1 == N - 1 {
                    continue;
                }

                match pos_to_val.range(p1 + 1..).next() {
                    Some((&p2, &v2)) => {
                        ans.push(m);
                        ans.push(v2);

                        pos_to_val.remove(&p1);
                        pos_to_val.remove(&p2);
                        val_to_pos.remove(&v2);
                        val_to_pos.remove(&m);
                    }
                    None => {
                        continue;
                    }
                }
            }
            None => {
                continue;
            }
        }
    }

    writeln!(
        out,
        "{}",
        ans.iter()
            .map(|x| x.to_string())
            .collect::<Vec<_>>()
            .join(" ")
    );
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0