Submission #462326

#TimeUsernameProblemLanguageResultExecution timeMemory
462326prvocisloJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
530 ms262148 KiB
#include <iostream> #include <vector> #include <algorithm> #include <queue> typedef long long ll; using namespace std; const int sqrtn = 180, inf = 1e9 + 5; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; int k = sqrtn * n; vector<vector<pair<int, int> > > g(k); vector<int> b(m), p(m); for (int i = 0; i < m; i++) { cin >> b[i] >> p[i]; if (p[i] < sqrtn) { g[b[i]].push_back({ p[i] * n + b[i], 0 }); } else { for (int j = b[i] % p[i]; j < n; j += p[i]) { g[b[i]].push_back({ j, abs(b[i] - j) / p[i] }); } } } for (int step = 1; step < sqrtn; step++) for (int i = 0; i < n; i++) { if (i - step >= 0) { g[step * n + i].push_back({ step * n + i - step, 1 }); g[step * n + i].push_back({ i - step, 1 }); } if (i + step < n) { g[step * n + i].push_back({ step * n + i + step, 1 }); g[step * n + i].push_back({ i + step, 1 }); } } //cout << "\n"; //for (int i = 0; i < k; i++) for (pair<int, int> j : g[i]) cout << i << " " << j.first << " " << j.second << "\n"; priority_queue<pair<int, int> > pq; pq.push({ 0, b[0] }); vector<int> dist(k, inf); while (!pq.empty()) { int u = pq.top().second, d = -pq.top().first; //cout << u << endl; pq.pop(); if (dist[u] != inf) continue; dist[u] = d; for (pair<int, int> i : g[u]) if (dist[i.first] == inf) { pq.push({ -(d + i.second), i.first }); } } cout << (dist[b[1]] == inf ? -1 : dist[b[1]]) << "\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...