Submission #670487

#TimeUsernameProblemLanguageResultExecution timeMemory
670487MahdiRope (JOI17_rope)C++17
80 / 100
2564 ms119132 KiB
#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]]; } if(m==1){ cout<<0<<'\n'; return 0; } set<pii>s; for(int i=1;i<=m;++i) s.insert({cnt[i], i}); for(int i=1;i<=m;++i){ s.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){ z=c[z]; s.erase({cnt[z], z}); w.push_back(z); --cnt[z]; s.insert({cnt[z], z}); } } auto it=s.end(); --it; ans[i]=cnt[i]+it->F; for(int u: w){ s.erase({cnt[u], u}); ++cnt[u]; s.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){ z=c[z]; s.erase({cnt[z], z}); w.push_back(z); --cnt[z]; s.insert({cnt[z], z}); } } it=s.end(); --it; ans[i]=max(ans[i], cnt[i]+it->F); for(int u: w){ s.erase({cnt[u], u}); ++cnt[u]; s.insert({cnt[u], u}); } s.insert({cnt[i], i}); } for(int i=1;i<=m;++i) cout<<n-ans[i]<<'\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...