Submission #51002

#TimeUsernameProblemLanguageResultExecution timeMemory
51002DiuvenRope (JOI17_rope)C++11
100 / 100
1652 ms81856 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; const int MX=1000010, inf=2e9; int n, m, A[MX]; int C[MX]; int ans[MX]; int idx[MX]; vector<int> G[MX]; void init(int st){ for(int i=1; i<=m; i++) G[i].clear(), C[i]=0; for(int i=st; i<=n; i+=2){ C[A[i]]++; C[A[i+1]]++; } C[0]=0; for(int i=st; i<n; i+=2){ if(i<1 || A[i]==A[i+1]) continue; G[A[i]].push_back(A[i+1]); G[A[i+1]].push_back(A[i]); } iota(idx+1, idx+m+1, 1); sort(idx+1, idx+m+1, [](int a, int b){ return C[a]>C[b]; }); } void solve(int st){ init(st); int adj[MX]={}; for(int v=1; v<=m; v++){ int mx=0; for(int x:G[v]) adj[x]++; for(int x:G[v]){ mx=max(mx, C[x]-adj[x]); } for(int i=1; i<=m; i++){ int x=idx[i]; if(x!=v && adj[x]==0){ mx=max(mx, C[x]); break; } } for(int x:G[v]) adj[x]--; ans[v]=min(ans[v], n-C[v]-mx); } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cin>>n>>m; for(int i=1; i<=n; i++) cin>>A[i]; fill(ans+1, ans+m+1, inf); solve(0); solve(1); for(int i=1; i<=m; i++) cout<<ans[i]<<'\n'; return 0; }
#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...