Submission #977396

#TimeUsernameProblemLanguageResultExecution timeMemory
977396mariaclaraAncient Books (IOI17_books)C++17
0 / 100
1 ms348 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define mk make_pair #define pb push_back #define fr first #define sc second ll minimum_walk(vector<int> p, int s) { int n = sz(p); vector<bool> vis(n,0); vector<pii> aux, range; ll ans = 0; for(int i = 0; i < n; i++) { int x = i, m = i; while(!vis[x]) { vis[x] = 1; x = p[x]; m = max(m, x); } ans += abs(p[i]-i); if(i != m) aux.pb({i,m}); } //cout << "ok" << endl; for(int i = 0; i < sz(aux);) { int l = aux[i].fr, r = aux[i].sc, j = i; while(j < sz(aux) and aux[j].fr <= r) { //cout << i << " " << j << " " << l << " " << r << endl; r = max(r, aux[j].sc); j++; } i=j; range.pb({l,r}); } //cout << "ok" << endl; for(int i = 1; i < sz(range); i++) ans += 2*(range[i].fr - range[i-1].sc); int dist = n; for(int i = 0; i < n; i++) if(p[i] != i) dist = min(dist, abs(s-i)); return ans * 2*(dist%n); }
#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...