AtCoder Beginner Contest 329 D

https://atcoder.jp/contests/abc329/tasks/abc329_d

BTreeSetを使うとよいです。

// Election Quick Report
#![allow(non_snake_case)]

use std::collections::BTreeSet;


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}


//////////////////// process ////////////////////

fn read_input() -> (usize, Vec<usize>) {
    let v = read_vec();
    let N = v[0];
    let A: Vec<usize> = read_vec::<usize>().into_iter().
                                    map(|a| a-1).collect();
    (N, A)
}

fn F(N: usize, A: Vec<usize>) {
    let mut votes: Vec<i32> = vec![0; N];
    let mut tree: BTreeSet<(i32, i32, usize)>
                = (0..N).map(|i| (0, -(i as i32), i)).collect();
    
    for i in A.into_iter() {
        let vote: (i32, i32, usize) = (votes[i], -(i as i32), i);
        tree.remove(&vote);
        votes[i] += 1;
        tree.insert((votes[i], -(i as i32), i));
        println!("{}", tree.last().unwrap().2 + 1)
    }
}

fn main() {
    let (N, A) = read_input();
    F(N, A)
}