Submission #699856

#TimeUsernameProblemLanguageResultExecution timeMemory
699856happypotatoJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
335 ms262144 KiB
#include <bits/stdc++.h> using namespace std; const int mxN = 3e4 + 1; pair<int, int> doge[mxN]; queue<int> jump[mxN]; map<int, int> dist[mxN]; vector<queue<pair<int, int>>> q; int n; void PushElement(pair<int, pair<int, int>> &nxt) { if ((int)(q.size()) <= nxt.first) { q.resize(nxt.first + 1); } q[nxt.first].push(nxt.second); } void AddElement(pair<int, pair<int, int>> &nxt) { if (!(0 <= nxt.second.first && nxt.second.first < n)) return; if (dist[nxt.second.first].find(nxt.second.second) == dist[nxt.second.first].end()) { PushElement(nxt); dist[nxt.second.first][nxt.second.second] = nxt.first; } else if (dist[nxt.second.first][nxt.second.second] > nxt.first) { PushElement(nxt); dist[nxt.second.first][nxt.second.second] = nxt.first; } } void solve() { int m; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> doge[i].first >> doge[i].second; if (i != 0) jump[doge[i].first].push(doge[i].second); } pair<int, pair<int, int>> nxt; nxt = {0, doge[0]}; AddElement(nxt); for (int curdist = 0; curdist < (int)(q.size()); curdist++) { while (!q[curdist].empty()) { pair<int, int> cur = q[curdist].front(); q[curdist].pop(); if (dist[cur.first][cur.second] < curdist) continue; if (cur.first == doge[1].first) { cout << curdist << endl; return; } while (!jump[cur.first].empty()) { int delta = jump[cur.first].front(); jump[cur.first].pop(); nxt = {curdist, {cur.first, delta}}; AddElement(nxt); } nxt = {curdist + 1, {cur.first - cur.second, cur.second}}; AddElement(nxt); nxt = {curdist + 1, {cur.first + cur.second, cur.second}}; AddElement(nxt); } } cout << "-1\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); 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...