Submission #710904

#TimeUsernameProblemLanguageResultExecution timeMemory
710904JJAnawatJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
814 ms41404 KiB
#include<bits/stdc++.h> using namespace std; using ti = tuple<int,int,int>; const int inf=1e9; int n,m; int dis[30005][305]; vector<int> bg[30005],sm[30005]; int b[30005],p[30005]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; int sq=sqrt(n); for(int i=0;i<m;i++){ cin >> b[i] >> p[i]; if(p[i]<sq) sm[b[i]].push_back(p[i]); else bg[b[i]].push_back(p[i]); } for(int i=0;i<n;i++) for(int j=0;j<sq;j++) dis[i][j]=inf; priority_queue<ti,vector<ti>,greater<ti>> pq; dis[b[0]][0]=0; pq.push({0,b[0],0}); while(!pq.empty()){ auto [du,u,pw]=pq.top(); pq.pop(); if(du!=dis[u][pw]) continue; if(pw){ if(u-pw>=0){ if(du+1<dis[u-pw][pw]){ dis[u-pw][pw]=du+1; pq.push({du+1,u-pw,pw}); } if(du+1<dis[u-pw][0]){ dis[u-pw][0]=du+1; pq.push({du+1,u-pw,0}); } } if(u+pw<n){ if(du+1<dis[u+pw][pw]){ dis[u+pw][pw]=du+1; pq.push({du+1,u+pw,pw}); } if(du+1<dis[u+pw][0]){ dis[u+pw][0]=du+1; pq.push({du+1,u+pw,0}); } } } else{ for(auto pl:bg[u]){ for(int v=u+pl,cnt=1;v<n;v+=pl,cnt++){ if(dis[v][0]>du+cnt){ dis[v][0]=du+cnt; pq.push({du+cnt,v,0}); } } for(int v=u-pl,cnt=1;v>=0;v-=pl,cnt++){ if(dis[v][0]>du+cnt){ dis[v][0]=du+cnt; pq.push({du+cnt,v,0}); } } } for(auto pl:sm[u]){ if(dis[u][pl]>du){ dis[u][pl]=du; pq.push({du,u,pl}); } } } } int mn=inf; for(int i=0;i<sq;i++) mn=min(mn,dis[b[1]][i]); if(mn==inf) mn=-1; cout << mn; }
#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...