Submission #917948

#TimeUsernameProblemLanguageResultExecution timeMemory
917948nightfalAncient Books (IOI17_books)C++14
100 / 100
103 ms20052 KiB
#include <cstdio> #include <vector> #include <cassert> #include <cstdlib> #include <tuple> #include <climits> using namespace std; bool isSubtask3(int s) {return s==0;} bool isSubtask4(int n) {return n<=1000;} long long subtask3(vector<int> p, int s = 0, int e = -1 , int dir = 1) { int n = p.size(), last = s; if (e==-1) e = n-1; long long total = 0; for(int i=s; dir==1 && i<=e || dir==-1 && i>=e; i += dir) { if (i==p[i]) continue; if (dir*(i-last) > 0) total += abs(i-last)*2; total += abs(p[i]-i); last = dir==1? max(last,p[i]):min(last,p[i]); } return total; } long long cal(vector<int> p, int l, int r, int lmost, int rmost, vector<vector<long long>>& dp) { if (dp[l][r]!=LLONG_MAX) return dp[l][r]; int n = p.size(); long long temp1=LLONG_MAX, temp2=LLONG_MAX; if (l>0) { temp1 = abs(p[l-1]-(l-1)); if(l-1 < lmost) temp1 += 2; int lmostN = min(lmost,min(l-1,p[l-1])), rmostN = max(rmost,p[l-1]); temp1 += cal(p,l-1,r,lmostN,rmostN,dp); } if (r<n-1) { temp2 = abs(p[r+1]-(r+1)); if(r+1 > rmost) temp2 += 2; int lmostN = min(lmost,p[r+1]), rmostN = max(rmost,max(r+1,p[r+1])); temp2 += cal(p,l,r+1,lmostN,rmostN,dp); } dp[l][r] = min(temp1,temp2); return dp[l][r]; } long long subtask4(vector<int> p, int s) { int n = p.size(); // long long move = 0; int start; for(start = 0; start < s; start++) if(start!=p[start]) break; int end; for(end = n-1; end > s; end--) if(end!=p[end]) break; for(int i = 0; i <= end-start; i++) p[i] = p[i+start]-start; s -= start; n = end-start+1; p.resize(n); vector<vector<long long>> dp(n,vector<long long>(n,LLONG_MAX)); dp[0][n-1]=0; int l=s, r=s, lmost = min(s,p[s]), rmost = max(s,p[s]); return cal(p,l,r,lmost,rmost,dp)+abs(p[s]-s); } pair<int,int> extend(int l, int r, vector<int>& lmost, vector<int>& rmost) { while(l > min(lmost[l],lmost[r]) || r < max(rmost[l],rmost[r])) { l=min(l,min(lmost[l],lmost[r])); r=max(r,max(rmost[r],rmost[l])); } return make_pair(l,r); } long long fulltask(vector<int> p, int s) { int n = p.size(); // curtail ignorable head and tail. int start; for(start = 0; start < s; start++) if(start!=p[start]) break; int end; for(end = n-1; end > s; end--) if(end!=p[end]) break; long long totalJumps=0; for(int i=start; i<=end; i++) totalJumps += abs(p[i]-i); int l = min(s,p[s]), r=max(s,p[s]); vector<int> lmost(n), rmost(n); lmost[s] = min(s,p[s]); rmost[s] = max(s,p[s]); for(int i=s-1; i>=start; i--) {lmost[i] = min(lmost[i+1],p[i]); rmost[i] = max(rmost[i+1],p[i]);} for(int i=s+1; i<=end ; i++) {lmost[i] = min(lmost[i-1],p[i]); rmost[i] = max(rmost[i-1],p[i]);} tie(l,r) = extend(l,r,lmost,rmost); long long totalMoves=0, rMoves=0, lMoves=0; while(start<l || r<end) { int l2, r2; if(r<end && (l<=start || rMoves < lMoves)) { r++; rMoves++; tie(l2,r2) = extend(l,r,lmost,rmost); if (l2<l) {totalMoves += rMoves; rMoves = lMoves = 0;} } else { l--; lMoves++; tie(l2,r2) = extend(l,r,lmost,rmost); if (r<r2) {totalMoves += lMoves; rMoves = lMoves = 0;} } l = l2; r = r2; } totalMoves += rMoves + lMoves; // printf("totalMoves=%d, jumps=%d\n",totalMoves,jumps); return totalMoves*2+totalJumps; } long long minimum_walk(std::vector<int> p, int s) { // if (isSubtask3(s)) return subtask3(p); // if (isSubtask4(s)) return subtask4(p,s); return fulltask(p,s); }

Compilation message (stderr)

books.cpp: In function 'long long int subtask3(std::vector<int>, int, int, int)':
books.cpp:18:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   18 |     for(int i=s; dir==1 && i<=e || dir==-1 && i>=e; i += dir) {
      |                  ~~~~~~~^~~~~~~
#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...