#include <bits/stdc++.h>
using namespace std;
struct tree {
vector<int> t;
vector<int> a;
int n;
void turnon(int u) {
u += n;
t[u] = a[u - n];
for (; u != 1; u >>= 1) {
t[u >> 1] = max(t[u], t[u ^ 1]);
}
}
void turnoff(int u) {
u += n;
t[u] = 0;
for (; u != 1; u >>= 1) {
t[u >> 1] = max(t[u], t[u ^ 1]);
}
}
int get_max() {
return t[1];
}
tree(const vector<int> &a_) {
a = a_;
n = 1;
while (n < (int) a.size()) {
n *= 2;
}
t.resize(2 * n, 0);
a.resize(n, 0);
for (int i = 0; i < n; ++i) {
t[i + n] = a[i];
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> c(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
--c[i];
}
vector<int> cnt(m, 0);
for (int i = 0; i < n; ++i) {
++cnt[c[i]];
}
vector<int> ans(m);
for (int u = 0; u < m; ++u) {
ans[u] = n - cnt[u];
}
tree t(cnt);
for (int p = 0; p < 2; ++p) {
vector<vector<int>> g(m);
for (int i = p; i + 1 < n; i += 2) {
if (c[i] != c[i + 1]) {
g[c[i]].push_back(c[i + 1]);
g[c[i + 1]].push_back(c[i]);
}
}
for (int u = 0; u < m; ++u) {
sort(begin(g[u]), end(g[u]));
vector<int> tmp;
int len = 0;
for (int i = 0; i < (int) g[u].size(); ++i) {
int v = g[u][i];
++len;
if (i == (int) g[u].size() - 1 || g[u][i + 1] != v) {
ans[u] = min(ans[u], n - cnt[u] - cnt[v] + len);
len = 0;
tmp.push_back(v);
}
}
g[u] = tmp;
for (int v : g[u]) {
t.turnoff(v);
}
t.turnoff(u);
ans[u] = min(ans[u], n - cnt[u] - t.get_max());
t.turnon(u);
for (int v : g[u]) {
t.turnon(v);
}
}
}
for (int u = 0; u < m; ++u) {
cout << ans[u] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |