結果

問題 No.2740 Old Maid
ユーザー 👑 tipstar0125tipstar0125
提出日時 2024-04-20 18:05:54
言語 Rust
(1.77.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 4,355 bytes
コンパイル時間 3,185 ms
コンパイル使用メモリ 195,860 KB
実行使用メモリ 20,760 KB
最終ジャッジ日時 2024-04-20 18:06:06
合計ジャッジ時間 8,194 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
20,740 KB
testcase_01 AC 92 ms
20,636 KB
testcase_02 AC 64 ms
20,632 KB
testcase_03 AC 63 ms
20,656 KB
testcase_04 AC 65 ms
20,756 KB
testcase_05 AC 63 ms
20,676 KB
testcase_06 AC 64 ms
20,656 KB
testcase_07 AC 67 ms
20,760 KB
testcase_08 AC 62 ms
20,720 KB
testcase_09 AC 62 ms
20,636 KB
testcase_10 AC 61 ms
20,556 KB
testcase_11 AC 64 ms
20,640 KB
testcase_12 AC 72 ms
19,728 KB
testcase_13 AC 56 ms
20,040 KB
testcase_14 AC 12 ms
6,524 KB
testcase_15 AC 16 ms
6,564 KB
testcase_16 AC 36 ms
11,528 KB
testcase_17 AC 11 ms
6,508 KB
testcase_18 AC 51 ms
19,816 KB
testcase_19 AC 24 ms
10,940 KB
testcase_20 AC 58 ms
20,172 KB
testcase_21 AC 14 ms
6,600 KB
testcase_22 AC 34 ms
11,552 KB
testcase_23 AC 9 ms
5,376 KB
testcase_24 AC 22 ms
10,788 KB
testcase_25 AC 64 ms
20,304 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 6 ms
5,376 KB
testcase_28 AC 33 ms
11,492 KB
testcase_29 AC 32 ms
11,500 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 70 ms
20,656 KB
testcase_32 AC 25 ms
11,020 KB
testcase_33 AC 80 ms
20,536 KB
testcase_34 AC 30 ms
11,316 KB
testcase_35 AC 20 ms
6,764 KB
testcase_36 AC 21 ms
10,708 KB
testcase_37 AC 28 ms
11,156 KB
testcase_38 AC 17 ms
6,796 KB
testcase_39 AC 9 ms
5,376 KB
testcase_40 AC 45 ms
19,608 KB
testcase_41 AC 57 ms
20,156 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 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)]
#![allow(unused_imports)]
#![allow(unused_macros)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::comparison_chain)]
#![allow(clippy::nonminimal_bool)]
#![allow(clippy::neg_multiply)]
#![allow(dead_code)]
// use itertools::Itertools;
use std::cmp::Reverse;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, VecDeque};
// use superslice::Ext;
// use bitset_fixed::BitSet;

use proconio::{
    input,
    marker::{Chars, Usize1},
};

#[derive(Default)]
struct Solver {}
impl Solver {
    // #[fastout]
    fn solve(&mut self) {
        input! {
            N: usize,
            P: [usize; N]
        }

        let mut used = vec![false; N + 1];
        let mut ans = vec![];
        let mut linked_list = LinkedList::new(P);
        for pos in 1..=N {
            if ans.len() == N {
                break;
            }
            if used[pos] || linked_list.list[&pos].back.is_none() {
                continue;
            }
            ans.push(pos);
            used[pos] = true;
            if let Some(nxt) = linked_list.list[&pos].back {
                ans.push(nxt);
                used[nxt] = true;
                linked_list.remove(pos);
                linked_list.remove(nxt);
            }
        }
        println!("{}", join_to_string(&ans, " "));
    }
}

fn join_to_string<T: std::string::ToString>(v: &[T], sep: &str) -> String {
    v.iter()
        .fold("".to_string(), |s, x| s + x.to_string().as_str() + sep)
}

#[derive(Debug)]
struct LinkedNode {
    front: Option<usize>,
    back: Option<usize>,
}

#[derive(Debug)]
struct LinkedList {
    list: HashMap<usize, LinkedNode>,
}

impl LinkedList {
    fn new(v: Vec<usize>) -> Self {
        let mut list = HashMap::new();
        if v.len() == 1 {
            list.insert(
                v[0],
                LinkedNode {
                    front: None,
                    back: None,
                },
            );
        } else if v.len() >= 2 {
            list.insert(
                v[0],
                LinkedNode {
                    front: None,
                    back: Some(v[1]),
                },
            );
            list.insert(
                v[v.len() - 1],
                LinkedNode {
                    front: Some(v[v.len() - 2]),
                    back: None,
                },
            );
            for i in 1..v.len() - 1 {
                list.insert(
                    v[i],
                    LinkedNode {
                        front: Some(v[i - 1]),
                        back: Some(v[i + 1]),
                    },
                );
            }
        }
        LinkedList { list }
    }
    fn remove(&mut self, x: usize) {
        let front = self.list[&x].front;
        let back = self.list[&x].back;
        if let Some(front) = front {
            if let Some(v) = self.list.get_mut(&front) {
                v.back = back;
            }
        }
        if let Some(back) = back {
            if let Some(v) = self.list.get_mut(&back) {
                v.front = front;
            }
        }
        self.list.remove(&x);
    }
    // xの直後にyを挿入
    fn insert(&mut self, x: usize, y: usize) {
        self.list.insert(
            y,
            LinkedNode {
                front: Some(x),
                back: self.list[&x].back,
            },
        );
        if let Some(back) = self.list[&x].back {
            if let Some(v) = self.list.get_mut(&back) {
                v.front = Some(y);
            }
        }
        if let Some(v) = self.list.get_mut(&x) {
            v.back = Some(y);
        }
    }
    fn to_list(&self) -> Vec<usize> {
        let mut ret = vec![];
        let mut now = {
            let mut head = 0;
            for (k, v) in self.list.iter() {
                if v.front.is_none() {
                    head = *k;
                    break;
                }
            }
            head
        };
        loop {
            ret.push(now);
            if let Some(x) = self.list[&now].back {
                now = x;
            } else {
                break;
            }
        }
        ret
    }
}

fn main() {
    std::thread::Builder::new()
        .stack_size(128 * 1024 * 1024)
        .spawn(|| Solver::default().solve())
        .unwrap()
        .join()
        .unwrap();
}
0