Submission #424793

#TimeUsernameProblemLanguageResultExecution timeMemory
424793salehAncient Books (IOI17_books)C++17
12 / 100
1 ms308 KiB
#include "books.h" #include <bits/stdc++.h> #define ft first #define sd second using namespace std; typedef pair<int, int> pii; const int MAXN = 1000 * 1000 + 23; int mark[MAXN], cnt = 0, ans, jav; set<pii, greater<pii>> se; long long minimum_walk(std::vector<int> p, int s) { if (s != 0) return 0; int n = p.size(); for (int i = 0; i < n; i++) if (p[i] != i && mark[i] == 0) { int tmp = i, pre = i, mx = i; mark[i] = ++cnt; while ((tmp = p[tmp]) != i) { mark[tmp] = cnt; jav += abs(pre - tmp); pre = tmp; mx = max(mx, tmp); } se.insert({mx, i}); jav += abs(i - pre); ans = max(ans, i); } while (se.begin() -> ft > ans) { ans = min(ans, se.begin() -> sd); se.erase(se.begin()); } // cout << jav << ' ' << ans << endl; return jav + ans * 2; } //int main() {cout << minimum_walk({0, 2, 3, 1}, 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...