Submission #1037650

#TimeUsernameProblemLanguageResultExecution timeMemory
1037650MuhammetJakarta Skyscrapers (APIO15_skyscraper)C++17
0 / 100
1 ms604 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long int #define sz(x) (int)x.size() #define ff first #define ss second int T, n, m; vector <int> v1; set <int> v; int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; vector <int> dis(n+1,1e9), c(m,0); vector <pair<int,int>> a(m); for(int i = 0; i < m; i++){ cin >> a[i].ff >> a[i].ss; if(a[i].ff != a[0].ff){ v.insert(a[i].ff); } } int ind = a[0].ff, p = a[1].ff; sort (a.begin(),a.end()); c[a[0].ff] = 0; for(int i = 1; i < m; i++){ if(a[i].ff != a[i-1].ff) c[a[i].ff] = i; } dis[ind] = 0; for(int i = 0; i < m; i++){ if(a[i].ff == ind){ if(sz(v1) > 0 and a[i].ss == v1.back()) continue; v1.push_back(a[i].ss); } } while(sz(v) > 0){ int mn = 1e9, k = -1; for(auto i : v1){ for(auto j : v){ if(dis[j] <= mn){ mn = dis[j]; k = j; } if(abs(j-ind) % i != 0) continue; dis[j] = min(dis[j],dis[ind]+abs(j-ind)/i); if(dis[j] <= mn){ mn = dis[j]; k = j; } } } v1.clear(); int x = c[k]; ind = k; for(int i = x; i < m; i++){ if(a[i].ff == ind){ if(sz(v1) > 0 and a[i].ss == v1.back()) continue; v1.push_back(a[i].ss); } else break; } v.erase(v.find(ind)); if(ind == p) break; } if(dis[p] == 1e9) dis[p] = -1; cout << dis[p]; 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...