Submission #719583

#TimeUsernameProblemLanguageResultExecution timeMemory
719583Jarif_RahmanAncient Books (IOI17_books)C++17
22 / 100
2045 ms106444 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; struct dsu{ int n; vector<int> sz, p; dsu(int nn){ n = nn; sz.resize(n, 1); p.resize(n); for(int i = 0; i < n; i++) p[i] = i; } int get(int x){ if(p[x] != x) p[x] = get(p[x]); return p[x]; } void unite(int a, int b){ a = get(a), b = get(b); if(a == b) return; if(sz[b] > sz[a]) swap(a, b); sz[a]+=sz[b]; sz[b] = 0; p[b] = a; } }; ll minimum_walk(vector<int> p, int s){ int n = p.size(); ll ans = 0; vector<int> C(n, -1), sz; int k = 0; for(int i = 0; i < n; i++){ if(C[i] != -1) continue; int x = p[i]; C[i] = k; sz.pb(1); ans+=abs(i-x); while(x != i){ C[x] = k; sz.back()++; ans+=abs(p[x]-x); x = p[x]; } k++; } //for(int x: C) cerr << x << " "; cerr << "C1\n"; vector<int> first(k, -1), last(k); for(int i = 0; i < n; i++){ if(first[C[i]] == -1) first[C[i]] = i; last[C[i]] = i; } int L, R; vector<bool> unnecessary(k, 0); for(int i = 0; i < n; i++){ if(sz[C[i]] > 1 || i == s){ L = i; break; } unnecessary[C[i]] = 1; } for(int i = n-1; i >= 0; i--){ if(sz[C[i]] > 1 || i == s){ R = i; break; } unnecessary[C[i]] = 1; } vector<set<int>> sth(k); for(int i = 0; i < n; i++) sth[C[i]].insert(i); dsu ds(k); for(int T = 0; T < k-1; T++){ for(int i = 0; i+1 < n; i++){ if(unnecessary[C[i]] || unnecessary[i+C[1]]) continue; if(C[i] == C[i+1]) continue; if(i == last[C[i]] && i+1 == first[C[i+1]]) continue; auto it = sth[C[i]].find(i); if(it != sth[C[i]].end() && i+1 == first[C[i+1]] && *next(it) > last[C[i+1]]) continue; it = sth[C[i+1]].find(i+1); if(it != sth[C[i+1]].begin() && i == last[C[i]] && *prev(it) < first[C[i]]) continue; ds.unite(C[i], C[i+1]); } for(int &x: C) x = ds.get(x); first.assign(k, -1); for(int i = 0; i < n; i++){ if(first[C[i]] == -1) first[C[i]] = i; last[C[i]] = i; } for(int i = 0; i < k; i++){ if(unnecessary[i] || ds.get(i) == i) continue; int p = ds.get(i); if(sth[p].size() > sth[i].size()) swap(sth[p], sth[i]); for(int x: sth[i]) sth[p].insert(x); sth[i].clear(); } } //for(int x: C) cerr << x << " "; cerr << "C2\n"; int l = first[C[s]], r = last[C[s]]; int cnt = 0; while(l > L || r < R){ if(cnt++ > 2*n) { exit(-1); } int in = -1; ll lx = 0, rx = 0; for(int i = l-1; i >= 0; i--){ if(unnecessary[C[i]]) break; if(C[i] == C[i+1]) continue; lx+=2; if(i != last[C[i]]){ in = i; break; } i = first[C[i]]; } for(int i = r+1; i < n; i++){ if(unnecessary[C[i]]) break; if(C[i] == C[i-1]) continue; rx+=2; if(i != first[C[i]]){ in = i; break; } i = last[C[i]]; } if(in == -1){ ans+=lx+rx; break; } ans+=min(lx, rx); l = first[C[in]], r = last[C[in]]; } return ans; }
#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...