結果

問題 No.2740 Old Maid
ユーザー t33ft33f
提出日時 2024-04-21 11:53:09
言語 Rust
(1.77.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,361 bytes
コンパイル時間 1,685 ms
コンパイル使用メモリ 173,568 KB
実行使用メモリ 14,196 KB
最終ジャッジ日時 2024-04-21 11:53:23
合計ジャッジ時間 6,847 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
14,068 KB
testcase_01 AC 48 ms
14,068 KB
testcase_02 AC 47 ms
14,064 KB
testcase_03 AC 47 ms
14,084 KB
testcase_04 AC 46 ms
14,064 KB
testcase_05 AC 48 ms
14,068 KB
testcase_06 AC 49 ms
14,196 KB
testcase_07 AC 46 ms
14,072 KB
testcase_08 AC 46 ms
14,068 KB
testcase_09 AC 46 ms
14,144 KB
testcase_10 AC 43 ms
14,196 KB
testcase_11 AC 44 ms
14,072 KB
testcase_12 AC 43 ms
10,756 KB
testcase_13 AC 52 ms
11,264 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 12 ms
5,376 KB
testcase_16 AC 30 ms
8,336 KB
testcase_17 AC 9 ms
5,376 KB
testcase_18 AC 44 ms
10,940 KB
testcase_19 AC 18 ms
5,376 KB
testcase_20 AC 51 ms
12,248 KB
testcase_21 AC 10 ms
5,376 KB
testcase_22 AC 32 ms
8,216 KB
testcase_23 AC 7 ms
5,376 KB
testcase_24 AC 16 ms
5,376 KB
testcase_25 AC 53 ms
12,804 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 31 ms
8,272 KB
testcase_29 AC 29 ms
7,996 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 62 ms
13,808 KB
testcase_32 AC 19 ms
6,164 KB
testcase_33 AC 55 ms
13,304 KB
testcase_34 AC 28 ms
8,072 KB
testcase_35 AC 18 ms
5,376 KB
testcase_36 AC 18 ms
5,404 KB
testcase_37 AC 22 ms
7,012 KB
testcase_38 AC 15 ms
5,376 KB
testcase_39 AC 9 ms
5,376 KB
testcase_40 AC 38 ms
9,448 KB
testcase_41 AC 50 ms
12,176 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 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 0 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)]
use std::collections::BTreeSet;
use std::iter::once;
use proconio::{input};

fn main() {
    input!{ n: usize, p: [usize; n] }
    let mut index = vec![0; n];
    for i in 0..n { index[p[i] - 1] = i; }
    let mut unused: BTreeSet<_> = (0..n).collect();
    let mut next = (1..n).chain(once(usize::MAX)).collect::<Vec<_>>();
    let mut prev = once(usize::MAX).chain(0..n-1).collect::<Vec<_>>();
    let mut ans = Vec::new();
    while !unused.is_empty() {
        let target_index = {
            let mut iter = unused.iter();
            let min = iter.next().unwrap();
            let min_index = index[*min];
            if next[min_index] < n {
                min_index
            } else {
                let second_min = iter.next().unwrap();
                index[*second_min]
            }
        };
        let target1 = p[target_index] - 1;
        let target2 = p[next[target_index]] - 1;
        ans.push(target1 + 1);
        ans.push(target2 + 1);
        unused.remove(&target1);
        unused.remove(&target2);
        let prev_index = prev[target_index];
        let next_index = next[next[target_index]];
        if prev_index < n {
            next[prev_index] = next_index;
        }
        if next_index < n {
            prev[next_index] = prev_index;
        }
    }
    for x in ans { print!("{x} "); }
}
0