#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 1;
int n, m, c[N], mn[N];
vector <int> occ[N];
void solve () {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> c[i];
occ[c[i]].push_back(i);
}
for (int i = 1; i <= m; i++) {
mn[i] = 1e9;
}
if (n % 2 == 1) {
vector <int> ff(m + 1, 0);
for (int i = 1; i <= n; i++) {
ff[c[i]]++;
}
set <pair <int, int>> mxs;
for (int i = 1; i <= m; i++) {
mxs.insert({ff[i], i});
}
for (int i = 1; i <= m; i++) {
mxs.erase({ff[i], i});
int s = n, v = 0;
for (auto j : occ[i]) {
if (j % 2 == 1) {
if (j + 1 <= n) {
if (c[j + 1] != i) {
v++;
mxs.erase({ff[c[j + 1]], c[j + 1]});
ff[c[j + 1]]--;
mxs.insert({ff[c[j + 1]], c[j + 1]});
}
}
if (j == n) {
s--;
} else {
s -= 2;
}
} else {
if (c[j - 1] != i) {
v++;
mxs.erase({ff[c[j - 1]], c[j - 1]});
ff[c[j - 1]]--;
mxs.insert({ff[c[j - 1]], c[j - 1]});
s -= 2;
}
}
}
mn[i] = min(mn[i], v + s - (mxs.empty() ? 0 : (*(--mxs.end())).first));
for (auto j : occ[i]) {
if (j % 2 == 1) {
if (j + 1 <= n) {
if (c[j + 1] != i) {
mxs.erase({ff[c[j + 1]], c[j + 1]});
ff[c[j + 1]]++;
mxs.insert({ff[c[j + 1]], c[j + 1]});
}
}
} else {
if (c[j - 1] != i) {
mxs.erase({ff[c[j - 1]], c[j - 1]});
ff[c[j - 1]]++;
mxs.insert({ff[c[j - 1]], c[j - 1]});
}
}
}
mxs.insert({ff[i], i});
}
}
if (n % 2 == 1) {
for (int i = 1; i <= m; i++) {
vector <int> ff(m + 1, 0);
int s = 0, v = 0;
for (int j = 2; j <= n; j += 2) {
if (c[j] != i && c[j + 1] != i) {
ff[c[j]]++;
ff[c[j + 1]]++;
s += 2;
} else {
v += c[j] != i;
v += c[j + 1] != i;
}
}
if (c[1] != i) {
ff[c[1]]++;
s++;
}
mn[i] = min(mn[i], v + s - *max_element(ff.begin(), ff.end()));
}
}
if (n % 2 == 0) {
for (int i = 1; i <= m; i++) {
vector <int> ff(m + 1, 0);
int s = 0, v = 0;
for (int j = 2; j < n; j += 2) {
if (c[j] != i && c[j + 1] != i) {
ff[c[j]]++;
ff[c[j + 1]]++;
s += 2;
} else {
v += c[j] != i;
v += c[j + 1] != i;
}
}
if (c[1] != i) {
ff[c[1]]++;
s++;
}
if (c[n] != i) {
ff[c[n]]++;
s++;
}
mn[i] = min(mn[i], v + s - *max_element(ff.begin(), ff.end()));
}
}
if (n % 2 == 0) {
for (int i = 1; i <= m; i++) {
vector <int> ff(m + 1, 0);
int s = 0, v = 0;
for (int j = 1; j <= n; j += 2) {
if (c[j] != i && c[j + 1] != i) {
ff[c[j]]++;
ff[c[j + 1]]++;
s += 2;
} else {
v += c[j] != i;
v += c[j + 1] != i;
}
}
mn[i] = min(mn[i], v + s - *max_element(ff.begin(), ff.end()));
}
}
for (int i = 1; i <= m; i++) {
cout << mn[i] << '\n';
}
}
signed main () {
#ifndef ONLINE_JUDGE
freopen("input_file", "r", stdin);
freopen("output_file", "w", stdout);
#endif
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
Compilation message (stderr)
rope.cpp: In function 'int main()':
rope.cpp:143:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | freopen("input_file", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
rope.cpp:144:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
144 | freopen("output_file", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |