This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
using ii = pair<int, int>;
int n, m;
vector<int> c, cnt;
vector< vector<int> > vec[2];
multiset<int> rem;
void upd (int col, int val) {
rem.erase(rem.find(cnt[col]) );
cnt[col] += val;
rem.insert(cnt[col]);
}
int solve (int col, bool _) {
int cnt_rem = n - cnt[col];
for (int _col : vec[_][col]) upd (_col, -1), --cnt_rem;
rem.erase(rem.find(cnt[col]) );
if (rem.size() ) cnt_rem -= *rem.rbegin();
rem.insert(cnt[col]);
for (int _col : vec[_][col]) upd (_col, 1);
return cnt_rem + (int)vec[_][col].size();
}
int main () {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
cnt.assign(m, 0);
c.assign(n, 0);
for (int i = 0; i < n; ++i) cin >> c[i], --c[i], ++cnt[ c[i] ];
for (int _ = 0; _ < 2; ++_) {
vec[_].assign(m, vector<int>() );
vector<int> sz_vec(m, 0);
for (int i = _; i + 1 < n; i += 2) {
if (c[i] != c[i + 1]) {
++sz_vec[ c[i] ];
++sz_vec[ c[i + 1] ];
}
}
for (int i = 0; i < m; ++i) vec[_][i].assign(sz_vec[i], 0);
sz_vec.assign(m, 0);
for (int i = _; i + 1 < n; i += 2) {
if (c[i] != c[i + 1]) {
vec[_][ c[i] ][ sz_vec[ c[i] ]++ ] = c[i + 1];
vec[_][ c[i + 1] ][ sz_vec[ c[i + 1] ]++ ] = c[i];
}
}
}
for (int col = 0; col < m; ++col) rem.insert(cnt[col]);
for (int col = 0; col < m; ++col) cout << min(solve(col, 0), solve(col, 1) ) << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |