From 4e8ac826929117f95baeb37e1518773d1169d900 Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Wed, 28 Feb 2024 18:00:30 +0100 Subject: Random uncommited stuff --- kattis-open/pickupsticks/Cargo.lock | 7 +++++ kattis-open/pickupsticks/Cargo.toml | 8 +++++ kattis-open/pickupsticks/src/main.rs | 61 ++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 kattis-open/pickupsticks/Cargo.lock create mode 100644 kattis-open/pickupsticks/Cargo.toml create mode 100644 kattis-open/pickupsticks/src/main.rs (limited to 'kattis-open/pickupsticks') diff --git a/kattis-open/pickupsticks/Cargo.lock b/kattis-open/pickupsticks/Cargo.lock new file mode 100644 index 0000000..4be0001 --- /dev/null +++ b/kattis-open/pickupsticks/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "challenge" +version = "0.1.0" diff --git a/kattis-open/pickupsticks/Cargo.toml b/kattis-open/pickupsticks/Cargo.toml new file mode 100644 index 0000000..13df4d9 --- /dev/null +++ b/kattis-open/pickupsticks/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "challenge" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/kattis-open/pickupsticks/src/main.rs b/kattis-open/pickupsticks/src/main.rs new file mode 100644 index 0000000..e264092 --- /dev/null +++ b/kattis-open/pickupsticks/src/main.rs @@ -0,0 +1,61 @@ +use std::{ + collections::{BTreeSet, HashMap}, + io::Read, + iter::repeat, +}; + +fn main() { + let mut input = String::new(); + std::io::stdin().read_to_string(&mut input).unwrap(); + + let mut lines = input.lines(); + let mut nm = lines.next().unwrap().split_ascii_whitespace(); + let n: usize = nm.next().unwrap().parse().unwrap(); + let m: usize = nm.next().unwrap().parse().unwrap(); + + let mut covers = HashMap::>::with_capacity(m); + let mut n_above = vec![0usize; n]; + let mut sticks_at: Vec> = repeat(BTreeSet::new()).take(m).collect(); + + for i in 0..n { + sticks_at[0].insert(i); + } + + for mut line in lines.map(str::split_ascii_whitespace) { + let a = line.next().unwrap().parse::().unwrap() - 1; + let b = line.next().unwrap().parse::().unwrap() - 1; + + covers.entry(a).or_default().push(b); + sticks_at[n_above[b]].remove(&b); + n_above[b] += 1; + sticks_at[n_above[b]].insert(b); + } + + let mut picks = Vec::with_capacity(n); + loop { + let stick = if let Some(&s) = sticks_at[0].iter().next() { + s + } else { + break; + }; + sticks_at[0].remove(&stick); + picks.push(stick); + + if let Some(c) = covers.get(&stick) { + for &b in c { + sticks_at[n_above[b]].remove(&b); + n_above[b] -= 1; + sticks_at[n_above[b]].insert(b); + } + } + } + + if picks.len() != n { + println!("IMPOSSIBLE"); + return; + } + + for p in picks { + println!("{}", p + 1); + } +} -- cgit v1.2.3