Submission #959578

#TimeUsernameProblemLanguageResultExecution timeMemory
959578BlagojJakarta Skyscrapers (APIO15_skyscraper)C++17
10 / 100
2 ms2292 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define ll long long #define all(x) (x).begin(), (x).end() const int mxn = 30010; int n, m; unordered_map<int, ll> best[mxn]; int spot[mxn], jump[mxn], val[mxn]; int dx[] = {-1, 1}; void solve() { priority_queue<pair<ll, pair<int, int>>> pq; pq.push({0, {spot[0], jump[0]}}); best[spot[0]][jump[0]] = 0; while (pq.size()) { ll dist = -pq.top().first, pos = pq.top().second.first, pwr = pq.top().second.second; pq.pop(); if (dist > best[pos][pwr]) continue; if (val[pos] != -1) { if (!best[pos].count(jump[val[pos]])) best[pos][jump[val[pos]]] = 1e18; if (dist < best[pos][jump[val[pos]]]) { best[pos][jump[val[pos]]] = dist; pq.push({-dist, {pos, jump[val[pos]]}}); } } for (int d = 0; d < 2; d++) { int newPos = pos + pwr * dx[d]; if (0 <= newPos && newPos < n) { if (!best[newPos].count(pwr)) best[newPos][pwr] = 1e18; if (dist + 1 < best[newPos][pwr]) { best[newPos][pwr] = dist + 1; pq.push({-best[newPos][pwr], {newPos, pwr}}); } } } } ll ans = 1e18; for (auto x : best[spot[1]]) ans = min(ans, x.second); cout << (ans != 1e18 ? ans : -1); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); memset(val, -1, sizeof(val)); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> spot[i] >> jump[i]; val[spot[i]] = i; } solve(); }
#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...