Submission #984172

#TimeUsernameProblemLanguageResultExecution timeMemory
984172Alkaser_IDRainforest Jumps (APIO21_jumps)C++17
4 / 100
4008 ms17208 KiB
#include "jumps.h" using namespace std; #include <bits/stdc++.h> bool sb1 = true; int n, dp[200005], ad[200005], a[200005]; int nxup[200005],nxls[200005],lsup[200005]; void init(int N, std::vector<int> H) { n = N; for(int i=0;i<n;++i) a[i] = H[i]; for(int i=1;i<n;++i){ if(a[i] < a[i-1]) sb1 = false; } vector<pair<int,int>> v; for(int i=0;i<n;++i){ v.push_back({H[i],i}); } sort(v.begin(),v.end(),greater<pair<int,int>>()); for(int i=0;i<n;++i) nxup[i] = nxls[i] = lsup[i] = -1; set<int> s; for(int i=0;i<n;++i) { if(!s.empty()){ set<int>::iterator it= s.upper_bound(v[i].second); if(it != s.end()){ nxup[v[i].second] = *it; } if(it != s.begin()){ --it; lsup[v[i].second] = *it; } } s.insert(v[i].second); } } int minimum_jumps(int A, int B, int C, int D) { if(sb1) return C - B; if(A == B && C == D) { if(a[C] < a[B]) return -1; int res = 0; for(int cn = 0;;++cn){ if(cn > 2 *n) { int vr = 5 / 0; return vr; } if(B == C) break; if(nxup[B] == -1 || B > C || B == -1) {res = -1; break;} ++res; int ls = lsup[B],nx = nxup[B]; if(ls != -1) { if(a[ls] > a[nx] && a[ls] < a[C]) B = ls; else B = nx; } else B = nx; } return res; } int res = 1e9; for(int i= C;i <= D; ++i) dp[i] = 0; for(int i = A;i <= B; ++i) ad[i] = 0; for(int i=0;i < A; ++i) ad[i] = 1e9; for(int i=C-1;i>=0;i--){ int nx = nxup[i]; if(nx > D) dp[i] = 1e9; else dp[i] = dp[nx] + 1; if(i >= A && i <= B){ res = min(res,dp[i]); } int ls = lsup[i]; if(ls != -1){ ad[ls] = min(ad[ls], ad[i] + 1); } else if(i< A){ res = min(res,dp[i] + ad[i]); } } if(res >= 1e9) return -1; return res; } /* 8 5 3 2 1 7 4 5 6 8 4 4 7 7 1 3 5 6 0 1 2 2 2 2 7 7 1 1 5 5 */

Compilation message (stderr)

jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:45:28: warning: division by zero [-Wdiv-by-zero]
   45 |                 int vr = 5 / 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...