Submission #421493

#TimeUsernameProblemLanguageResultExecution timeMemory
421493MarcoMeijerAncient Books (IOI17_books)C++14
50 / 100
211 ms16116 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; // macros typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef pair<ll, ll> lll; typedef tuple<int, int, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<ll> vll; typedef vector<lll> vlll; #define REP(a,b,c) for(int a=int(b); a<int(c); a++) #define RE(a,c) REP(a,0,c) #define RE1(a,c) REP(a,1,c+1) #define REI(a,b,c) REP(a,b,c+1) #define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--) #define FOR(a,b) for(auto& a : b) #define all(a) a.begin(), a.end() #define INF 1e9 #define EPS 1e-9 #define pb push_back #define popb pop_back #define fi first #define se second #define sz size() mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll minimum_walk(vi p, int s) { int n = p.size(); ll ans = 0; vi visited; visited.assign(n,0); /*if(s == 0) { ll rightMost = 0; RE(i,n) { if(visited[i]) continue; if(i == p[i]) continue; int u = p[i]; ans += (ll)abs(i-p[i]); if(rightMost < i) ans += (i-rightMost)*2ll; while(u != i) { visited[u] = 1; rightMost = max<ll>(rightMost, u); ans += (ll)abs(u - p[u]); u = p[u]; } } } else {*/ int ls=s, rs=s; queue<int> q; q.push(s); while(ls != 0 || rs != n-1 || !q.empty()) { while(!q.empty()) { int i = q.front(); q.pop(); if(visited[i]) continue; if(i == p[i]) continue; int u = p[i]; ans += (ll)abs(i-p[i]); while(u != i) { visited[u] = 1; ans += (ll)abs(u - p[u]); while(ls > u) q.push(--ls); while(rs < u) q.push(++rs); u = p[u]; } } int leftSize = INF, rightSize = INF; REV(i,0 ,ls) if(p[i] != i) { leftSize = ls - i; break; } REP(i,rs+1,n ) if(p[i] != i) { rightSize = i - rs; break; } if(leftSize == INF && rightSize == INF) { break; } if(leftSize < rightSize) { RE(_,leftSize) q.push(--ls), ans += 2ll; } else { RE(_,rightSize) q.push(++rs), ans += 2ll; } } // } 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...