This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
using namespace std;
ll n,m,i,j,dist[30005],b[30005],p[30005],x,y;
vector<pair<ll,ll>> v[30005];
bitset<30005> vis;
priority_queue<pair<ll,ll>> q;
int main(){
cin >> n >> m;
for(i=0;i<m;i++){
cin >> b[i] >> p[i];
x=b[i]+p[i];
y=0;
while(x<n){
y++;
v[b[i]].push_back({x,y});
x+=p[i];
}
x=b[i]-p[i];
y=0;
while(x>=0){
y++;
v[b[i]].push_back({x,y});
x-=p[i];
}
}
q.push({0,b[0]});
while(!q.empty()){
x=q.top().ss;
y=q.top().ff;
q.pop();
if(vis[x]) continue;
dist[x]=-y;
vis[x]=1;
for(i=0;i<v[x].size();i++){
if(!vis[v[x][i].ff]){
q.push({y-v[x][i].ss,v[x][i].ff});
}
}
}
if(vis[b[1]]) cout << dist[b[1]];
else cout << -1;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:37:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |