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;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
const int N=1e6+5;
int n, m, c[N], cnt[N], ans[N];
vector<int>v[N];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;++i){
cin>>c[i];
v[c[i]].push_back(i);
++cnt[c[i]];
}
set<pii>st;
for(int i=1;i<=m;++i)
st.insert({cnt[i], i});
for(int i=1;i<=m;++i){
st.erase({cnt[i], i});
vector<int>w;
for(int u: v[i]){
int z=u+((u&1) ? -1 : 1);
if(z>0 && z<=n && c[z]!=i){
st.erase({cnt[c[z]], c[z]});
w.push_back(c[z]);
--cnt[c[z]];
st.insert({cnt[c[z]], c[z]});
}
}
auto it=st.end();
--it;
ans[i]=cnt[i]+it->F;
for(int u: w){
st.erase({cnt[u], u});
++cnt[u];
st.insert({cnt[u], u});
}
w.clear();
for(int u: v[i]){
int z=u+((u&1) ? 1 : -1);
if(z>0 && z<=n && c[z]!=i){
st.erase({cnt[c[z]], c[z]});
w.push_back(c[z]);
--cnt[c[z]];
st.insert({cnt[c[z]], c[z]});
}
}
it=st.end();
--it;
ans[i]=max(ans[i], cnt[i]+it->F);
for(int u: w){
st.erase({cnt[u], u});
++cnt[u];
st.insert({cnt[u], u});
}
st.insert({c[i], i});
}
for(int i=1;i<=m;++i)
cout<<n-ans[i]<<'\n';
}
# | 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... |