Submission #357667

#TimeUsernameProblemLanguageResultExecution timeMemory
357667beksultan04Ancient Books (IOI17_books)C++14
70 / 100
219 ms114028 KiB
#include "books.h" #ifndef EVAL #include "grader.cpp" #endif // EVAL #include <bits/stdc++.h> using namespace std; #define lol long long #define pii pair<int,int> #define pll pair<lol,lol> #define OK puts("OK"); #define NO puts("NO"); #define YES puts("YES"); #define fr first #define sc second #define ret return #define scanl(a) scanf("%lld",&a); #define scanll(a,b) scanf("%lld %lld",&a, &b); #define scanlll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c); #define scan1(a) scanf("%d",&a); #define scan2(a,b) scanf("%d %d",&a, &b); #define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c); #define all(s) s.begin(),s.end() #define allr(s) s.rbegin(),s.rend() #define pb push_back #define sz(v) (int)v.size() #define endi puts(""); #define eps 1e-12 int vis[1000001],n,tl,tr,t[1000001]; lol dp[2002][2002]; map<vector<int>,int> mp; pii q[1000001]; void extend(int &l,int &r){ int ll = min(q[t[l]].fr,q[t[r]].fr),rr = max(q[t[l]].sc,q[t[r]].sc); while (1){ if (ll < l){ ll = min(ll,q[t[--l]].fr); rr = max(rr,q[t[l]].sc); } else if (r < rr){ ll = min(ll,q[t[++r]].fr); rr = max(rr,q[t[r]].sc); } else break; } } lol rec(int l,int r){ if (l < tl || tr < r)ret 1e9+7; extend(l,r); if (l == tl && r == tr)ret 0; lol &res = dp[l][r]; if (res != -1)ret dp[l][r]; res = min(rec(l-1,r),rec(l,r+1))+2; ret res; } lol minimum_walk(vector<int> p, int s) { memset(dp,-1,sizeof(dp)); n = p.size(); int i,col=0; tl = tr = s; lol ans=0; for (i=0;i<n;++i){ if (vis[i] == 1)continue; int x = i,l=i,r=i; while (vis[x] == 0){ vis[x] = 1; l = min(l,x); r = max(x,r); t[x] = col; ans += abs(p[x]-x); x = p[x]; } q[col++] = {l,r}; if (l != r){ tl = min(tl,l); tr = max(tr,r); } } ret ans+rec(s,s); } /* 4 0 0 2 3 1 */
#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...