제출 #1001043

#제출 시각아이디문제언어결과실행 시간메모리
1001043OtalpRope (JOI17_rope)C++17
55 / 100
2314 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<pii, int> dp[2]; int cnt[1001000]; int a[1001000]; vector<int> d[1000100]; 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()); int h[2]={0, 0}; for(int i=1; i+1<=n; i+=2){ int l=a[i], r=a[i + 1]; dp[1][{l, r}] ++; dp[1][{r, l}] ++; h[1] ++; 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][{l, r}] ++; dp[0][{r, l}] ++; h[0] ++; d[l].pb(r); d[r].pb(l); } ll dd = 0; for(int i=1; i<=m; i++) dd += d[i].size(); if(dd > 2e6) return; if(g.size() == 0 or g.size() > 1e6) return; 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][{i, j}]); } us[j] = 1; } while(ls >= 0 and us.count(g[ls].ss)) ls--; if(ls >= 0) ans = min(ans, n - cnt[i] - g[ls].ff); cout<<ans<<'\n'; } } signed main(){ ios_base::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL); 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...