#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
const int sz = 100;
int dist[30005];
int brr[30005][105];
vector <int> stuf[30005];
int S[30005], P[30005], proc[30005];
void solve(){
cin >> n >> m;
for(int i=1;i<=m;i++){
int a, b; cin >> a >> b;
stuf[a].push_back(b);
S[i] = a, P[i] = b;
}
for(int i=0;i<n;i++)for(int j=0;j<=sz;j++)brr[i][j] = 1e18;
for(int i=0;i<n;i++)dist[i] = 1e18;
priority_queue <pii, vector<pii>, greater <pii> > q;
q.push({0, {S[1], -1}});
dist[S[1]] = 0;
while(!q.empty()){
int z = q.top().fi, x = q.top().se.fi, y = q.top().se.se; q.pop();
if(y != -1){
if(x + y < n && brr[x + y][y] > brr[x][y] + 1)brr[x + y][y] = brr[x][y] + 1, dist[x + y] = min(dist[x + y], brr[x][y] + 1), q.push({z+1, {x + y, y}});
if(x - y >= 0 && brr[x-y][y] > brr[x][y] + 1)brr[x-y][y] = brr[x][y] + 1, dist[x-y] = min(dist[x-y], brr[x][y] + 1), q.push({z+1, {x-y, y}});
}
if(!proc[x]){
proc[x] = 1;
for(auto i : stuf[x]){
if(i > sz){
for(int j = x + i; j < n; j += i){
if(dist[j] > dist[x] + (j - x) / i)dist[j] = dist[x] + (j - x) / i, q.push({dist[j], {j, -1}});
}
for(int j = x - i; j >= 0; j -= i){
if(dist[j] > dist[x] + (x - j) / i)dist[j] = dist[x] + (x - j) / i, q.push({dist[j], {j, -1}});
}
}
else{
y = i;
if(x + y < n && brr[x + y][y] > dist[x] + 1)brr[x + y][y] = dist[x] + 1, dist[x + y] = min(dist[x + y], dist[x] + 1), q.push({z + 1, {x + y, y}});
if(x - y >= 0 && brr[x-y][y] > dist[x] + 1)brr[x-y][y] = dist[x] + 1, dist[x-y] = min(dist[x-y], dist[x] + 1), q.push({z + 1,{x-y, y}});
}
}
}
}
cout << (dist[S[2]] == 1e18 ? -1 : dist[S[2]]);
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
skyscraper.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
61 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
17 |
Correct |
2 ms |
1748 KB |
Output is correct |
18 |
Correct |
2 ms |
2484 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
2 ms |
2456 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2716 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
7 ms |
2832 KB |
Output is correct |
30 |
Correct |
3 ms |
2644 KB |
Output is correct |
31 |
Correct |
4 ms |
2700 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
13 ms |
2988 KB |
Output is correct |
34 |
Correct |
12 ms |
2968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
17 |
Correct |
2 ms |
1748 KB |
Output is correct |
18 |
Correct |
2 ms |
2448 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
1 ms |
1432 KB |
Output is correct |
22 |
Correct |
1 ms |
2516 KB |
Output is correct |
23 |
Correct |
2 ms |
2388 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2720 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2844 KB |
Output is correct |
29 |
Correct |
6 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2644 KB |
Output is correct |
31 |
Correct |
4 ms |
2792 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
12 ms |
2900 KB |
Output is correct |
34 |
Correct |
12 ms |
2996 KB |
Output is correct |
35 |
Correct |
10 ms |
3412 KB |
Output is correct |
36 |
Correct |
3 ms |
2132 KB |
Output is correct |
37 |
Correct |
15 ms |
3668 KB |
Output is correct |
38 |
Correct |
16 ms |
4144 KB |
Output is correct |
39 |
Correct |
18 ms |
4180 KB |
Output is correct |
40 |
Correct |
18 ms |
4116 KB |
Output is correct |
41 |
Correct |
14 ms |
4180 KB |
Output is correct |
42 |
Correct |
5 ms |
3672 KB |
Output is correct |
43 |
Correct |
5 ms |
3668 KB |
Output is correct |
44 |
Correct |
5 ms |
3780 KB |
Output is correct |
45 |
Correct |
27 ms |
4292 KB |
Output is correct |
46 |
Correct |
27 ms |
4292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
17 |
Correct |
2 ms |
1748 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2648 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
1 ms |
1368 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
2 ms |
2376 KB |
Output is correct |
24 |
Correct |
3 ms |
2720 KB |
Output is correct |
25 |
Correct |
2 ms |
2716 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2720 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
7 ms |
2776 KB |
Output is correct |
30 |
Correct |
3 ms |
2696 KB |
Output is correct |
31 |
Correct |
4 ms |
2776 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
12 ms |
2992 KB |
Output is correct |
34 |
Correct |
13 ms |
2960 KB |
Output is correct |
35 |
Correct |
10 ms |
3352 KB |
Output is correct |
36 |
Correct |
3 ms |
2200 KB |
Output is correct |
37 |
Correct |
15 ms |
3708 KB |
Output is correct |
38 |
Correct |
14 ms |
4156 KB |
Output is correct |
39 |
Correct |
14 ms |
4180 KB |
Output is correct |
40 |
Correct |
17 ms |
4208 KB |
Output is correct |
41 |
Correct |
14 ms |
4180 KB |
Output is correct |
42 |
Correct |
5 ms |
3668 KB |
Output is correct |
43 |
Correct |
4 ms |
3736 KB |
Output is correct |
44 |
Correct |
5 ms |
3776 KB |
Output is correct |
45 |
Correct |
31 ms |
4332 KB |
Output is correct |
46 |
Correct |
27 ms |
4308 KB |
Output is correct |
47 |
Correct |
66 ms |
13260 KB |
Output is correct |
48 |
Correct |
16 ms |
21332 KB |
Output is correct |
49 |
Correct |
17 ms |
23124 KB |
Output is correct |
50 |
Correct |
14 ms |
24912 KB |
Output is correct |
51 |
Correct |
46 ms |
28424 KB |
Output is correct |
52 |
Correct |
52 ms |
28400 KB |
Output is correct |
53 |
Correct |
22 ms |
27596 KB |
Output is correct |
54 |
Correct |
12 ms |
26128 KB |
Output is correct |
55 |
Correct |
14 ms |
26132 KB |
Output is correct |
56 |
Correct |
18 ms |
27756 KB |
Output is correct |
57 |
Correct |
85 ms |
26372 KB |
Output is correct |
58 |
Correct |
17 ms |
27096 KB |
Output is correct |
59 |
Correct |
20 ms |
27224 KB |
Output is correct |
60 |
Correct |
23 ms |
27168 KB |
Output is correct |
61 |
Correct |
20 ms |
27168 KB |
Output is correct |
62 |
Correct |
40 ms |
27884 KB |
Output is correct |
63 |
Correct |
46 ms |
30224 KB |
Output is correct |
64 |
Correct |
49 ms |
30148 KB |
Output is correct |
65 |
Correct |
65 ms |
33344 KB |
Output is correct |
66 |
Correct |
423 ms |
33204 KB |
Output is correct |
67 |
Correct |
395 ms |
33280 KB |
Output is correct |