Submission #371098

#TimeUsernameProblemLanguageResultExecution timeMemory
371098dooweyAncient Books (IOI17_books)C++14
22 / 100
2082 ms24556 KiB
#include <bits/stdc++.h> #include "books.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; #define fi first #define se second #define mp make_pair int n; const int N = 1010; int lef[N], rig[N]; bool vis[N]; bool has[N]; int tl, tr; int ash[1005][1005]; void extend(int &li, int &ri){ int ni = min(lef[li],lef[ri]); int nj = max(rig[li],rig[ri]); while(li > ni || ri < nj){ if(li > ni){ li--; ni = min(ni, lef[li]); nj = max(nj, rig[li]); } else{ ri++; ni = min(ni, lef[ri]); nj = max(nj, rig[ri]); } } } int solve(int li, int ri){ if(li < tl || ri > tr) return (int)1e9 + 9; if(ash[li][ri] != -1) return ash[li][ri]; extend(li, ri); if(li == tl && ri == tr) return ash[li][ri]=0; return ash[li][ri]=min(solve(li-1,ri), solve(li,ri+1)) + 2; } long long minimum_walk(vector<int> p, int s) { n = p.size(); int id; int low, high; ll sol = 0; tl = tr = s; for(int i = 0 ; i < n; i ++ ){ for(int j = 0 ; j < n; j ++ ){ ash[i][j]=-1; } } for(int i = 0 ; i < n; i ++ ){ sol += abs(p[i] - i); if(!vis[i]){ vector<int> cyc; id = i; while(!vis[id]){ vis[id]=true; cyc.push_back(id); id=p[id]; } low = n-1; high = 0; for(auto x : cyc){ low = min(low, x); high = max(high, x); } for(auto x : cyc){ lef[x] = low; rig[x] = high; } } if(p[i] != i){ tl = min(tl, i); tr = max(tr, i); } } return sol + solve(s, s); }
#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...