結果

問題 No.2740 Old Maid
ユーザー hayatroidhayatroid
提出日時 2024-04-20 17:21:26
言語 Rust
(1.77.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 2,830 bytes
コンパイル時間 1,344 ms
コンパイル使用メモリ 160,256 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-04-20 17:21:32
合計ジャッジ時間 5,353 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
8,192 KB
testcase_01 AC 40 ms
8,192 KB
testcase_02 AC 41 ms
8,320 KB
testcase_03 AC 40 ms
8,320 KB
testcase_04 AC 40 ms
8,320 KB
testcase_05 AC 41 ms
8,320 KB
testcase_06 AC 40 ms
8,320 KB
testcase_07 AC 40 ms
8,320 KB
testcase_08 AC 40 ms
8,320 KB
testcase_09 AC 40 ms
8,320 KB
testcase_10 AC 40 ms
8,320 KB
testcase_11 AC 40 ms
8,320 KB
testcase_12 AC 30 ms
6,400 KB
testcase_13 AC 33 ms
6,912 KB
testcase_14 AC 8 ms
5,376 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 24 ms
5,504 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 32 ms
6,656 KB
testcase_19 AC 13 ms
5,376 KB
testcase_20 AC 38 ms
7,168 KB
testcase_21 AC 9 ms
5,376 KB
testcase_22 AC 23 ms
5,376 KB
testcase_23 AC 6 ms
5,376 KB
testcase_24 AC 13 ms
5,376 KB
testcase_25 AC 42 ms
7,680 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 4 ms
5,376 KB
testcase_28 AC 23 ms
5,376 KB
testcase_29 AC 22 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 45 ms
8,192 KB
testcase_32 AC 15 ms
5,376 KB
testcase_33 AC 43 ms
7,936 KB
testcase_34 AC 22 ms
5,376 KB
testcase_35 AC 12 ms
5,376 KB
testcase_36 AC 13 ms
5,376 KB
testcase_37 AC 18 ms
5,376 KB
testcase_38 AC 11 ms
5,376 KB
testcase_39 AC 6 ms
5,376 KB
testcase_40 AC 29 ms
6,016 KB
testcase_41 AC 38 ms
7,296 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 0 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 1 ms
5,376 KB
testcase_58 AC 0 ms
5,376 KB
testcase_59 AC 1 ms
5,376 KB
testcase_60 AC 0 ms
5,376 KB
testcase_61 AC 1 ms
5,376 KB
testcase_62 AC 0 ms
5,376 KB
testcase_63 AC 1 ms
5,376 KB
testcase_64 AC 0 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, Chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, Usize1) => {
        read_value!($next, usize) - 1
    };
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        n: usize,
        p: [Usize1; n],
    }
    let mut idx = vec![0; n];
    for (i, &p) in p.iter().enumerate() {
        idx[p] = i;
    }
    let mut v = (0..n).collect::<Vec<_>>();
    let mut used = vec![false; n];
    let mut ans = vec![];
    for x in 0..n {
        let i = idx[x];
        if i != v[i] || i == n - 1 {
            continue;
        }
        let j = if i + 1 != tail(v[i + 1], &mut v) {
            tail(v[i + 1], &mut v) + 1
        } else {
            i + 1
        };
        if j == n {
            continue;
        }
        if used[i] || used[j] {
            continue;
        }
        if i > 0 && used[i - 1] {
            union(i, i - 1, &mut v);
        }
        if j < n - 1 && used[j + 1] {
            union(j, j + 1, &mut v);
        }
        union(i, i + 1, &mut v);
        union(j - 1, j, &mut v);
        used[i] = true;
        used[j] = true;
        ans.push(p[i]);
        ans.push(p[j]);
    }
    for (i, ans) in ans.iter().enumerate() {
        if i > 0 {
            print!(" ");
        }
        print!("{}", ans + 1);
    }
    println!();
}

fn tail(x: usize, v: &mut Vec<usize>) -> usize {
    if x == v[x] {
        x
    } else {
        v[x] = tail(v[x], v);
        v[x]
    }
}

fn union(x: usize, y: usize, v: &mut Vec<usize>) {
    let tx = tail(x, v);
    let ty = tail(y, v);
    if tx < ty {
        v[tx] = ty;
    } else {
        v[ty] = tx;
    }
}
0