This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#define MAXN 1000005
using namespace std;
int N, M, C[MAXN], Val[MAXN][2], Ans[MAXN];
vector <int> Pairs[MAXN][2];
class segtree {
int ST[MAXN * 2];
public:
void upd(int ind, int val) {
for (ind += M, ST[ind] += val; ind > 1; ind >>= 1) {
ST[ind >> 1] = max(ST[ind], ST[ind ^ 1]);
}
}
int get() {return(ST[1]);}
} ST1, ST2;
int main() {
//freopen("ropein.txt","r",stdin);
scanf("%d %d",&N,&M);
for (int i=1; i<=M; i++) {
Ans[i] = N;
}
for (int i=1; i<=N; i++) {
scanf("%d",&C[i]);
Ans[C[i]] -= 1;
}
for (int i=1; i<N; i+=2) {
Pairs[C[i]][0].push_back(C[i+1]);
Pairs[C[i+1]][0].push_back(C[i]);
Val[C[i]][0]++, Val[C[i+1]][0]++;
}
for (int i=2; i<N; i+=2) {
Pairs[C[i]][1].push_back(C[i+1]);
Pairs[C[i+1]][1].push_back(C[i]);
Val[C[i]][1]++, Val[C[i+1]][1]++;
}
if (N%2) {
Val[C[N]][0]++, Val[C[1]][1]++;
} else {
Val[C[1]][1]++, Val[C[N]][1]++;
}
for (int i=1; i<=M; i++) {
ST1.upd(i, Val[i][0]);
ST2.upd(i, Val[i][1]);
}
for (int i=1; i<=M; i++) {
//solve case 1
for (int v: Pairs[i][0]) {
ST1.upd(v, -1);
}
ST1.upd(i, -1<<20);
Ans[i] = min(Ans[i], N - ST1.get() - Val[i][0]);
for (int v: Pairs[i][0]) {
ST1.upd(v, 1);
}
ST1.upd(i, 1<<20);
//solve case 2:
for (int v: Pairs[i][1]) {
ST2.upd(v, -1);
}
ST2.upd(i, -1<<20);
Ans[i] = min(Ans[i], N - ST2.get() - Val[i][1]);
for (int v: Pairs[i][1]) {
ST2.upd(v, 1);
}
ST2.upd(i, 1<<20);
}
for (int i=1; i<=M; i++) {
printf("%d\n",Ans[i]);
}
}
Compilation message (stderr)
rope.cpp: In function 'int main()':
rope.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
21 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
rope.cpp:26:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
26 | scanf("%d",&C[i]);
| ~~~~~^~~~~~~~~~~~
# | 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... |