#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 1;
int n, m, c[N], mn[N];
void solve () {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 1; i <= m; i++) {
mn[i] = 1e9;
}
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 = 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;
}
}
if (c[n] != i) {
ff[c[n]]++;
s++;
}
int cnt = 0;
for (int j = 1; j <= m; j++) {
cnt += c[j] == i;
}
mn[i] = min(mn[i], v + s - *max_element(ff.begin(), ff.end()));
}
}
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++;
}
int cnt = 0;
for (int j = 1; j <= m; j++) {
cnt += c[j] == i;
}
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++;
}
int cnt = 0;
for (int j = 1; j <= m; j++) {
cnt += c[j] == i;
}
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;
}
}
int cnt = 0;
for (int j = 1; j <= m; j++) {
cnt += c[j] == 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 () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
# | 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... |