|
| 1 | +use std::{ |
| 2 | + cmp::max, |
| 3 | + io::{self}, |
| 4 | +}; |
| 5 | + |
| 6 | +pub struct Reader { |
| 7 | + buffer: Vec<String>, |
| 8 | +} |
| 9 | + |
| 10 | +impl Reader { |
| 11 | + pub fn new() -> Self { |
| 12 | + Self { buffer: Vec::new() } |
| 13 | + } |
| 14 | + pub fn next<T: std::str::FromStr>(&mut self) -> T { |
| 15 | + loop { |
| 16 | + if let Some(token) = self.buffer.pop() { |
| 17 | + return token.parse().ok().expect("Failed to parse token"); |
| 18 | + } |
| 19 | + let mut input = String::new(); |
| 20 | + io::stdin() |
| 21 | + .read_line(&mut input) |
| 22 | + .expect("Failed to read line"); |
| 23 | + self.buffer = input.split_whitespace().rev().map(String::from).collect(); |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +struct Counter { |
| 29 | + cnt: Vec<usize>, |
| 30 | + buf: Vec<Vec<usize>>, |
| 31 | +} |
| 32 | +impl Counter { |
| 33 | + pub fn new(n: usize) -> Self { |
| 34 | + let mut res = Self { |
| 35 | + cnt: vec![0; n], |
| 36 | + buf: vec![Vec::new()], |
| 37 | + }; |
| 38 | + for i in 0..n { |
| 39 | + res.buf[0].push(i); |
| 40 | + } |
| 41 | + res |
| 42 | + } |
| 43 | + pub fn add(&mut self, x: usize) { |
| 44 | + self.cnt[x] += 1; |
| 45 | + if self.buf.len() <= self.cnt[x] { |
| 46 | + self.buf.push(Vec::new()); |
| 47 | + } |
| 48 | + self.buf[self.cnt[x]].push(x); |
| 49 | + } |
| 50 | + pub fn del(&mut self, x: usize) { |
| 51 | + self.cnt[x] -= 1; |
| 52 | + self.buf[self.cnt[x]].push(x); |
| 53 | + } |
| 54 | + pub fn max_occurence(&mut self) -> usize { |
| 55 | + loop { |
| 56 | + let buf_len = self.buf.len(); |
| 57 | + let last_buf = self.buf.last_mut().unwrap(); |
| 58 | + while !last_buf.is_empty() && self.cnt[*last_buf.last().unwrap()] != buf_len - 1 { |
| 59 | + last_buf.pop(); |
| 60 | + } |
| 61 | + if last_buf.is_empty() { |
| 62 | + self.buf.pop(); |
| 63 | + } else { |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + self.buf.len() - 1 |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fn main() { |
| 72 | + let mut reader = Reader::new(); |
| 73 | + |
| 74 | + let n = reader.next(); |
| 75 | + let m = reader.next(); |
| 76 | + let mut c = vec![0_usize; n]; |
| 77 | + for i in c.iter_mut() { |
| 78 | + *i = reader.next::<usize>() - 1; |
| 79 | + } |
| 80 | + |
| 81 | + let mut counter = Counter::new(m); |
| 82 | + for color in c.iter() { |
| 83 | + counter.add(*color); |
| 84 | + } |
| 85 | + |
| 86 | + let mut ans = vec![0_usize; m]; |
| 87 | + for b in 0..2 { |
| 88 | + let mut pairs_of_color = vec![vec![]; m]; |
| 89 | + |
| 90 | + let mut i = b + 1; |
| 91 | + while i < n { |
| 92 | + let pair = [c[i - 1], c[i]]; |
| 93 | + pairs_of_color[pair[0]].push(pair); |
| 94 | + if pair[1] != pair[0] { |
| 95 | + pairs_of_color[pair[1]].push(pair); |
| 96 | + } |
| 97 | + i += 2; |
| 98 | + } |
| 99 | + |
| 100 | + for color in 0..m { |
| 101 | + let pairs = &pairs_of_color[color]; |
| 102 | + let mut res = 0; |
| 103 | + if b == 1 && c[0] == color { |
| 104 | + res += 1; |
| 105 | + counter.del(color); |
| 106 | + } |
| 107 | + if n % 2 != b && c[n - 1] == color { |
| 108 | + res += 1; |
| 109 | + counter.del(color); |
| 110 | + } |
| 111 | + for pair in pairs { |
| 112 | + pair.iter().for_each(|&x| { |
| 113 | + if x == color { |
| 114 | + res += 1; |
| 115 | + } |
| 116 | + counter.del(x); |
| 117 | + }); |
| 118 | + } |
| 119 | + ans[color] = max(ans[color], res + counter.max_occurence()); |
| 120 | + for pair in pairs { |
| 121 | + pair.iter().for_each(|&x| { |
| 122 | + if x == color { |
| 123 | + res -= 1; |
| 124 | + } |
| 125 | + counter.add(x); |
| 126 | + }); |
| 127 | + } |
| 128 | + for _ in 0..res { |
| 129 | + counter.add(color); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + for color in 0..m { |
| 135 | + println!("{}", n - ans[color]); |
| 136 | + } |
| 137 | +} |
0 commit comments