結果

問題 No.2740 Old Maid
ユーザー Yukino DX.Yukino DX.
提出日時 2024-04-27 15:49:02
言語 Rust
(1.77.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,949 bytes
コンパイル時間 16,532 ms
コンパイル使用メモリ 378,500 KB
実行使用メモリ 15,812 KB
最終ジャッジ日時 2024-04-27 15:49:26
合計ジャッジ時間 23,687 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
15,688 KB
testcase_01 AC 44 ms
15,688 KB
testcase_02 AC 44 ms
15,688 KB
testcase_03 AC 52 ms
15,812 KB
testcase_04 AC 43 ms
15,812 KB
testcase_05 AC 44 ms
15,688 KB
testcase_06 AC 44 ms
15,812 KB
testcase_07 AC 46 ms
15,628 KB
testcase_08 AC 45 ms
15,812 KB
testcase_09 AC 44 ms
15,812 KB
testcase_10 AC 41 ms
15,688 KB
testcase_11 AC 42 ms
15,688 KB
testcase_12 AC 41 ms
11,700 KB
testcase_13 AC 45 ms
12,488 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 13 ms
5,376 KB
testcase_16 AC 30 ms
9,196 KB
testcase_17 AC 8 ms
5,376 KB
testcase_18 AC 43 ms
12,008 KB
testcase_19 AC 22 ms
5,888 KB
testcase_20 AC 48 ms
13,708 KB
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 29 ms
9,124 KB
testcase_23 AC 7 ms
5,376 KB
testcase_24 AC 16 ms
5,660 KB
testcase_25 AC 53 ms
14,144 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 30 ms
9,140 KB
testcase_29 AC 27 ms
8,840 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 58 ms
15,456 KB
testcase_32 AC 19 ms
6,876 KB
testcase_33 AC 56 ms
14,820 KB
testcase_34 AC 27 ms
8,756 KB
testcase_35 AC 14 ms
5,632 KB
testcase_36 AC 16 ms
5,760 KB
testcase_37 AC 23 ms
7,812 KB
testcase_38 AC 14 ms
5,520 KB
testcase_39 AC 8 ms
5,376 KB
testcase_40 AC 36 ms
10,372 KB
testcase_41 AC 51 ms
13,508 KB
testcase_42 AC 0 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 1 ms
5,376 KB
testcase_45 AC 0 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 1 ms
5,376 KB
testcase_50 AC 1 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 0 ms
5,376 KB
testcase_59 AC 0 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 0 ms
5,376 KB
testcase_64 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
//use itertools::{iproduct, Itertools};
use proconio::input;
use proconio::marker::*;
use std::collections::*;

fn main() {
    input! {
        n:usize,
        p:[Usize1;n],
    }

    let mut nexts = vec![None; n];
    let mut prevs = vec![None; n];
    for i in 0..n {
        if i != n - 1 {
            nexts[p[i]] = Some(p[i + 1]);
        }
        if i != 0 {
            prevs[p[i]] = Some(p[i - 1]);
        }
    }

    let mut rem = (0..n).collect::<BTreeSet<_>>();
    let mut ans = vec![];
    while !rem.is_empty() {
        let fst = *rem.first().unwrap();
        if let Some(nxt) = nexts[fst] {
            ans.push(fst);
            ans.push(nxt);
            match (prevs[fst], nexts[nxt]) {
                (Some(pre), Some(nnxt)) => {
                    nexts[pre] = Some(nnxt);
                    prevs[nnxt] = Some(pre);
                }
                (Some(pre), None) => {
                    nexts[pre] = None;
                }
                (None, Some(nnxt)) => {
                    prevs[nnxt] = None;
                }
                _ => (),
            }

            rem.remove(&fst);
            rem.remove(&nxt);
            continue;
        }

        let scn = *rem.iter().nth(1).unwrap();
        if let Some(nxt) = nexts[scn] {
            ans.push(scn);
            ans.push(nxt);

            match (prevs[scn], nexts[nxt]) {
                (Some(pre), Some(nnxt)) => {
                    nexts[pre] = Some(nnxt);
                    prevs[nnxt] = Some(pre);
                }
                (Some(pre), None) => {
                    nexts[pre] = None;
                }
                (None, Some(nnxt)) => {
                    prevs[nnxt] = None;
                }
                _ => (),
            }

            rem.remove(&scn);
            rem.remove(&nxt);
        }
    }

    for i in 0..n {
        print!("{} ", ans[i] + 1);
    }
    println!();
}
0