Submission #568531

#TimeUsernameProblemLanguageResultExecution timeMemory
568531Ooops_sorryJakarta Skyscrapers (APIO15_skyscraper)C++14
0 / 100
128 ms262144 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define ld double #define ll long long mt19937 rnd(51); const int N = 30010, K = 30, INF = 1e9; int d[N][K]; deque<pair<int,int>> arr[N * K]; signed main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif // LOCAL ios_base::sync_with_stdio(0); cin.tie(0); for (int i = 0; i < N; i++) { for (int j = 0; j < K; j++) { d[i][j] = INF; } } int n, m; cin >> n >> m; vector<int> b(m), p(m), ans(n, INF); vector<vector<int>> need(n); for (int i = 0; i < m; i++) { cin >> b[i] >> p[i]; need[b[i]].pb(abs(p[i])); } ans[b[0]] = 0; arr[0].pb({b[0], -1}); for (int i = 0; i < N * K; i++) { while (arr[i].size() > 0) { int v = arr[i][0].first, j = arr[i][0].second; arr[i].pop_front(); if (j == -1) { if (ans[v] != i) continue; } else if (d[v][j] != i) { continue; } if (j == -1) { for (auto to : need[v]) { if (to < K) { if (d[v][to] > ans[v]) { d[v][to] = ans[v]; arr[i].pb({v, to}); } } else { for (int k = v, add = 0; k < n; k += to, add++) { if (ans[k] > ans[v] + add) { ans[k] = ans[v] + add; if (ans[k] < N * K) arr[ans[k]].pb({k, -1}); } } for (int k = v, add = 0; k >= 0; k -= to, add++) { if (ans[k] > ans[v] + add) { ans[k] = ans[v] + add; if (ans[k] < N * K) arr[ans[k]].pb({k, -1}); } } } } } else { if (d[v][j] < ans[v]) { ans[v] = d[v][j]; arr[i].pb({v, -1}); } if (v + j < n && d[v + j][j] > d[v][j] + 1) { d[v + j][j] = d[v][j] + 1; if (d[v + j][j] < N * K) arr[d[v + j][j]].pb({v + j, j}); } if (v - j >= 0 && d[v - j][j] > d[v][j] + 1) { d[v - j][j] = d[v][j] + 1; if (d[v - j][j] < N * K) arr[d[v - j][j]].pb({v - j, j}); } } } } cout << (ans[b[1]] == INF ? -1 : ans[b[1]]) << endl; 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...