Submission #1140915

#TimeUsernameProblemLanguageResultExecution timeMemory
1140915steveonalexAncient Books (IOI17_books)C++20
100 / 100
115 ms16032 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1ULL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){return __gcd(a, b);} ll lcm(ll a, ll b){return a / gcd(a, b) * b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ull mask){return __builtin_popcountll(mask);} int ctz(ull mask){return __builtin_ctzll(mask);} int logOf(ull mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} double rngesus_d(double l, double r){ double cur = rngesus(0, MASK(60) - 1); cur /= MASK(60) - 1; return l + cur * (r - l); } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: container) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } #include "books.h" const int INF = 1e9 + 69; void update(pair<int, int> &x, pair<int, int> y){ minimize(x.first, y.first); maximize(x.second, y.second); } ll minimum_walk(vector<int> p, int s) { s++; int n = p.size(); for(int &i: p) i++; p.insert(p.begin(), 0); ll ans = 0; pair<int, int> min_range = {s, s}; for(int i = 1; i <= n; ++i){ ans += abs(i - p[i]); if (i !=p[i]){ minimize(min_range.first, i); maximize(min_range.second, i); } } vector<pair<int,int>> big_range(n+1, {-1, -1}); for(int i = 1; i <= n; ++i){ if (big_range[i].first != -1) continue; pair<int,int> mi = {i, i}; int j = p[i]; while(j != i){ minimize(mi.first, j); maximize(mi.second, j); j = p[j]; } do{ big_range[j] = mi; j = p[j]; } while(j != i); } pair<int, int> cur = {s, s}; pair<int, int> projected = big_range[s]; while(true){ while(cur.first > projected.first || cur.second < projected.second){ if (cur.first > projected.first){ update(projected, big_range[--cur.first]); } else{ update(projected, big_range[++cur.second]); } } if (cur.first <= min_range.first && cur.second >= min_range.second) break; int left = 0, right = 0; bool check = true; // go left pair<int, int> tmp = cur, _projected = projected; while(tmp.first > min_range.first && tmp.second == cur.second){ update(_projected, big_range[--tmp.first]); left++; while(tmp.first > _projected.first || tmp.second < _projected.second){ if (tmp.first > _projected.first){ update(_projected, big_range[--tmp.first]); } else{ update(_projected, big_range[++tmp.second]); } } } if (tmp.second == cur.second) check = false; // go right tmp = cur, _projected = projected; while(tmp.second < min_range.second && tmp.first == cur.first){ update(_projected, big_range[++tmp.second]); right++; while(tmp.first > _projected.first || tmp.second < _projected.second){ if (tmp.first > _projected.first){ update(_projected, big_range[--tmp.first]); } else{ update(_projected, big_range[++tmp.second]); } } } if (tmp.first == cur.first) check = false; cur = tmp; projected = _projected; if (check){ ans += min(left, right) * 2; } else{ ans += (left + right) * 2; break; } } return ans; } // int main(void){ // ios::sync_with_stdio(0);cin.tie(0); cout.tie(0); // clock_t start = clock(); // int n; cin >> n; // vector<int> p(n); // for(int i = 0; i<n; ++i) cin >> p[i]; // int s; cin >> s; // cout << minimum_walk(p, s) << "\n"; // cerr << "Time elapsed: " << clock() - start << " ms\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...