#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int N = 3e4 + 5;
const int SQ = 25;
vector<int> adj1[N][SQ];
vector<int> adj11[N][SQ];
vector<pair<int,int>> r_adj1[N];
vector<pair<int,int>> adj2[N];
vector<int> r_adj2[N];
ll dis[3][N][SQ];
void solve(){
int n,m;
cin>>n>>m;
vector<pair<int,int>> d(m);
for(auto &[i,p] : d) cin>>i>>p;
vector<vector<int>> dogs(n+1);
for(int i=0;i<m;i++) dogs[d[i].first].push_back(i);
for(int i=0;i<n;i++){
for(int j=1;j<SQ;j++){
adj1[i][j].push_back(i);
if(i+j<n) adj11[i][j].push_back(i+j) ;
if(i-j>=0) adj11[i][j].push_back(i-j);
}
}
for(int i=0;i<n;i++){
for(auto j : dogs[i]){
if(d[j].second < SQ) r_adj1[i].push_back({d[j].first,d[j].second});
else r_adj2[i].push_back(j);
}
}
for(int j=0;j<m;j++){
auto [i,p] = d[j];
if(p < SQ) continue;
for(int k=i+p;k<n;k+=p) adj2[j].push_back({k,(k-i)/p});
for(int k=i-p;k>=0;k-=p) adj2[j].push_back({k,(i-k)/p});
}
priority_queue<array<ll,4>,vector<array<ll,4>>,greater<>> q;
for(int i=0;i<N;i++) for(int j=0;j<SQ;j++) dis[0][i][j] = dis[1][i][j] = dis[2][i][j] = 1e18;
dis[0][d[0].first][0] = 0;
q.push({0,0,d[0].first,0});
while(q.size()){
auto [c,t,i,p] = q.top(); q.pop();
if(c > dis[t][i][p]) continue;
if(t == 0){
for(auto [ii,pp] : r_adj1[i]){
if(c < dis[1][ii][pp]) {
//cout<<"HI"<<endl;
q.push({c,1,ii,pp});
dis[1][ii][pp] = c;
}
}
for(auto j : r_adj2[i]){
if(c < dis[2][j][0]){
q.push({c,2,j,0});
dis[2][j][0] = c;
}
}
}
else if(t == 1){
//if(t == 1 && i == 4 && p == 1) cout<<"WOW"<<endl;
for(auto j : adj1[i][p]){
if(c < dis[0][j][0]){
//if(i == 0 && p == 2) cout<<j<<endl;
q.push({c,0,j,0});
dis[0][j][0] = c;
}
}
for(auto j : adj11[i][p]){
//if(t == 1 && i == 4 && p == 1) cout<<"mm"<<endl;
if(c+1 < dis[1][j][p]){
//if(t == 1 && i == 4 && p == 1) cout<<j<<' '<<p<<endl;
q.push({c+1,1,j,p});
dis[1][j][p] = c+1;
}
}
}
else {
for(auto [j,cost] : adj2[i]){
if(c + cost < dis[0][j][0]){
q.push({c+cost,0,j,0});
dis[0][j][0] = c + cost;
}
}
}
}
cout<<(dis[0][d[1].first][0] == 1e18 ? -1 : dis[0][d[1].first][0])<<endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:18:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
18 | for(auto &[i,p] : d) cin>>i>>p;
| ^
skyscraper.cpp:36:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | auto [i,p] = d[j];
| ^
skyscraper.cpp:46:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | auto [c,t,i,p] = q.top(); q.pop();
| ^
skyscraper.cpp:49:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
49 | for(auto [ii,pp] : r_adj1[i]){
| ^
skyscraper.cpp:85:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
85 | for(auto [j,cost] : adj2[i]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
55376 KB |
Output is correct |
2 |
Correct |
11 ms |
55376 KB |
Output is correct |
3 |
Correct |
10 ms |
55376 KB |
Output is correct |
4 |
Correct |
11 ms |
55544 KB |
Output is correct |
5 |
Correct |
10 ms |
55544 KB |
Output is correct |
6 |
Correct |
11 ms |
55376 KB |
Output is correct |
7 |
Correct |
10 ms |
55376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
55376 KB |
Output is correct |
2 |
Correct |
13 ms |
55376 KB |
Output is correct |
3 |
Correct |
11 ms |
55232 KB |
Output is correct |
4 |
Correct |
10 ms |
55396 KB |
Output is correct |
5 |
Correct |
11 ms |
55376 KB |
Output is correct |
6 |
Correct |
11 ms |
55376 KB |
Output is correct |
7 |
Correct |
11 ms |
55180 KB |
Output is correct |
8 |
Correct |
13 ms |
55376 KB |
Output is correct |
9 |
Correct |
10 ms |
55544 KB |
Output is correct |
10 |
Correct |
11 ms |
55376 KB |
Output is correct |
11 |
Correct |
13 ms |
55632 KB |
Output is correct |
12 |
Correct |
11 ms |
55376 KB |
Output is correct |
13 |
Correct |
11 ms |
55416 KB |
Output is correct |
14 |
Correct |
12 ms |
55632 KB |
Output is correct |
15 |
Correct |
11 ms |
55612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
55376 KB |
Output is correct |
2 |
Correct |
11 ms |
55376 KB |
Output is correct |
3 |
Correct |
10 ms |
55376 KB |
Output is correct |
4 |
Correct |
11 ms |
55428 KB |
Output is correct |
5 |
Correct |
11 ms |
55544 KB |
Output is correct |
6 |
Correct |
11 ms |
55376 KB |
Output is correct |
7 |
Correct |
11 ms |
55376 KB |
Output is correct |
8 |
Correct |
11 ms |
55376 KB |
Output is correct |
9 |
Correct |
10 ms |
55376 KB |
Output is correct |
10 |
Correct |
10 ms |
55392 KB |
Output is correct |
11 |
Correct |
11 ms |
55632 KB |
Output is correct |
12 |
Correct |
11 ms |
55632 KB |
Output is correct |
13 |
Correct |
11 ms |
55632 KB |
Output is correct |
14 |
Correct |
11 ms |
55632 KB |
Output is correct |
15 |
Correct |
12 ms |
55632 KB |
Output is correct |
16 |
Correct |
12 ms |
55760 KB |
Output is correct |
17 |
Correct |
15 ms |
56656 KB |
Output is correct |
18 |
Correct |
14 ms |
57936 KB |
Output is correct |
19 |
Correct |
16 ms |
58544 KB |
Output is correct |
20 |
Correct |
17 ms |
58448 KB |
Output is correct |
21 |
Correct |
13 ms |
55900 KB |
Output is correct |
22 |
Correct |
16 ms |
58192 KB |
Output is correct |
23 |
Correct |
16 ms |
57936 KB |
Output is correct |
24 |
Correct |
18 ms |
58600 KB |
Output is correct |
25 |
Correct |
17 ms |
58448 KB |
Output is correct |
26 |
Correct |
16 ms |
58448 KB |
Output is correct |
27 |
Correct |
16 ms |
58488 KB |
Output is correct |
28 |
Correct |
18 ms |
58704 KB |
Output is correct |
29 |
Correct |
21 ms |
59216 KB |
Output is correct |
30 |
Correct |
17 ms |
58448 KB |
Output is correct |
31 |
Correct |
16 ms |
58960 KB |
Output is correct |
32 |
Correct |
16 ms |
58704 KB |
Output is correct |
33 |
Correct |
24 ms |
59728 KB |
Output is correct |
34 |
Correct |
23 ms |
59728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
55392 KB |
Output is correct |
2 |
Correct |
10 ms |
55544 KB |
Output is correct |
3 |
Correct |
11 ms |
55376 KB |
Output is correct |
4 |
Correct |
10 ms |
55376 KB |
Output is correct |
5 |
Correct |
11 ms |
55544 KB |
Output is correct |
6 |
Correct |
10 ms |
55376 KB |
Output is correct |
7 |
Correct |
11 ms |
55376 KB |
Output is correct |
8 |
Correct |
11 ms |
55632 KB |
Output is correct |
9 |
Correct |
11 ms |
55376 KB |
Output is correct |
10 |
Correct |
11 ms |
55608 KB |
Output is correct |
11 |
Correct |
11 ms |
55632 KB |
Output is correct |
12 |
Correct |
11 ms |
55632 KB |
Output is correct |
13 |
Correct |
10 ms |
55632 KB |
Output is correct |
14 |
Correct |
12 ms |
55632 KB |
Output is correct |
15 |
Correct |
11 ms |
55632 KB |
Output is correct |
16 |
Correct |
11 ms |
55632 KB |
Output is correct |
17 |
Correct |
15 ms |
56656 KB |
Output is correct |
18 |
Correct |
17 ms |
57936 KB |
Output is correct |
19 |
Correct |
21 ms |
58448 KB |
Output is correct |
20 |
Correct |
17 ms |
58448 KB |
Output is correct |
21 |
Correct |
12 ms |
55888 KB |
Output is correct |
22 |
Correct |
16 ms |
58192 KB |
Output is correct |
23 |
Correct |
16 ms |
57976 KB |
Output is correct |
24 |
Correct |
19 ms |
58448 KB |
Output is correct |
25 |
Correct |
19 ms |
58448 KB |
Output is correct |
26 |
Correct |
17 ms |
58448 KB |
Output is correct |
27 |
Correct |
17 ms |
58448 KB |
Output is correct |
28 |
Correct |
18 ms |
58704 KB |
Output is correct |
29 |
Correct |
25 ms |
59216 KB |
Output is correct |
30 |
Correct |
21 ms |
58452 KB |
Output is correct |
31 |
Correct |
18 ms |
58960 KB |
Output is correct |
32 |
Correct |
18 ms |
58872 KB |
Output is correct |
33 |
Correct |
24 ms |
59728 KB |
Output is correct |
34 |
Correct |
24 ms |
59728 KB |
Output is correct |
35 |
Correct |
32 ms |
60096 KB |
Output is correct |
36 |
Correct |
17 ms |
57424 KB |
Output is correct |
37 |
Correct |
37 ms |
61640 KB |
Output is correct |
38 |
Correct |
41 ms |
61640 KB |
Output is correct |
39 |
Correct |
41 ms |
61632 KB |
Output is correct |
40 |
Correct |
41 ms |
61648 KB |
Output is correct |
41 |
Correct |
40 ms |
61644 KB |
Output is correct |
42 |
Correct |
19 ms |
59084 KB |
Output is correct |
43 |
Correct |
20 ms |
58964 KB |
Output is correct |
44 |
Correct |
20 ms |
59092 KB |
Output is correct |
45 |
Correct |
39 ms |
67024 KB |
Output is correct |
46 |
Correct |
49 ms |
67232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
55376 KB |
Output is correct |
2 |
Correct |
11 ms |
55420 KB |
Output is correct |
3 |
Correct |
11 ms |
55376 KB |
Output is correct |
4 |
Correct |
10 ms |
55280 KB |
Output is correct |
5 |
Correct |
10 ms |
55376 KB |
Output is correct |
6 |
Correct |
11 ms |
55388 KB |
Output is correct |
7 |
Correct |
11 ms |
55376 KB |
Output is correct |
8 |
Correct |
10 ms |
55376 KB |
Output is correct |
9 |
Correct |
11 ms |
55376 KB |
Output is correct |
10 |
Correct |
11 ms |
55388 KB |
Output is correct |
11 |
Correct |
12 ms |
55632 KB |
Output is correct |
12 |
Correct |
10 ms |
55408 KB |
Output is correct |
13 |
Correct |
11 ms |
55632 KB |
Output is correct |
14 |
Correct |
11 ms |
55632 KB |
Output is correct |
15 |
Correct |
12 ms |
55632 KB |
Output is correct |
16 |
Correct |
11 ms |
55768 KB |
Output is correct |
17 |
Correct |
16 ms |
56656 KB |
Output is correct |
18 |
Correct |
15 ms |
57936 KB |
Output is correct |
19 |
Correct |
16 ms |
58448 KB |
Output is correct |
20 |
Correct |
17 ms |
58448 KB |
Output is correct |
21 |
Correct |
11 ms |
55984 KB |
Output is correct |
22 |
Correct |
15 ms |
58192 KB |
Output is correct |
23 |
Correct |
17 ms |
57936 KB |
Output is correct |
24 |
Correct |
19 ms |
58448 KB |
Output is correct |
25 |
Correct |
18 ms |
58584 KB |
Output is correct |
26 |
Correct |
18 ms |
58544 KB |
Output is correct |
27 |
Correct |
17 ms |
58448 KB |
Output is correct |
28 |
Correct |
18 ms |
58872 KB |
Output is correct |
29 |
Correct |
22 ms |
59216 KB |
Output is correct |
30 |
Correct |
20 ms |
58548 KB |
Output is correct |
31 |
Correct |
19 ms |
58960 KB |
Output is correct |
32 |
Correct |
18 ms |
58704 KB |
Output is correct |
33 |
Correct |
24 ms |
59728 KB |
Output is correct |
34 |
Correct |
24 ms |
59728 KB |
Output is correct |
35 |
Correct |
30 ms |
60096 KB |
Output is correct |
36 |
Correct |
16 ms |
57424 KB |
Output is correct |
37 |
Correct |
36 ms |
61656 KB |
Output is correct |
38 |
Correct |
41 ms |
61640 KB |
Output is correct |
39 |
Correct |
39 ms |
61640 KB |
Output is correct |
40 |
Correct |
39 ms |
61648 KB |
Output is correct |
41 |
Correct |
45 ms |
61724 KB |
Output is correct |
42 |
Correct |
20 ms |
59096 KB |
Output is correct |
43 |
Correct |
23 ms |
59200 KB |
Output is correct |
44 |
Correct |
20 ms |
59344 KB |
Output is correct |
45 |
Correct |
41 ms |
66892 KB |
Output is correct |
46 |
Correct |
40 ms |
67004 KB |
Output is correct |
47 |
Correct |
131 ms |
82632 KB |
Output is correct |
48 |
Correct |
83 ms |
93512 KB |
Output is correct |
49 |
Correct |
84 ms |
96328 KB |
Output is correct |
50 |
Correct |
91 ms |
99912 KB |
Output is correct |
51 |
Correct |
158 ms |
105920 KB |
Output is correct |
52 |
Correct |
163 ms |
106176 KB |
Output is correct |
53 |
Correct |
112 ms |
102736 KB |
Output is correct |
54 |
Correct |
101 ms |
101192 KB |
Output is correct |
55 |
Correct |
101 ms |
101192 KB |
Output is correct |
56 |
Correct |
104 ms |
103120 KB |
Output is correct |
57 |
Correct |
256 ms |
116420 KB |
Output is correct |
58 |
Correct |
109 ms |
101748 KB |
Output is correct |
59 |
Correct |
108 ms |
102084 KB |
Output is correct |
60 |
Correct |
111 ms |
102596 KB |
Output is correct |
61 |
Correct |
115 ms |
102096 KB |
Output is correct |
62 |
Correct |
141 ms |
106824 KB |
Output is correct |
63 |
Correct |
185 ms |
132788 KB |
Output is correct |
64 |
Correct |
180 ms |
141136 KB |
Output is correct |
65 |
Correct |
199 ms |
152508 KB |
Output is correct |
66 |
Correct |
354 ms |
189208 KB |
Output is correct |
67 |
Correct |
346 ms |
189356 KB |
Output is correct |