Submission #1073852

#TimeUsernameProblemLanguageResultExecution timeMemory
1073852ArthuroWichAncient Books (IOI17_books)C++17
12 / 100
2 ms384 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; long long minimum_walk(vector<int> p, int s) { long long int ans = 0, n = p.size(); for (int i = 0; i < n; i++) { ans += abs(i-p[i]); } vector<pair<int, int>> cycles; for (int i = 0; i < n; i++) { if (p[i] == i) { continue; } int a = i, l = i, r = i; a = p[i]; l = min(l, a); r = max(r, a); while(a != i) { a = p[a]; l = min(l, a); r = max(r, a); } cycles.push_back({min(l, r), max(l, r)}); } vector<int> pref(n+1, 0), f1(n, 0), f2(n, 0); for (auto [i, j] : cycles) { pref[i] += 1; pref[j+1] -= 1; } for (int i = 1; i < n; i++) { pref[i] += pref[i-1]; } for (int i = n-1; i >= 0; i--) { if (pref[i] > 0) { f2[i] = 1; } if (i < n-1) { f2[i] |= f2[i+1]; } } for (int i = n-1; i >= 0; i--) { if (f2[i] && pref[i] == 0) { 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...