Submission #972403

#TimeUsernameProblemLanguageResultExecution timeMemory
972403Halym2007Jakarta Skyscrapers (APIO15_skyscraper)C++17
36 / 100
248 ms262144 KiB
#include "bits/stdc++.h" #define ll long long int #define pb push_back #define pii pair<ll,ll> #define ff first #define ss second #define sz size() const int N = 3e4 + 5; using namespace std; ll n, m, b[N], p[N]; vector <pii> v[N]; priority_queue <pii> pq; vector <ll> dis(N,1e9); map <pii,ll> mp; void f(){ pq.push({0,b[1]}); dis[b[1]] = 0; while(!pq.empty()){ int a = pq.top().ss; pq.pop(); for(auto i: v[a]){ if(dis[a] + i.ss < dis[i.ff]){ dis[i.ff] = dis[a] + i.ss; pq.push({-dis[i.ff],i.ff}); } } } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); //freopen("input.in", "r", stdin); //freopen("output.out", "w", stdout); cin >> n >> m; for(int i = 1; i <= m; i++){ cin >> b[i] >> p[i]; mp[{b[i],p[i]}]++; } for(int i = 1; i <= m; i++){ ll nd = b[i], cnt = 0; while(1){ nd -= p[i]; if(nd < 0) break; cnt++; v[b[i]].pb({nd,cnt}); if(mp.find({nd,p[i]}) != mp.end()) break; } nd = b[i], cnt = 0; while(1){ nd += p[i]; if(nd > n-1) break; cnt++; v[b[i]].pb({nd,cnt}); if(mp.find({nd,p[i]}) != mp.end()) break; } if(mp[{b[i],p[i]}] > 1) mp[{b[i],p[i]}]--; } f(); cout << (dis[b[2]] == 1e9 ? -1 : dis[b[2]]); }
#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...