Submission #624683

#TimeUsernameProblemLanguageResultExecution timeMemory
624683wiwihoAncient Books (IOI17_books)C++14
50 / 100
210 ms58196 KiB
#include "books.h" #include <bits/stdc++.h> #include <bits/extc++.h> #define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define lsort(a) sort(iter(a)) #define gsort(a) sort(riter(a)) #define pb(a) push_back(a) #define eb(a) emplace_back(a) #define pf(a) push_front(a) #define ef(a) emplace_front(a) #define pob pop_back() #define pof pop_front() #define mp(a, b) make_pair(a, b) #define F first #define S second #define mt make_tuple #define gt(t, i) get<i>(t) #define tomax(a, b) ((a) = max((a), (b))) #define tomin(a, b) ((a) = min((a), (b))) #define topos(a) ((a) = (((a) % MOD + MOD) % MOD)) #define uni(a) a.resize(unique(iter(a)) - a.begin()) #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ b << "\n";} using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef long double ld; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using tiii = tuple<int, int, int>; const ll MOD = 1000000007; const ll MAX = 2147483647; template<typename A, typename B> ostream& operator<<(ostream& o, pair<A, B> p){ return o << '(' << p.F << ',' << p.S << ')'; } ll ifloor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a < 0) return (a - b + 1) / b; else return a / b; } ll iceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a > 0) return (a + b - 1) / b; else return a / b; } int n; vector<int> dsu, mx; int findDSU(int a){ if(dsu[a] != a) dsu[a] = findDSU(dsu[a]); return dsu[a]; } void unionDSU(int a, int b){ a = findDSU(a); b = findDSU(b); if(a > b) swap(a, b); if(a == b) return; dsu[b] = a; mx[a] = max(mx[a], mx[b]); } ll minimum_walk(vector<int> arr, int s){ n = arr.size(); //printv(arr, cerr); dsu.resize(n); mx.resize(n); iota(iter(dsu), 0); iota(iter(mx), 0); ll ans = 0; for(int i = 0; i < n; i++){ ans += abs(i - arr[i]); unionDSU(i, arr[i]); } vector<int> st; vector<int> vst(n); vector<int> root; vector<vector<int>> child(n); vector<int> pr(n); for(int i = 0; i < n; i++){ int p = findDSU(i); if(vst[p] == 0){ vst[p] = 1; st.eb(p); } else{ while(st.back() != p){ unionDSU(p, st.back()); st.pob; } } //cerr << "owo " << i << " " << p << " : "; //printv(st, cerr); if(mx[p] == i){ st.pob; if(st.empty()) pr[p] = -1; else pr[p] = st.back(); } } for(int i = 0; i < n; i++){ if(dsu[i] != i) continue; if(pr[i] == -1){ root.eb(i); continue; } pr[i] = findDSU(pr[i]); child[pr[i]].eb(i); } int now = s; while(pr[now] != -1){ int p = pr[now]; //cerr << "test " << now << " " << p << "\n"; //printv(child[p], cerr); int l = 1, r = 1; int pos = -1; for(int i = 0; i < (int)child[p].size(); i++){ if(child[p][i] == now) pos = i; } int lst = now; for(int i = pos - 1; i >= 0; i--){ int c = child[p][i]; if(mx[c] + 1 != lst) break; l++; lst = c; } lst = mx[now]; for(int i = pos + 1; i < (int)child[p].size(); i++){ int c = child[p][i]; if(lst + 1 != c) break; r++; lst = mx[c]; } ans += min(l, r) * 2; now = p; } ans += ((int)root.size() - 1) * 2; for(int i = 0; i < now; i++){ if(arr[i] != i) break; ans -= 2; } for(int i = n - 1; i > mx[now]; i--){ if(arr[i] != i) break; ans -= 2; } 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...