Submission #386759

#TimeUsernameProblemLanguageResultExecution timeMemory
386759wenqiJakarta Skyscrapers (APIO15_skyscraper)C++14
10 / 100
1097 ms38776 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define all(x) begin(x), end(x) #define fillc(p, t, v) fill_n((t*) p, sizeof(p) / sizeof(t), v) #define pb push_back #define MID 2069 #define O 2069 #define MAX 96900000000ll #define K 9999999999699ll #define OM (O / MID + 69) int N, M; int B[O]; int P[O]; int TP; vector<int> SMALL[O]; vector<int> BIG[O]; ll smallc[O][MID + 69]; bool seen1[O][MID + 69]; ll bigc[O][2 * OM + 69]; ll dp_small(int, int); ll dp_big(int, int); ll dp_small(int pos, int step) { ll& ans = smallc[pos][step]; if(ans != -2) return ans; ans = K; ll a = ans; if(pos == TP) return ans = 0; for(int j : SMALL[pos]) { a = min(a, dp_small(B[j], P[j])); } for(int j : BIG[pos]) { a = min(a, dp_big(j, OM)); } if(pos - step >= 0) { a = min(a, 1 + dp_small(pos - step, step)); } if(pos + step < N) { a = min(a, 1 + dp_small(pos + step, step)); } if(a == K) { ans = -2; return K; }else{ ans = min(ans, MAX); return ans = a; } } ll dp_big(int i, int offset) { ll& ans = bigc[i][offset]; if(ans != -2) return ans; ans = MAX; int pos = B[i] + (offset - OM) * P[i]; if(pos == TP) return ans = 0; for(int j : SMALL[pos]) { ans = min(ans, dp_small(B[j], P[j])); } for(int j : BIG[pos]) { ans = min(ans, dp_big(j, OM)); } if(pos - P[i] >= 0) { ans = min(ans, 1 + dp_big(i, offset - 1)); } if(pos + P[i] < N) { ans = min(ans, 1 + dp_big(i, offset + 1)); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> M; for(int i = 0; i < M; i++) { cin >> B[i] >> P[i]; if(P[i] < MID) SMALL[B[i]].pb(i); else BIG[B[i]].pb(i); if(i == 1) TP = B[i]; } fillc(smallc, ll, -2); fillc(bigc, ll, -2); ll ans; if(P[0] < MID) { ans = dp_small(B[0], P[0]); }else{ ans = dp_big(0, OM); } cout << (ans >= MAX ? -1 : ans) << '\n'; return 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...