Submission #581723

#TimeUsernameProblemLanguageResultExecution timeMemory
581723alirezasamimi100Ancient Books (IOI17_books)C++17
0 / 100
1 ms296 KiB
#include "books.h" #include <bits/stdc++.h> #define pb push_back #define F first #define S second using namespace std; using ll = long long int; using pll = pair<ll, ll>; using pii = pair<int, int>; const int inf = 1.05e9; const ll INF = 1e18; ll minimum_walk(vector<int> p, int s) { ll ans = 0, mn; int n = p.size(); int f[n]; memset(f, 0, sizeof f); vector<pii> wtf; for(int i = 0; i < n; i++){ if(f[i]) continue; int ml = inf, mr = inf; for(int j = i; !f[j]; j = p[j]){ f[j] = 1; ans += abs(j - p[j]); if(j <= s) ml = min(ml, s - j); if(j >= s) mr = min(mr, j - s); } wtf.pb({ml, mr}); } int k = wtf.size(); int pm[k]; sort(wtf.begin(), wtf.end()); pm[k - 1] = wtf[k - 1].S; for(int i = k - 2; i >= 0; i--){ pm[i] = max(pm[i + 1], wtf[i].S); } mn = min(pm[0], wtf[k - 1].F); for(int i = 0; i < k - 1; i++) mn = min(mn, (ll)wtf[i].F + pm[i + 1]); return ans + 2 * mn; }
#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...