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<bits/stdc++.h>
using namespace std;
const int N = 1e6;
const int inf = 2e9;
int n,k;
int v[N];
int f[N];
int p[N];
int ans[N];
pair<int,int> special[N];
int cnt;
int get(pair<int,int>a){
int l = 0,r = cnt - 1;
if(cnt == 0)return 0;
while(l != r){
int mij = (l + r)/2;
if(special[mij] < a)l = mij + 1;
else r = mij;
}
while(r < cnt && special[r] == a){
r++;
}
return r - l;
}
void solve(int l, int r){
cnt = 0;
for(int i = l;i < r;i+=2){
if(v[i] != v[i + 1]){
special[cnt++] = {min(v[i],v[i + 1]),max(v[i],v[i + 1])};
}
}
sort(special,special + cnt);
for(int i = 0;i < k;i++){
for(int j = 0;j < k;j++){
if(i == p[j])continue;
int nr = get({min(i,p[j]),max(i,p[j])});
//cout<<l<<' '<<r<<' '<<i<<' '<<j<<' '<<p[j]<<' '<<n<<' '<<nr<<' '<<n - f[p[j]] - f[i] - nr<<'\n';
ans[i] = min(ans[i],n - f[p[j]] - f[i] + nr);
if(nr == 0)break;
}
}
}
int main(){
cin>>n>>k;
for(int i = 0;i < n;i++){
cin>>v[i];
v[i]--;
f[v[i]]++;
ans[i] = inf;
p[i] = i;
}
sort(p,p + k,[&](int a,int b){
return f[a] > f[b];
});
if(n%2 == 0){
solve(0,n - 1);
if(n >= 2)solve(1,n - 2);
}else{
solve(0,n - 2);
solve(1,n - 1);
}
for(int i = 0;i < k;i++){
ans[i] = min(ans[i],n - f[i]);
cout<<ans[i]<<'\n';
}
return 0;
}
# | 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... |