Submission #1001019

#TimeUsernameProblemLanguageResultExecution timeMemory
1001019OtalpRope (JOI17_rope)C++17
55 / 100
2287 ms262144 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define ff first #define ss second #define pb push_back map<ll, int> dp[2]; int cnt[1001000]; int a[1001000]; vector<int> d[1000100]; ll h(ll a, ll b){ return a * 1e8 + b; } void solve(){ int n, m; cin>>n>>m; for(int i=1; i<=n; i++){ cin>>a[i]; cnt[a[i]] ++; } vector<pii> g; for(int i=1; i<=m; i++){ g.pb({cnt[i], i}); } sort(g.begin(), g.end()); for(int i=1; i+1<=n; i+=2){ int l=a[i], r=a[i + 1]; dp[1][h(l, r)] ++; dp[1][h(r, l)] ++; d[l].pb(r); d[r].pb(l); } for(int i=2; i+1<=n; i+=2){ int l=a[i], r=a[i + 1]; dp[0][h(l, r)] ++; dp[0][h(r, l)] ++; d[l].pb(r); d[r].pb(l); } for(int i=1; i<=m; i++){ int ans = n - cnt[i]; int ls = g.size() - 1; map<int, int> us; us[i] = 1; for(int j: d[i]){ if(j == i) continue; for(int k=0; k<2; k++){ ans = min(ans, n-cnt[i]-cnt[j]+dp[k][h(i, j)]); } us[j] = 1; } while(ls >= 0 and us[g[ls].ss]) ls--; if(ls >= 0) ans = min(ans, n - cnt[i] - g[ls].ff); cout<<ans<<'\n'; } } signed main(){ solve(); }
#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...