/**
* Prof.Nicola
**/
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define mp make_pair
#define F first
#define S second
#define REP(i,l,r) for(int i=(l);i<(r);i++)
#define PER(i,l,r) for(int i=(r)-1;i>=(l);i--)
#define bitl __builtin_clz
#define bitr __builtin_ctz
#define bits __builtin_popcount
const long dx[4]={1,0,-1,0},dz[4]={0,1,0,-1};
const long double pi=3.14159265359;
const long long mod=1e9+7;
long long p(long long x){while(x&(x-1)){x=x&(x-1);}return x;}
long long squared(long long x){return (x*x)%mod;}
long long power(long long x,long long p){if(p==0){return 1;}if(p%2==1){return (power(x,p-1)*x)%mod;}return squared(power(x,p/2));}
long long inv(long long x){return power(x,mod-2);}
long long log(long long x,long long z){if(x<z){return 0;}return 1+log(x/z,z);}
template<class T>void re(T&x){cin>>x;}
template<class T1,class T2> void re(pair<T1,T2>&x){re(x.first);re(x.second);}
template<class T>void re(vector<T>&x){for(long i=0;i<x.size();i++){re(x[i]);}}
template<class T>void re(deque<T>&x){for(long i=0;i<x.size();i++){re(x[i]);}}
template<class T>void out(T x){cout<<x<<" ";}
template<class T1,class T2>void out(pair<T1,T2>x){out(x.first);out(x.second);cout<<endl;}
template<class T>void out(vector<T>x,long l=0,long r=0){if(!r){r=x.size();}for(long i=l;i<r;i++){out(x[i]);}cout<<endl;}
template<class T>void out(deque<T>x,long l=0,long r=0){if(!r){r=x.size();}for(long i=l;i<r;i++){out(x[i]);}cout<<endl;}
template<class T>void out(set<T>x){while(!x.empty()){out(*x.begin());x.erase(*x.begin());}cout<<endl;}
template<class T1,class T2>void out(map<T1,T2>x){while(!x.empty()){out(*x.begin());x.erase(x.begin()->first);}cout<<endl;}
template<class T>void out(queue<T>x){while(!x.empty()){out(x.front());x.pop();}cout<<endl;}
template<class T>void out(priority_queue<T>x){while(!x.empty()){out(x.top());x.pop();}cout<<endl;}
template<class T>void out(stack<T>x){while(!x.empty()){out(x.top());x.pop();}cout<<endl;}
template<class T>T cross(complex<T>x,complex<T>z){return (conj(x)*z).imag();}
template<class T>T dot(complex<T>x,complex<T>z){return (conj(x)*z).real();}
set<long long>::iterator T;
long long vLE(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>x){return -1;}long long z=p(r-l);while(z){if(l+z<=r&&VT[l+z]<=x){l+=z;}z/=2;}return l;}
long long vL(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>=x){return -1;}long long z=p(r-l);while(z){if(l+z<=r&&VT[l+z]<x){l+=z;}z/=2;}return l;}
long long vGE(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>=x){return l;}l=vL(x,VT,l,r);if(l==r){return -1;}return l+1;}
long long vG(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>x){return l;}l=vLE(x,VT,l,r);if(l==r){return -1;}return l+1;}
long long sLE(long long x,set<long long>&ST){if(ST.count(x)){return x;}if(*ST.begin()>x){return -1;}ST.insert(x);T=ST.find(x);T--;ST.erase(x);return *T;}
long long sL(long long x,set<long long>&ST){if(*ST.begin()>=x){return -1;}if(ST.count(x)){T=ST.find(x);T--;return *T;}ST.insert(x);T=ST.find(x);T--;ST.erase(x);return *T;}
long long sGE(long long x,set<long long>&ST){if(ST.count(x)){return x;}if(*ST.rbegin()<x){return -1;}ST.insert(x);T=ST.find(x);T++;ST.erase(x);return *T;}
long long sG(long long x,set<long long>&ST){if(*ST.rbegin()<=x){return -1;}if(ST.count(x)){T=ST.find(x);T++;return *T;}ST.insert(x);T=ST.find(x);T++;ST.erase(x);return *T;}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m,s,cost,pos,jump,ans,inf=1e9;
cin>>n>>m;
s=sqrt(n)+1;
vector<pair<int,int> >v(m);
vector<bool>ok(n);
re(v);
vector<vector<int> >possible(n),cnt(n),l(n),r(n);
vector<vector<bool> >vis(n);
vector<int>path(n,inf);
REP(i,0,m){
if(v[i].S>s){
for(int j=v[i].F%v[i].S;j<n;j+=v[i].S){
cnt[j].push_back(v[i].S);
if(j+v[i].S<n){
r[j].push_back(cnt[j+v[i].S].size());
}
else{
r[j].push_back(0);
}
if(j-v[i].S>=0){
l[j].push_back(cnt[j-v[i].S].size()-1);
}
else{
l[j].push_back(0);
}
}
possible[v[i].F].push_back(cnt[v[i].F].size()+s);
continue;
}
possible[v[i].F].push_back(v[i].S);
}
REP(i,0,n){
vis[i].resize(s+1+cnt[i].size());
}
deque<pair<int,pair<int,int> > >pq;
pq.push_back(mp(0,mp(v[0].F,possible[v[0].F][0])));
while(!pq.empty()){
cost=pq.front().F;
pos=pq.front().S.F;
jump=pq.front().S.S;
pq.pop_front();
if(vis[pos][jump]){
continue;
}
vis[pos][jump]=true;
if(ok[pos]){
if(jump<=s){
if(pos+jump<n){
pq.push_back(mp(cost+1,mp(pos+jump,jump)));
}
if(pos-jump>=0){
pq.push_back(mp(cost+1,mp(pos-jump,jump)));
}
continue;
}
if(pos+cnt[pos][jump-s-1]<n){
pq.push_back(mp(cost+1,mp(pos+cnt[pos][jump-s-1],r[pos][jump-s-1]+s+1)));
}
if(pos-cnt[pos][jump-s-1]>=0){
pq.push_back(mp(cost+1,mp(pos-cnt[pos][jump-s-1],l[pos][jump-s-1]+s+1)));
}
continue;
}
path[pos]=cost;
if(jump<=s){
if(pos+jump<n){
pq.push_back(mp(cost+1,mp(pos+jump,jump)));
}
if(pos-jump>=0){
pq.push_back(mp(cost+1,mp(pos-jump,jump)));
}
}
else{
if(pos+cnt[pos][jump-s-1]<n){
pq.push_back(mp(cost+1,mp(pos+cnt[pos][jump-s-1],r[pos][jump-s-1]+s+1)));
}
if(pos-cnt[pos][jump-s-1]>=0){
pq.push_back(mp(cost+1,mp(pos-cnt[pos][jump-s-1],l[pos][jump-s-1]+s+1)));
}
}
ok[pos]=true;
REP(i,0,possible[pos].size()){
pq.push_front(mp(cost,mp(pos,possible[pos][i])));
}
}
ans=path[v[1].F];
if(ans==inf){
cout<<-1<<endl;
return 0;
}
cout<<ans<<endl;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:11:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | #define REP(i,l,r) for(int i=(l);i<(r);i++)
| ^
skyscraper.cpp:133:9: note: in expansion of macro 'REP'
133 | REP(i,0,possible[pos].size()){
| ^~~
skyscraper.cpp: In instantiation of 'void re(std::vector<_Tp>&) [with T = std::pair<int, int>]':
skyscraper.cpp:57:9: required from here
skyscraper.cpp:26:53: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | template<class T>void re(vector<T>&x){for(long i=0;i<x.size();i++){re(x[i]);}}
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
260 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
3 ms |
852 KB |
Output is correct |
25 |
Correct |
2 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
792 KB |
Output is correct |
28 |
Correct |
3 ms |
980 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
980 KB |
Output is correct |
33 |
Correct |
8 ms |
1616 KB |
Output is correct |
34 |
Correct |
7 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
852 KB |
Output is correct |
25 |
Correct |
2 ms |
852 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
3 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
980 KB |
Output is correct |
33 |
Correct |
9 ms |
1492 KB |
Output is correct |
34 |
Correct |
7 ms |
1620 KB |
Output is correct |
35 |
Correct |
13 ms |
2576 KB |
Output is correct |
36 |
Correct |
3 ms |
852 KB |
Output is correct |
37 |
Correct |
18 ms |
4052 KB |
Output is correct |
38 |
Correct |
20 ms |
3640 KB |
Output is correct |
39 |
Correct |
19 ms |
3668 KB |
Output is correct |
40 |
Correct |
19 ms |
3796 KB |
Output is correct |
41 |
Correct |
22 ms |
3748 KB |
Output is correct |
42 |
Correct |
7 ms |
1492 KB |
Output is correct |
43 |
Correct |
6 ms |
1476 KB |
Output is correct |
44 |
Correct |
4 ms |
1364 KB |
Output is correct |
45 |
Correct |
42 ms |
8020 KB |
Output is correct |
46 |
Correct |
40 ms |
8020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
3 ms |
852 KB |
Output is correct |
25 |
Correct |
2 ms |
852 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
3 ms |
980 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
5 ms |
980 KB |
Output is correct |
33 |
Correct |
8 ms |
1492 KB |
Output is correct |
34 |
Correct |
8 ms |
1620 KB |
Output is correct |
35 |
Correct |
14 ms |
2580 KB |
Output is correct |
36 |
Correct |
3 ms |
852 KB |
Output is correct |
37 |
Correct |
19 ms |
4052 KB |
Output is correct |
38 |
Correct |
20 ms |
3664 KB |
Output is correct |
39 |
Correct |
19 ms |
3740 KB |
Output is correct |
40 |
Correct |
20 ms |
3796 KB |
Output is correct |
41 |
Correct |
19 ms |
3768 KB |
Output is correct |
42 |
Correct |
5 ms |
1492 KB |
Output is correct |
43 |
Correct |
6 ms |
1364 KB |
Output is correct |
44 |
Correct |
5 ms |
1364 KB |
Output is correct |
45 |
Correct |
44 ms |
8020 KB |
Output is correct |
46 |
Correct |
57 ms |
8052 KB |
Output is correct |
47 |
Correct |
97 ms |
11648 KB |
Output is correct |
48 |
Correct |
28 ms |
7724 KB |
Output is correct |
49 |
Correct |
25 ms |
7904 KB |
Output is correct |
50 |
Correct |
21 ms |
8020 KB |
Output is correct |
51 |
Correct |
98 ms |
10268 KB |
Output is correct |
52 |
Correct |
70 ms |
10312 KB |
Output is correct |
53 |
Correct |
27 ms |
8036 KB |
Output is correct |
54 |
Correct |
5 ms |
5332 KB |
Output is correct |
55 |
Correct |
6 ms |
5332 KB |
Output is correct |
56 |
Correct |
11 ms |
6484 KB |
Output is correct |
57 |
Correct |
22 ms |
5460 KB |
Output is correct |
58 |
Correct |
9 ms |
6100 KB |
Output is correct |
59 |
Correct |
12 ms |
6100 KB |
Output is correct |
60 |
Correct |
19 ms |
6980 KB |
Output is correct |
61 |
Correct |
18 ms |
7304 KB |
Output is correct |
62 |
Correct |
102 ms |
11604 KB |
Output is correct |
63 |
Correct |
641 ms |
54248 KB |
Output is correct |
64 |
Correct |
638 ms |
55960 KB |
Output is correct |
65 |
Correct |
706 ms |
56324 KB |
Output is correct |
66 |
Correct |
666 ms |
56408 KB |
Output is correct |
67 |
Correct |
688 ms |
56320 KB |
Output is correct |