#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int N, M;
int A[1010101];
int Ecnt[1010101], E[1010101], EM, ES;
int Ocnt[1010101], O[1010101], OM, OS;
vector<int> Ec[1010101], Oc[1010101];
int Eup(int k, int v){
Ecnt[E[k]]--;
if (!Ecnt[EM]) EM--;
else if (EM == ES && Ecnt[EM]<=1) ES--;
else if (!Ecnt[ES]) ES--;
E[k] += v;
if (EM < E[k] && v>0) EM = E[k];
else if (ES < E[k] && v>0) ES = E[k];
Ecnt[E[k]]++;
}
int Oup(int k, int v){
Ocnt[O[k]]--;
if (!Ocnt[OM]) OM--;
else if (OM == OS && Ocnt[OM]<=1) OS--;
else if (!Ocnt[OS]) OS--;
O[k] += v;
if (OM < O[k] && v>0) OM = O[k];
else if (OS < O[k] && v>0) OS = O[k];
Ocnt[O[k]]++;
}
int main(){
scanf("%d %d", &N, &M);
if (M == 1){
puts("0");
return 0;
}
Ecnt[0]=Ocnt[0]=M;
for (int i=1; i<=N; i++) scanf("%d", &A[i]);
for (int i=1; i+1<=N; i+=2){
Eup(A[i], 1);
Eup(A[i+1], 1);
if (A[i] != A[i+1]){
Ec[A[i]].push_back(A[i+1]);
Ec[A[i+1]].push_back(A[i]);
}
}
for (int i=2; i+1<=N; i+=2){
Oup(A[i], 1);
Oup(A[i+1], 1);
if (A[i] != A[i+1]){
Oc[A[i]].push_back(A[i+1]);
Oc[A[i+1]].push_back(A[i]);
}
}
if (N & 1){
Eup(A[N], 1);
Oup(A[1], 1);
}
else{
Oup(A[1], 1);
Oup(A[N], 1);
}
for (int i=1; i<=M; i++){
int ans=0;
for (int x : Ec[i]) Eup(x, -1);
for (int x : Oc[i]) Oup(x, -1);
if (E[i] == EM) ans = max(ans, E[i]+ES);
else ans = max(ans, E[i]+EM);
if (O[i] == OM) ans = max(ans, O[i]+OS);
else ans = max(ans, O[i]+OM);
printf("%d\n", N-ans);
for (int x : Ec[i]) Eup(x, 1);
for (int x : Oc[i]) Oup(x, 1);
}
return 0;
}
Compilation message
rope.cpp: In function 'int Eup(int, int)':
rope.cpp:21:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
rope.cpp: In function 'int Oup(int, int)':
rope.cpp:32:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
rope.cpp: In function 'int main()':
rope.cpp:35:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
rope.cpp:41:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i=1; i<=N; i++) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
47736 KB |
Output is correct |
2 |
Correct |
46 ms |
47864 KB |
Output is correct |
3 |
Correct |
46 ms |
47736 KB |
Output is correct |
4 |
Correct |
46 ms |
47864 KB |
Output is correct |
5 |
Incorrect |
45 ms |
47736 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
47736 KB |
Output is correct |
2 |
Correct |
46 ms |
47864 KB |
Output is correct |
3 |
Correct |
46 ms |
47736 KB |
Output is correct |
4 |
Correct |
46 ms |
47864 KB |
Output is correct |
5 |
Incorrect |
45 ms |
47736 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
47736 KB |
Output is correct |
2 |
Correct |
46 ms |
47864 KB |
Output is correct |
3 |
Correct |
46 ms |
47736 KB |
Output is correct |
4 |
Correct |
46 ms |
47864 KB |
Output is correct |
5 |
Incorrect |
45 ms |
47736 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
47736 KB |
Output is correct |
2 |
Correct |
46 ms |
47864 KB |
Output is correct |
3 |
Correct |
46 ms |
47736 KB |
Output is correct |
4 |
Correct |
46 ms |
47864 KB |
Output is correct |
5 |
Incorrect |
45 ms |
47736 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
47736 KB |
Output is correct |
2 |
Correct |
46 ms |
47864 KB |
Output is correct |
3 |
Correct |
46 ms |
47736 KB |
Output is correct |
4 |
Correct |
46 ms |
47864 KB |
Output is correct |
5 |
Incorrect |
45 ms |
47736 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |