#include<bits/stdc++.h>
#define st first
#define nd second
using namespace std;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m;
cin>>n>>m;
vector<int> a(n), occ(m+1);
vector<vector<pair<int, int>>> col(m+1);
vector<pair<int, int>> best;
for(int i=0; i<n; ++i) {
cin>>a[i];
occ[a[i]]++;
if(i==0 || a[i] != a[i-1]) {
col[a[i]].push_back({i, i});
}
else {
col[a[i]].back().nd = i;
}
}
for(int i=1; i<=m; ++i) {
best.push_back({occ[i], i});
}
sort(best.rbegin(), best.rend());
vector<vector<int>> cnt(2, vector<int>(m+1));
for(int c=1; c<=m; ++c) {
if(col[c].empty()) continue;
vector<int> prv(2);
vector<bool> fail(2);
int beg = col[c][0].st;
int end = col[c][0].nd;
if(end==n-1) {
int ans = n;
for(int i=0; i<min(m, 2); ++i) {
if(c==best[i].nd) ans=min(ans, n-occ[c]);
else ans = min(ans, n-occ[c]-occ[best[i].nd]);
}
cout<<ans<<'\n';
continue;
}
if((end-beg+1)%2==1) {
if(beg==0) {
prv[0] = end;
cnt[1][a[end+1]] = 1;
prv[1] = end+1;
}
else {
cnt[0][a[beg-1]] = 1;
prv[0] = end;
cnt[1][a[end+1]] = 1;
prv[1] = end+1;
}
}
else {
if(beg==0) {
prv[0] = end;
cnt[1][a[end+1]] = 1;
prv[1] = end+1;
}
else {
prv[0] = end;
cnt[1][a[beg-1]] = 1;
cnt[1][a[end+1]] = 1;
prv[1] = end+1;
}
}
for(int it=0; it<=1; ++it) {
bool start=0;
for(auto cur: col[c]) {
if(!start) {
start=1;
continue;
}
beg = cur.st;
end = cur.nd;
if((beg-prv[it])%2==0) {
beg--;
cnt[it][a[beg]]++;
}
if((end-beg+1)%2==1 && end != n-1) {
end++;
cnt[it][a[end]]++;
}
prv[it] = end;
}
}
int ans = n;
for(int j=0; j<min(m, (int)col[c].size()+1); ++j) {
int i = best[j].nd;
if(c==i) {
ans = min(ans, n-occ[c]);
}
else {
if(!fail[0]) ans = min(ans, n - occ[c] - occ[i] + cnt[0][i]);
if(!fail[1]) ans = min(ans, n - occ[c] - occ[i] + cnt[1][i]);
}
}
cout<<ans<<'\n';
for(auto i: col[c]) {
if(i.st != 0) cnt[0][a[i.st-1]]=cnt[1][a[i.st-1]]=0;
if(i.nd != n-1) cnt[0][a[i.nd+1]]=cnt[1][a[i.nd+1]]=0;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
14 ms |
1792 KB |
Output is correct |
28 |
Correct |
11 ms |
1656 KB |
Output is correct |
29 |
Correct |
11 ms |
1792 KB |
Output is correct |
30 |
Correct |
24 ms |
1520 KB |
Output is correct |
31 |
Correct |
16 ms |
1984 KB |
Output is correct |
32 |
Correct |
11 ms |
1532 KB |
Output is correct |
33 |
Correct |
13 ms |
1792 KB |
Output is correct |
34 |
Correct |
11 ms |
1632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
14 ms |
1792 KB |
Output is correct |
28 |
Correct |
11 ms |
1656 KB |
Output is correct |
29 |
Correct |
11 ms |
1792 KB |
Output is correct |
30 |
Correct |
24 ms |
1520 KB |
Output is correct |
31 |
Correct |
16 ms |
1984 KB |
Output is correct |
32 |
Correct |
11 ms |
1532 KB |
Output is correct |
33 |
Correct |
13 ms |
1792 KB |
Output is correct |
34 |
Correct |
11 ms |
1632 KB |
Output is correct |
35 |
Correct |
17 ms |
1792 KB |
Output is correct |
36 |
Correct |
16 ms |
1792 KB |
Output is correct |
37 |
Correct |
24 ms |
1792 KB |
Output is correct |
38 |
Correct |
15 ms |
1792 KB |
Output is correct |
39 |
Correct |
15 ms |
1920 KB |
Output is correct |
40 |
Correct |
17 ms |
2048 KB |
Output is correct |
41 |
Correct |
15 ms |
2048 KB |
Output is correct |
42 |
Correct |
16 ms |
1920 KB |
Output is correct |
43 |
Correct |
15 ms |
2048 KB |
Output is correct |
44 |
Correct |
17 ms |
2048 KB |
Output is correct |
45 |
Correct |
16 ms |
2048 KB |
Output is correct |
46 |
Correct |
15 ms |
1920 KB |
Output is correct |
47 |
Correct |
16 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
14 ms |
1792 KB |
Output is correct |
28 |
Correct |
11 ms |
1656 KB |
Output is correct |
29 |
Correct |
11 ms |
1792 KB |
Output is correct |
30 |
Correct |
24 ms |
1520 KB |
Output is correct |
31 |
Correct |
16 ms |
1984 KB |
Output is correct |
32 |
Correct |
11 ms |
1532 KB |
Output is correct |
33 |
Correct |
13 ms |
1792 KB |
Output is correct |
34 |
Correct |
11 ms |
1632 KB |
Output is correct |
35 |
Correct |
17 ms |
1792 KB |
Output is correct |
36 |
Correct |
16 ms |
1792 KB |
Output is correct |
37 |
Correct |
24 ms |
1792 KB |
Output is correct |
38 |
Correct |
15 ms |
1792 KB |
Output is correct |
39 |
Correct |
15 ms |
1920 KB |
Output is correct |
40 |
Correct |
17 ms |
2048 KB |
Output is correct |
41 |
Correct |
15 ms |
2048 KB |
Output is correct |
42 |
Correct |
16 ms |
1920 KB |
Output is correct |
43 |
Correct |
15 ms |
2048 KB |
Output is correct |
44 |
Correct |
17 ms |
2048 KB |
Output is correct |
45 |
Correct |
16 ms |
2048 KB |
Output is correct |
46 |
Correct |
15 ms |
1920 KB |
Output is correct |
47 |
Correct |
16 ms |
2048 KB |
Output is correct |
48 |
Correct |
226 ms |
17656 KB |
Output is correct |
49 |
Correct |
224 ms |
17528 KB |
Output is correct |
50 |
Correct |
224 ms |
17528 KB |
Output is correct |
51 |
Correct |
202 ms |
17400 KB |
Output is correct |
52 |
Correct |
199 ms |
14840 KB |
Output is correct |
53 |
Correct |
156 ms |
16120 KB |
Output is correct |
54 |
Correct |
139 ms |
13944 KB |
Output is correct |
55 |
Correct |
145 ms |
13688 KB |
Output is correct |
56 |
Correct |
145 ms |
13048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
14 ms |
1792 KB |
Output is correct |
28 |
Correct |
11 ms |
1656 KB |
Output is correct |
29 |
Correct |
11 ms |
1792 KB |
Output is correct |
30 |
Correct |
24 ms |
1520 KB |
Output is correct |
31 |
Correct |
16 ms |
1984 KB |
Output is correct |
32 |
Correct |
11 ms |
1532 KB |
Output is correct |
33 |
Correct |
13 ms |
1792 KB |
Output is correct |
34 |
Correct |
11 ms |
1632 KB |
Output is correct |
35 |
Correct |
17 ms |
1792 KB |
Output is correct |
36 |
Correct |
16 ms |
1792 KB |
Output is correct |
37 |
Correct |
24 ms |
1792 KB |
Output is correct |
38 |
Correct |
15 ms |
1792 KB |
Output is correct |
39 |
Correct |
15 ms |
1920 KB |
Output is correct |
40 |
Correct |
17 ms |
2048 KB |
Output is correct |
41 |
Correct |
15 ms |
2048 KB |
Output is correct |
42 |
Correct |
16 ms |
1920 KB |
Output is correct |
43 |
Correct |
15 ms |
2048 KB |
Output is correct |
44 |
Correct |
17 ms |
2048 KB |
Output is correct |
45 |
Correct |
16 ms |
2048 KB |
Output is correct |
46 |
Correct |
15 ms |
1920 KB |
Output is correct |
47 |
Correct |
16 ms |
2048 KB |
Output is correct |
48 |
Correct |
226 ms |
17656 KB |
Output is correct |
49 |
Correct |
224 ms |
17528 KB |
Output is correct |
50 |
Correct |
224 ms |
17528 KB |
Output is correct |
51 |
Correct |
202 ms |
17400 KB |
Output is correct |
52 |
Correct |
199 ms |
14840 KB |
Output is correct |
53 |
Correct |
156 ms |
16120 KB |
Output is correct |
54 |
Correct |
139 ms |
13944 KB |
Output is correct |
55 |
Correct |
145 ms |
13688 KB |
Output is correct |
56 |
Correct |
145 ms |
13048 KB |
Output is correct |
57 |
Correct |
1092 ms |
89580 KB |
Output is correct |
58 |
Correct |
999 ms |
57028 KB |
Output is correct |
59 |
Correct |
1039 ms |
56912 KB |
Output is correct |
60 |
Correct |
1004 ms |
64912 KB |
Output is correct |
61 |
Correct |
966 ms |
64852 KB |
Output is correct |
62 |
Correct |
437 ms |
36428 KB |
Output is correct |
63 |
Correct |
637 ms |
45796 KB |
Output is correct |
64 |
Correct |
527 ms |
39904 KB |
Output is correct |
65 |
Correct |
290 ms |
25064 KB |
Output is correct |