제출 #1254099

#제출 시각아이디문제언어결과실행 시간메모리
1254099VKhangJakarta Skyscrapers (APIO15_skyscraper)C++20
0 / 100
15 ms27800 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5, MOD = 1e9 + 7; const long long L = 1e18; #define ll long long #define fi first #define se second #define yes {cout << "YES";return;} #define no {cout << "NO"; return;} #define pb push_back #define MASK(i) (1ll << (i)) #define BIT(i, s) ((1ll << (i)) & (s)) #define all(a) a.begin(), a.end() int n, m; int a[N], b[N]; bool visited[N]; int dist[N]; vector <int> s[N]; priority_queue <pair <int, int>, vector<pair <int, int>>, greater <pair <int, int>>> p; bool Left[N], Right[N]; void solve() { cin >> n >> m; for (int i = 0; i < m; i++){ cin >> a[i] >> b[i]; s[a[i]].pb(i); } memset(dist, 0x3f, sizeof dist); dist[0] = 0; p.push({0, 0}); while(p.size()){ int i = p.top().fi; int j = p.top().se; p.pop(); if (visited[j]) continue; visited[j] = true; if (!Left[j]){ bool ok = true; for (int l = a[j] - b[j]; ok && l >= 0; l -= b[j]){ for (int x: s[l]){ int y = (a[j] - l) / b[j]; dist[x] = min(dist[x], i + y); if (b[x] == b[j]){ Right[x] = true; ok = false; } p.push({dist[x], x}); } } } if (!Right[j]){ bool ok = true; for (int l = a[j] + b[j]; ok && l < n; l += b[j]){ for (int x: s[l]){ int y = (l - a[j]) / b[j]; dist[x] = min(dist[x], i + y); if (b[x] == b[j]){ Left[x] = true; ok = false; } p.push({dist[x], x}); } } } } cout << (dist[1] == dist[n + 1] ? -1 : dist[1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); solve(); 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...