#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = a; i<int(b);++i)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define trav(a,c) for(auto a: c)
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pii;
int main(){
cin.sync_with_stdio(false);
ll n,m;
cin>>n>>m;
vi b(m);
vi p(m);
vector<vi > doges(n);
vector<vi> e;
rep(i,0,m) {
cin>>b[i]>>p[i];
doges[b[i]].push_back(i);
}
priority_queue<pair<ll,ll>,vector<pii>,greater<pii> > q; //(jumps,doge);
q.emplace(0,b[0]);
vector<bool> seen(n);
while(q.size()){
ll pos,jumps;
tie(jumps,pos) = q.top();
q.pop();
//cout<<pos<<" "<<jumps<<": "<<endl;
if(seen[pos]) continue;
seen[pos] = true;
vi mn(n,1e18);
rep(j,0,doges[pos].size()){
if(doges[pos][j]==1){
cout<<jumps<<endl;
_Exit(0);
}
ll pi = p[doges[pos][j]];
rep(i,1,n/pi+1){
if(pos+pi*i<n)
mn[pos+pi*i] = min(mn[pos+pi*i],jumps+i);
if(pos-pi*i>=0)
mn[pos-pi*i] = min(mn[pos-pi*i],jumps+i);
}
}
rep(i,0,n){
if(!seen[i]&&mn[i]<1e18) q.emplace(mn[i],i);
/*ll mn = 1e18;
rep(j,0,doges[pos].size()){
if((i-pos)%p[doges[pos][j]]==0) mn = min(mn,abs(i-pos)/p[doges[pos][j]]);
}
if(mn<1e18)
q.emplace(mn+jumps,i);*/
}
/*
vector<bool> seenHere(n);
trav(doge,doges[pos]){
if(doge==1){
cout<<jumps<<endl;
return 0;
}
//rep(i,0,n)
// if((i-pos)%p[doge]==0&&!seen[i])
// q.emplace(jumps+abs(i-pos)/p[doge],i);
ll pi = p[doge];
rep(i,1,n/pi+1){
//cout<<pos+pi*i<<endl;
if(pos+pi*i<n) {
//if(seenHere[pos+pi*i]) break;
//seenHere[pos+pi*i] = true;
q.emplace(jumps+i,pos+pi*i);
}
if(pos-pi*i>=0) {
//if(seenHere[pos-pi*i]) break;
//seenHere[pos-pi*i] = true;
q.emplace(jumps+i,pos-pi*i);
}
}
}
/*rep(i,0,n){
if((b[i]-b[doge])%p[doge]==0&&!seen[i])
q.emplace(jumps+abs(b[i]-b[doge])/p[doge],i);
}*/
}
cout<<-1<<endl;
}
Compilation message
skyscraper.cpp:98:9: warning: "/*" within comment [-Wcomment]
/*rep(i,0,n){
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
436 KB |
Output is correct |
4 |
Correct |
3 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
616 KB |
Output is correct |
6 |
Correct |
2 ms |
616 KB |
Output is correct |
7 |
Correct |
3 ms |
616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
616 KB |
Output is correct |
2 |
Correct |
2 ms |
700 KB |
Output is correct |
3 |
Correct |
2 ms |
712 KB |
Output is correct |
4 |
Correct |
2 ms |
712 KB |
Output is correct |
5 |
Correct |
3 ms |
712 KB |
Output is correct |
6 |
Correct |
2 ms |
712 KB |
Output is correct |
7 |
Correct |
2 ms |
712 KB |
Output is correct |
8 |
Correct |
2 ms |
712 KB |
Output is correct |
9 |
Correct |
2 ms |
712 KB |
Output is correct |
10 |
Correct |
2 ms |
712 KB |
Output is correct |
11 |
Correct |
3 ms |
712 KB |
Output is correct |
12 |
Correct |
3 ms |
712 KB |
Output is correct |
13 |
Correct |
4 ms |
764 KB |
Output is correct |
14 |
Correct |
4 ms |
764 KB |
Output is correct |
15 |
Correct |
3 ms |
764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
764 KB |
Output is correct |
2 |
Correct |
3 ms |
764 KB |
Output is correct |
3 |
Correct |
3 ms |
764 KB |
Output is correct |
4 |
Correct |
3 ms |
764 KB |
Output is correct |
5 |
Correct |
2 ms |
764 KB |
Output is correct |
6 |
Correct |
2 ms |
764 KB |
Output is correct |
7 |
Correct |
2 ms |
764 KB |
Output is correct |
8 |
Correct |
2 ms |
764 KB |
Output is correct |
9 |
Correct |
3 ms |
764 KB |
Output is correct |
10 |
Correct |
3 ms |
764 KB |
Output is correct |
11 |
Correct |
4 ms |
764 KB |
Output is correct |
12 |
Correct |
3 ms |
764 KB |
Output is correct |
13 |
Correct |
4 ms |
764 KB |
Output is correct |
14 |
Correct |
4 ms |
840 KB |
Output is correct |
15 |
Correct |
4 ms |
840 KB |
Output is correct |
16 |
Correct |
3 ms |
840 KB |
Output is correct |
17 |
Correct |
6 ms |
892 KB |
Output is correct |
18 |
Correct |
3 ms |
892 KB |
Output is correct |
19 |
Correct |
3 ms |
892 KB |
Output is correct |
20 |
Correct |
587 ms |
17220 KB |
Output is correct |
21 |
Correct |
4 ms |
17220 KB |
Output is correct |
22 |
Correct |
4 ms |
17220 KB |
Output is correct |
23 |
Correct |
12 ms |
17220 KB |
Output is correct |
24 |
Correct |
18 ms |
17220 KB |
Output is correct |
25 |
Correct |
5 ms |
17220 KB |
Output is correct |
26 |
Correct |
13 ms |
17220 KB |
Output is correct |
27 |
Correct |
15 ms |
17220 KB |
Output is correct |
28 |
Correct |
17 ms |
17220 KB |
Output is correct |
29 |
Correct |
20 ms |
17220 KB |
Output is correct |
30 |
Correct |
22 ms |
17220 KB |
Output is correct |
31 |
Correct |
20 ms |
17220 KB |
Output is correct |
32 |
Correct |
27 ms |
17220 KB |
Output is correct |
33 |
Correct |
40 ms |
17220 KB |
Output is correct |
34 |
Correct |
28 ms |
17220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
17220 KB |
Output is correct |
2 |
Correct |
3 ms |
17220 KB |
Output is correct |
3 |
Correct |
3 ms |
17220 KB |
Output is correct |
4 |
Correct |
2 ms |
17220 KB |
Output is correct |
5 |
Correct |
4 ms |
17220 KB |
Output is correct |
6 |
Correct |
3 ms |
17220 KB |
Output is correct |
7 |
Correct |
3 ms |
17220 KB |
Output is correct |
8 |
Correct |
2 ms |
17220 KB |
Output is correct |
9 |
Correct |
3 ms |
17220 KB |
Output is correct |
10 |
Correct |
3 ms |
17220 KB |
Output is correct |
11 |
Correct |
4 ms |
17220 KB |
Output is correct |
12 |
Correct |
4 ms |
17220 KB |
Output is correct |
13 |
Correct |
4 ms |
17220 KB |
Output is correct |
14 |
Correct |
4 ms |
17220 KB |
Output is correct |
15 |
Correct |
4 ms |
17220 KB |
Output is correct |
16 |
Correct |
3 ms |
17220 KB |
Output is correct |
17 |
Correct |
6 ms |
17220 KB |
Output is correct |
18 |
Correct |
4 ms |
17220 KB |
Output is correct |
19 |
Correct |
3 ms |
17220 KB |
Output is correct |
20 |
Correct |
625 ms |
17224 KB |
Output is correct |
21 |
Correct |
4 ms |
17224 KB |
Output is correct |
22 |
Correct |
3 ms |
17224 KB |
Output is correct |
23 |
Correct |
14 ms |
17224 KB |
Output is correct |
24 |
Correct |
17 ms |
17224 KB |
Output is correct |
25 |
Correct |
6 ms |
17224 KB |
Output is correct |
26 |
Correct |
19 ms |
17224 KB |
Output is correct |
27 |
Correct |
19 ms |
17224 KB |
Output is correct |
28 |
Correct |
28 ms |
17224 KB |
Output is correct |
29 |
Correct |
25 ms |
17224 KB |
Output is correct |
30 |
Correct |
18 ms |
17224 KB |
Output is correct |
31 |
Correct |
19 ms |
17224 KB |
Output is correct |
32 |
Correct |
17 ms |
17224 KB |
Output is correct |
33 |
Correct |
26 ms |
17224 KB |
Output is correct |
34 |
Correct |
26 ms |
17224 KB |
Output is correct |
35 |
Correct |
27 ms |
17224 KB |
Output is correct |
36 |
Correct |
9 ms |
17224 KB |
Output is correct |
37 |
Correct |
38 ms |
17224 KB |
Output is correct |
38 |
Correct |
27 ms |
17224 KB |
Output is correct |
39 |
Correct |
13 ms |
17224 KB |
Output is correct |
40 |
Correct |
15 ms |
17224 KB |
Output is correct |
41 |
Correct |
18 ms |
17224 KB |
Output is correct |
42 |
Correct |
23 ms |
17224 KB |
Output is correct |
43 |
Correct |
22 ms |
17224 KB |
Output is correct |
44 |
Correct |
801 ms |
17992 KB |
Output is correct |
45 |
Correct |
94 ms |
17992 KB |
Output is correct |
46 |
Correct |
81 ms |
17992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
17992 KB |
Output is correct |
2 |
Correct |
2 ms |
17992 KB |
Output is correct |
3 |
Correct |
3 ms |
17992 KB |
Output is correct |
4 |
Correct |
2 ms |
17992 KB |
Output is correct |
5 |
Correct |
3 ms |
17992 KB |
Output is correct |
6 |
Correct |
3 ms |
17992 KB |
Output is correct |
7 |
Correct |
3 ms |
17992 KB |
Output is correct |
8 |
Correct |
2 ms |
17992 KB |
Output is correct |
9 |
Correct |
3 ms |
17992 KB |
Output is correct |
10 |
Correct |
3 ms |
17992 KB |
Output is correct |
11 |
Correct |
5 ms |
17992 KB |
Output is correct |
12 |
Correct |
4 ms |
17992 KB |
Output is correct |
13 |
Correct |
4 ms |
17992 KB |
Output is correct |
14 |
Correct |
4 ms |
17992 KB |
Output is correct |
15 |
Correct |
4 ms |
17992 KB |
Output is correct |
16 |
Correct |
3 ms |
17992 KB |
Output is correct |
17 |
Correct |
6 ms |
17992 KB |
Output is correct |
18 |
Correct |
3 ms |
17992 KB |
Output is correct |
19 |
Correct |
3 ms |
17992 KB |
Output is correct |
20 |
Correct |
671 ms |
17992 KB |
Output is correct |
21 |
Correct |
3 ms |
17992 KB |
Output is correct |
22 |
Correct |
3 ms |
17992 KB |
Output is correct |
23 |
Correct |
18 ms |
17992 KB |
Output is correct |
24 |
Correct |
19 ms |
17992 KB |
Output is correct |
25 |
Correct |
6 ms |
17992 KB |
Output is correct |
26 |
Correct |
14 ms |
17992 KB |
Output is correct |
27 |
Correct |
17 ms |
17992 KB |
Output is correct |
28 |
Correct |
25 ms |
17992 KB |
Output is correct |
29 |
Correct |
30 ms |
17992 KB |
Output is correct |
30 |
Correct |
19 ms |
17992 KB |
Output is correct |
31 |
Correct |
21 ms |
17992 KB |
Output is correct |
32 |
Correct |
21 ms |
17992 KB |
Output is correct |
33 |
Correct |
28 ms |
17992 KB |
Output is correct |
34 |
Correct |
32 ms |
17992 KB |
Output is correct |
35 |
Correct |
29 ms |
17992 KB |
Output is correct |
36 |
Correct |
5 ms |
17992 KB |
Output is correct |
37 |
Correct |
33 ms |
17992 KB |
Output is correct |
38 |
Correct |
24 ms |
17992 KB |
Output is correct |
39 |
Correct |
13 ms |
17992 KB |
Output is correct |
40 |
Correct |
13 ms |
17992 KB |
Output is correct |
41 |
Correct |
17 ms |
17992 KB |
Output is correct |
42 |
Correct |
23 ms |
17992 KB |
Output is correct |
43 |
Correct |
23 ms |
17992 KB |
Output is correct |
44 |
Correct |
840 ms |
18012 KB |
Output is correct |
45 |
Correct |
103 ms |
18012 KB |
Output is correct |
46 |
Correct |
115 ms |
18012 KB |
Output is correct |
47 |
Correct |
403 ms |
18012 KB |
Output is correct |
48 |
Correct |
17 ms |
18012 KB |
Output is correct |
49 |
Correct |
18 ms |
18012 KB |
Output is correct |
50 |
Correct |
14 ms |
18012 KB |
Output is correct |
51 |
Execution timed out |
1027 ms |
18012 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |