Submission #541626

#TimeUsernameProblemLanguageResultExecution timeMemory
541626aryan12Rainforest Jumps (APIO21_jumps)C++17
56 / 100
4058 ms45660 KiB
#include "jumps.h" #include <vector> using namespace std; #include <bits/stdc++.h> const int MAXN = 2e5 + 5; int DP_Min[19][MAXN], DP_Max[19][MAXN]; vector<int> INPUT; vector<int> g[MAXN]; void init(int N, vector<int> H) { vector<int> L(N), R(N); stack<int> q; for(int i = 0; i < N; i++) { INPUT.push_back(H[i]); while(!q.empty() && H[q.top()] <= H[i]) { q.pop(); } if(q.empty()) { L[i] = -1; } else { L[i] = q.top(); } q.push(i); } while(!q.empty()) { q.pop(); } for(int i = N - 1; i >= 0; i--) { while(!q.empty() && H[q.top()] <= H[i]) { q.pop(); } if(q.empty()) { R[i] = N; } else { R[i] = q.top(); } q.push(i); } for(int i = 0; i < N; i++) { if(L[i] == -1 && R[i] == N) { DP_Min[0][i] = -1; DP_Max[0][i] = -1; } else if(L[i] == -1 || R[i] == N) { if(L[i] == -1) { DP_Min[0][i] = R[i]; DP_Max[0][i] = R[i]; } else { DP_Min[0][i] = L[i]; DP_Max[0][i] = L[i]; } } else { if(H[L[i]] < H[R[i]]) { DP_Min[0][i] = L[i]; DP_Max[0][i] = R[i]; } else { DP_Min[0][i] = R[i]; DP_Max[0][i] = L[i]; } //DP_Min[0][i] = min(L[i], R[i]); //DP_Max[0][i] = max(L[i], R[i]); } } for(int i = 1; i < 19; i++) { for(int j = 0; j < N; j++) { if(DP_Min[i - 1][j] == -1) { DP_Min[i][j] = -1; } else { DP_Min[i][j] = DP_Min[i - 1][DP_Min[i - 1][j]]; } if(DP_Max[i - 1][j] == -1) { DP_Max[i][j] = -1; } else { DP_Max[i][j] = DP_Max[i - 1][DP_Max[i - 1][j]]; } } } //cout << "L[i] and R[i]\n"; //for(int i = 0; i < N; i++) { // cout << L[i] << " " << R[i] << "\n"; //} //cout << DP_Min[0][4] << " " << DP_Max[0][4] << "\n"; for(int i = 0; i < N; i++) { if(L[i] != -1) { g[i].push_back(L[i]); } if(R[i] != -1) { g[i].push_back(R[i]); } } } int minimum_jumps(int A, int B, int C, int D) { if(A != B || C != D) { queue<int> q; vector<int> dist(MAXN, 1e9); for(int i = A; i <= B; i++) { q.push(i); dist[i] = 0; } while(!q.empty()) { int node = q.front(); q.pop(); for(int i = 0; i < g[node].size(); i++) { if(dist[g[node][i]] == 1e9) { dist[g[node][i]] = dist[node] + 1; q.push(g[node][i]); } } } int ans = 1e9; for(int i = C; i <= D; i++) { ans = min(ans, dist[i]); } if(ans == 1e9) { return -1; } return ans; } int ans = 0, cur = B; for(int i = 18; i >= 0; i--) { if(DP_Max[i][cur] != -1 && INPUT[DP_Max[i][cur]] <= INPUT[C]) { //cout << "prev cur = " << cur << "\n"; ans += (1 << i); cur = DP_Max[i][cur]; //cout << "new cur = " << cur << ", with added answer: " << (1 << i) << "\n"; } } for(int i = 18; i >= 0; i--) { if(DP_Min[i][cur] != -1 && INPUT[DP_Min[i][cur]] <= INPUT[C]) { ans += (1 << i); cur = DP_Min[i][cur]; } } if(cur != C) return -1; return ans; }

Compilation message (stderr)

jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:112:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |       for(int i = 0; i < g[node].size(); i++) {
      |                      ~~^~~~~~~~~~~~~~~~
#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...