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;
using ll = long long;
const int N = 1000000, M = 10000000;
int a[N], ans[M], cnt[M];
vector<int> g[N];
priority_queue<pair<int, int>> pq;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
memset(ans, 0x3f, sizeof(ans));
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
--a[i];
++cnt[a[i]];
}
for (int i = 0; i < m; ++i) {
pq.push({cnt[i], i});
}
for (int k = 0; k < 2; ++k) {
for (int i = 0; i < m; ++i) {
g[i].clear();
}
for (int i = k; i + 1 < n; i += 2) {
if (a[i] != a[i + 1]) {
g[a[i]].push_back(a[i + 1]);
g[a[i + 1]].push_back(a[i]);
}
}
for (int c = 0; c < m; ++c) {
int now = cnt[c];
auto upd = [](int v, int nw) {
pq.push({cnt[v] = nw, v});
};
upd(c, 0);
for (int to : g[c]) {
upd(to, cnt[to] - 1);
}
while (cnt[pq.top().second] != pq.top().first) {
pq.pop();
}
ans[c] = min(ans[c], n - now - pq.top().first);
upd(c, now);
for (int to : g[c]) {
upd(to, cnt[to] + 1);
}
}
}
for (int i = 0; i < m; ++i) {
cout << ans[i] << "\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... |