Skip to content

Commit

Permalink
AC atcoder/abc333/e
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Feb 8, 2024
1 parent 664a7e5 commit 21e8b5f
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -862,3 +862,7 @@ path = "atcoder/abc332/d/main.rs"
[[bin]]
name = "atcoder-abc332-d-main-v2"
path = "atcoder/abc332/d/main_v2.rs"

[[bin]]
name = "atcoder-abc333-e-main"
path = "atcoder/abc333/e/main.rs"
141 changes: 141 additions & 0 deletions atcoder/abc333/e/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// AC

// https://atcoder.jp/contests/abc333/tasks/abc333_e

fn main() {
// ~ 10^5
let n: usize = read_vec()[0];
// x \in [1, n]
let ls: Vec<[usize; 2]> = (0..n).map(|_| read_array()).collect();

// solve reverse problem to find optimal
// TODO: prove correctness

let mut required: Vec<usize> = vec![0; n + 1];
let mut history: Vec<isize> = vec![0; n];

for (i, &[t, x]) in ls.iter().enumerate().rev() {
match t {
1 => {
if required[x] > 0 {
required[x] -= 1;
history[i] = 1;
}
}
2 => {
required[x] += 1;
history[i] = -1;
}
_ => unreachable!(),
}
}

if required.iter().all(|&v| v == 0) {
let mut acc = history.clone();
for i in 1..n {
acc[i] += acc[i - 1];
}
let result = acc.iter().max().unwrap();
println!("{}", result);
for (i, v) in history.iter().filter(|&&v| v >= 0).enumerate() {
if i > 0 {
print!(" ");
}
print!("{}", v);
}
println!();
} else {
println!("-1");
}
}

/*
python misc/run.py atcoder/abc333/e/main.rs
%%%% begin
13
1 2
1 3
1 1
1 3
1 2
2 3
1 3
1 3
2 3
1 3
2 2
2 3
2 1
%%%%
3
1 1 1 0 0 1 0 1
%%%% end
%%%% begin
4
2 3
1 4
2 1
1 2
%%%%
-1
%%%% end
%%%% begin
30
1 25
1 2
1 10
1 18
2 18
1 11
2 11
1 21
1 6
2 2
2 10
1 11
1 24
1 11
1 3
1 2
1 18
2 25
1 8
1 10
1 11
2 18
2 10
1 10
2 2
1 24
1 10
2 10
1 25
2 6
%%%%
4
1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0
%%%% end
*/

//
// utils
//

#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
line.split_whitespace()
.map(|s| s.parse().ok().unwrap())
.collect()
}

#[allow(dead_code)]
fn read_array<T: std::str::FromStr, const LEN: usize>() -> [T; LEN] {
#[allow(unused_imports)]
use std::convert::TryInto;
read_vec::<T>().try_into().ok().unwrap()
}

0 comments on commit 21e8b5f

Please sign in to comment.