結果

問題 No.3780 Erase and Mex
コンテスト
ユーザー akakimidori
提出日時 2026-10-30 22:31:11
言語 Rust
(1.99.0 + proconio + num + itertools + ACL)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 14 ms / 2,000 ms
+ 506µs
コード長 2,400 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 819 ms
コンパイル使用メモリ 184,544 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-10-30 22:31:29
合計ジャッジ時間 4,616 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 43
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type alias `Map` is never used
  --> src/main.rs:80:6
   |
80 | type Map<K, V> = BTreeMap<K, V>;
   |      ^^^
   |
   = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

warning: type alias `Set` is never used
  --> src/main.rs:81:6
   |
81 | type Set<T> = BTreeSet<T>;
   |      ^^^

warning: type alias `Deque` is never used
  --> src/main.rs:82:6
   |
82 | type Deque<T> = VecDeque<T>;
   |      ^^^^^

ソースコード

diff #
raw source code

fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
    let t: u32 = sc.next();
    for _ in 0..t {
        let n: usize = sc.next();
        let a = sc.next_vec::<usize>(n);
        writeln!(out, "{}", solve(a)).ok();
    }
}

fn solve(a: Vec<usize>) -> usize {
    let n = a.len();
    if n == 1 {
        return a[0].max(1);
    }
    let mut cnt = vec![0usize; n + 2];
    for a in a {
        cnt[a.min(n + 1)] += 1;
    }
    let can = |m: usize| -> bool {
        let mut need = vec![0usize; n + 1];
        let mut free = cnt[m..].iter().sum::<usize>();
        need[m] = 1;
        let mut add = 0;
        for i in (0..m).rev() {
            add += need[i + 1];
            add = add.min(n + 1);
            need[i] += add;
            let v = need[i].min(cnt[i]);
            need[i] -= v;
            free += cnt[i] - v;
        }
        need[0] <= free
    };
    let mut ok = 1;
    let mut ng = n + 1;
    while ng - ok > 1 {
        let m = (ok + ng) / 2;
        if can(m) {
            ok = m;
        } else {
            ng = m;
        }
    }
    ok
}

// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    pub struct Scanner<'a> {
        it: std::str::SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            self.it.next().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn next_bytes(&mut self) -> Vec<u8> {
            self.it.next().unwrap().bytes().collect()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.it.next().unwrap().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
}
// ---------- end scannner ----------

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut sc = scanner::Scanner::new(&s);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    run(&mut sc, &mut out);
}
0