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>
#define SIZE 1000001
using namespace std;
int N,M,A[SIZE],B[SIZE],C[SIZE],D[SIZE],ans[SIZE];
vector<int>v1[SIZE],v2[SIZE];
multiset<pair<int,int> >st;
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0);
cin>>N>>M;
for(int i=1;i<=N;i++){
cin>>A[i];
B[A[i]]++;
}
for(int i=1;i<=M;i++){
st.insert({-B[i],i});
ans[i]=1e9;
}
for(int i=1;i<N;i+=2){
v1[A[i]].push_back(A[i+1]);
v1[A[i+1]].push_back(A[i]);
}
for(int i=1;i<=M;i++){
set<int>u;
C[i]=-B[i];
u.insert(i);
for(int j:v1[i]){
if(u.find(j)==u.end()){
C[j]=-B[j];
u.insert(j);
}
st.erase({C[j],j});
st.insert({++C[j],j});
}
st.erase({C[i],i});
if(!st.empty())ans[i]=min(ans[i],N-B[i]+st.begin()->first);
else ans[i]=0;
st.insert({C[i],i});
for(int j:u){
st.erase({C[j],j});
st.insert({-B[j],j});
}
}
for(int i=2;i<N;i+=2){
v2[A[i]].push_back(A[i+1]);
v2[A[i+1]].push_back(A[i]);
}
for(int i=1;i<=M;i++){
set<int>u;
C[i]=-B[i];
u.insert(i);
for(int j:v2[i]){
if(u.find(j)==u.end()){
C[j]=-B[j];
u.insert(j);
}
st.erase({C[j],j});
st.insert({++C[j],j});
}
st.erase({C[i],i});
if(!st.empty())ans[i]=min(ans[i],N-B[i]+st.begin()->first);
else ans[i]=0;
st.insert({C[i],i});
for(int j:u){
st.erase({C[j],j});
st.insert({-B[j],j});
}
printf("%d\n",ans[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... |