#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
vector<pair<int,int> > graf[N];
pair<int,int>w[N];
int l,num[N];
vi li[300];
bitset<N>bb;
bitset<N>vis;
vi kol[N*2];
int n,m;
void single(int x){
for(int i=1;i<=x;i++){
num[i]=++l;
graf[l].push_back({i,0});
}
for(int i=x+1;i<=n;i++){
num[i]=++l;
graf[l].push_back({i,0}),graf[l].push_back({num[i-x],1});
}
for(int i=1;i<=n;i++) bb[i]=0;
for(auto u:li[x]) bb[w[u].fi]=1;
for(int i=1;i<=n;i++) if(bb[i]) graf[i].push_back({num[i],0});
for(int i=n;i>=n-x+1;i--){
num[i]=++l;
graf[l].push_back({i,0});
}
for(int i=n-x;i>=1;i--){
num[i]=++l;
graf[l].push_back({i,0}),graf[l].push_back({num[i+x],1});
}
for(int i=1;i<=n;i++) if(bb[i]) graf[i].push_back({num[i],0});
}
int Dijkstra(int p,int k){
kol[0].push_back(p);
for(int i=0;i<=(int)1e6;i++){
for(int j=0;j<kol[i].size();j++){
int v=kol[i][j];
if(vis[v]) continue;
vis[v]=1;
if(v==k) return i;
for(auto u:graf[v])
if(!vis[u.fi]) kol[i+u.se].push_back(u.fi);
}
}
return -1;
}
void solve(){
cin>>n>>m;
l=n;
int p=sqrt(n);
if(p*p!=n) p++;
for(int i=1;i<=m;i++){
cin>>w[i].fi>>w[i].se;
w[i].fi++;
if(w[i].se>p){
for(int j=w[i].fi+w[i].se;j<=n;j+=w[i].se) graf[w[i].fi].push_back({j,(j-w[i].fi)/w[i].se});
for(int j=w[i].fi-w[i].se;j>=1;j-=w[i].se) graf[w[i].fi].push_back({j,(w[i].fi-j)/w[i].se});
}else li[w[i].se].push_back(i);
}
for(int i=1;i<=p;i++) single(i);
cout<<Dijkstra(w[1].fi,w[2].fi)<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
Compilation message
skyscraper.cpp: In function 'int Dijkstra(int, int)':
skyscraper.cpp:42:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int j=0;j<kol[i].size();j++){
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
70732 KB |
Output is correct |
2 |
Correct |
35 ms |
70788 KB |
Output is correct |
3 |
Correct |
32 ms |
70676 KB |
Output is correct |
4 |
Correct |
36 ms |
70868 KB |
Output is correct |
5 |
Correct |
36 ms |
70732 KB |
Output is correct |
6 |
Correct |
34 ms |
70692 KB |
Output is correct |
7 |
Correct |
32 ms |
70680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
70732 KB |
Output is correct |
2 |
Correct |
36 ms |
70788 KB |
Output is correct |
3 |
Correct |
35 ms |
70724 KB |
Output is correct |
4 |
Correct |
34 ms |
70728 KB |
Output is correct |
5 |
Correct |
33 ms |
70732 KB |
Output is correct |
6 |
Correct |
33 ms |
70808 KB |
Output is correct |
7 |
Correct |
37 ms |
70788 KB |
Output is correct |
8 |
Correct |
33 ms |
70732 KB |
Output is correct |
9 |
Correct |
32 ms |
70828 KB |
Output is correct |
10 |
Correct |
38 ms |
70860 KB |
Output is correct |
11 |
Correct |
37 ms |
70892 KB |
Output is correct |
12 |
Correct |
36 ms |
70876 KB |
Output is correct |
13 |
Correct |
32 ms |
70848 KB |
Output is correct |
14 |
Correct |
33 ms |
70924 KB |
Output is correct |
15 |
Correct |
35 ms |
70860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
70676 KB |
Output is correct |
2 |
Correct |
35 ms |
70708 KB |
Output is correct |
3 |
Correct |
35 ms |
70792 KB |
Output is correct |
4 |
Correct |
37 ms |
70724 KB |
Output is correct |
5 |
Correct |
36 ms |
70724 KB |
Output is correct |
6 |
Correct |
34 ms |
70784 KB |
Output is correct |
7 |
Correct |
37 ms |
70660 KB |
Output is correct |
8 |
Correct |
36 ms |
70712 KB |
Output is correct |
9 |
Correct |
42 ms |
70732 KB |
Output is correct |
10 |
Correct |
35 ms |
70760 KB |
Output is correct |
11 |
Correct |
34 ms |
70816 KB |
Output is correct |
12 |
Correct |
33 ms |
70872 KB |
Output is correct |
13 |
Correct |
33 ms |
70860 KB |
Output is correct |
14 |
Correct |
34 ms |
70928 KB |
Output is correct |
15 |
Correct |
34 ms |
70808 KB |
Output is correct |
16 |
Correct |
47 ms |
70980 KB |
Output is correct |
17 |
Correct |
44 ms |
72208 KB |
Output is correct |
18 |
Correct |
47 ms |
75396 KB |
Output is correct |
19 |
Correct |
48 ms |
76336 KB |
Output is correct |
20 |
Correct |
46 ms |
76484 KB |
Output is correct |
21 |
Correct |
36 ms |
71252 KB |
Output is correct |
22 |
Correct |
51 ms |
75588 KB |
Output is correct |
23 |
Correct |
49 ms |
74816 KB |
Output is correct |
24 |
Correct |
48 ms |
76112 KB |
Output is correct |
25 |
Correct |
48 ms |
76492 KB |
Output is correct |
26 |
Correct |
48 ms |
76564 KB |
Output is correct |
27 |
Correct |
51 ms |
76588 KB |
Output is correct |
28 |
Correct |
48 ms |
76848 KB |
Output is correct |
29 |
Correct |
60 ms |
76852 KB |
Output is correct |
30 |
Correct |
58 ms |
76520 KB |
Output is correct |
31 |
Correct |
64 ms |
76704 KB |
Output is correct |
32 |
Correct |
53 ms |
76608 KB |
Output is correct |
33 |
Correct |
53 ms |
77376 KB |
Output is correct |
34 |
Correct |
53 ms |
77172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
70732 KB |
Output is correct |
2 |
Correct |
37 ms |
70732 KB |
Output is correct |
3 |
Correct |
33 ms |
70760 KB |
Output is correct |
4 |
Correct |
34 ms |
70724 KB |
Output is correct |
5 |
Correct |
33 ms |
70740 KB |
Output is correct |
6 |
Correct |
34 ms |
70724 KB |
Output is correct |
7 |
Correct |
34 ms |
70732 KB |
Output is correct |
8 |
Correct |
34 ms |
70780 KB |
Output is correct |
9 |
Correct |
34 ms |
70708 KB |
Output is correct |
10 |
Correct |
33 ms |
70812 KB |
Output is correct |
11 |
Correct |
35 ms |
70876 KB |
Output is correct |
12 |
Correct |
36 ms |
70820 KB |
Output is correct |
13 |
Correct |
43 ms |
70808 KB |
Output is correct |
14 |
Correct |
35 ms |
70852 KB |
Output is correct |
15 |
Correct |
36 ms |
70924 KB |
Output is correct |
16 |
Correct |
41 ms |
70924 KB |
Output is correct |
17 |
Correct |
39 ms |
72140 KB |
Output is correct |
18 |
Correct |
50 ms |
75420 KB |
Output is correct |
19 |
Correct |
51 ms |
76452 KB |
Output is correct |
20 |
Correct |
52 ms |
76484 KB |
Output is correct |
21 |
Correct |
51 ms |
71244 KB |
Output is correct |
22 |
Correct |
48 ms |
75596 KB |
Output is correct |
23 |
Correct |
42 ms |
74828 KB |
Output is correct |
24 |
Correct |
50 ms |
76072 KB |
Output is correct |
25 |
Correct |
54 ms |
76572 KB |
Output is correct |
26 |
Correct |
65 ms |
76524 KB |
Output is correct |
27 |
Correct |
50 ms |
76616 KB |
Output is correct |
28 |
Correct |
51 ms |
76876 KB |
Output is correct |
29 |
Correct |
50 ms |
76848 KB |
Output is correct |
30 |
Correct |
49 ms |
76488 KB |
Output is correct |
31 |
Correct |
49 ms |
76652 KB |
Output is correct |
32 |
Correct |
60 ms |
76672 KB |
Output is correct |
33 |
Correct |
54 ms |
77384 KB |
Output is correct |
34 |
Correct |
57 ms |
77124 KB |
Output is correct |
35 |
Correct |
61 ms |
75076 KB |
Output is correct |
36 |
Correct |
42 ms |
73220 KB |
Output is correct |
37 |
Correct |
61 ms |
77852 KB |
Output is correct |
38 |
Correct |
59 ms |
77888 KB |
Output is correct |
39 |
Correct |
58 ms |
77576 KB |
Output is correct |
40 |
Correct |
58 ms |
77640 KB |
Output is correct |
41 |
Correct |
58 ms |
77920 KB |
Output is correct |
42 |
Correct |
54 ms |
76848 KB |
Output is correct |
43 |
Correct |
54 ms |
76852 KB |
Output is correct |
44 |
Correct |
53 ms |
76892 KB |
Output is correct |
45 |
Correct |
72 ms |
82220 KB |
Output is correct |
46 |
Correct |
64 ms |
82116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
70800 KB |
Output is correct |
2 |
Correct |
34 ms |
70740 KB |
Output is correct |
3 |
Correct |
34 ms |
70704 KB |
Output is correct |
4 |
Correct |
35 ms |
70736 KB |
Output is correct |
5 |
Correct |
36 ms |
70784 KB |
Output is correct |
6 |
Correct |
33 ms |
70700 KB |
Output is correct |
7 |
Correct |
34 ms |
70728 KB |
Output is correct |
8 |
Correct |
32 ms |
70688 KB |
Output is correct |
9 |
Correct |
32 ms |
70772 KB |
Output is correct |
10 |
Correct |
33 ms |
70860 KB |
Output is correct |
11 |
Correct |
33 ms |
70816 KB |
Output is correct |
12 |
Correct |
33 ms |
70900 KB |
Output is correct |
13 |
Correct |
33 ms |
70872 KB |
Output is correct |
14 |
Correct |
36 ms |
70928 KB |
Output is correct |
15 |
Correct |
36 ms |
70860 KB |
Output is correct |
16 |
Correct |
35 ms |
70876 KB |
Output is correct |
17 |
Correct |
38 ms |
72208 KB |
Output is correct |
18 |
Correct |
51 ms |
75332 KB |
Output is correct |
19 |
Correct |
51 ms |
76424 KB |
Output is correct |
20 |
Correct |
61 ms |
76564 KB |
Output is correct |
21 |
Correct |
39 ms |
71240 KB |
Output is correct |
22 |
Correct |
47 ms |
75616 KB |
Output is correct |
23 |
Correct |
43 ms |
74820 KB |
Output is correct |
24 |
Correct |
47 ms |
76100 KB |
Output is correct |
25 |
Correct |
48 ms |
76476 KB |
Output is correct |
26 |
Correct |
51 ms |
76672 KB |
Output is correct |
27 |
Correct |
49 ms |
76548 KB |
Output is correct |
28 |
Correct |
53 ms |
76872 KB |
Output is correct |
29 |
Correct |
53 ms |
76788 KB |
Output is correct |
30 |
Correct |
46 ms |
76520 KB |
Output is correct |
31 |
Correct |
49 ms |
76704 KB |
Output is correct |
32 |
Correct |
52 ms |
76680 KB |
Output is correct |
33 |
Correct |
62 ms |
77464 KB |
Output is correct |
34 |
Correct |
56 ms |
77184 KB |
Output is correct |
35 |
Correct |
47 ms |
75112 KB |
Output is correct |
36 |
Correct |
41 ms |
73156 KB |
Output is correct |
37 |
Correct |
57 ms |
77892 KB |
Output is correct |
38 |
Correct |
58 ms |
77960 KB |
Output is correct |
39 |
Correct |
59 ms |
77728 KB |
Output is correct |
40 |
Correct |
63 ms |
77796 KB |
Output is correct |
41 |
Correct |
55 ms |
77876 KB |
Output is correct |
42 |
Correct |
52 ms |
76884 KB |
Output is correct |
43 |
Correct |
51 ms |
76896 KB |
Output is correct |
44 |
Correct |
51 ms |
76784 KB |
Output is correct |
45 |
Correct |
70 ms |
82244 KB |
Output is correct |
46 |
Correct |
68 ms |
82116 KB |
Output is correct |
47 |
Runtime error |
197 ms |
215000 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |