제출 #386660

#제출 시각아이디문제언어결과실행 시간메모리
386660wenqiJakarta Skyscrapers (APIO15_skyscraper)C++14
22 / 100
66 ms96108 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 169 #define O 30069 #define MAX 30000000 #define OM (O / MID + 69) int N, M; int B[O]; int P[O]; int TP; vector<int> SMALL[O]; vector<int> BIG[O]; int smallc[O][MID + 69]; int bigc[O][2 * OM + 69]; int dp_small(int, int); int dp_big(int, int); int dp_small(int pos, int step) { int& ans = smallc[pos][step]; if(ans != -2) return ans; ans = MAX; 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 - step >= 0) { ans = min(ans, 1 + dp_small(pos - step, step)); } if(pos + step < N) { ans = min(ans, 1 + dp_small(pos + step, step)); } return ans; } int dp_big(int i, int offset) { int& ans = bigc[i][offset]; if(ans != -2) return ans; ans = MAX; if(i == 1) return ans = 0; int pos = B[i] + (offset - OM) * P[i]; 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, int, -2); fillc(bigc, int, -2); int 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...