// #pragma GCC target("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define cll const ll
#define lp(a, b, c) for(ll a = b; a <= c; ++a)
#define lpd(a, b, c) for(ll a = b; a >= c; --a)
#define vec(a) vector<a>
#define pp(a, b) pair<a, b>
#define EACHCASE lpd(cs, read(), 1)
#define Fname "f"
using namespace std;
template <typename T> inline void Read(T &x){
x = 0; char c;
ll dau = 1;
while(!isdigit(c = getchar())) if(c == '-') dau = -1;
do{
x = x * 10 + c - '0';
} while (isdigit(c = getchar()));
x *= dau;
}
ll read(){
ll tmp;
cin >> tmp;
return tmp;
}
void giuncute(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
}
void OF(){
if(fopen(Fname".inp", "r")){
freopen(Fname".inp", "r", stdin);
freopen(Fname".out", "w", stdout);
} else if(fopen(Fname".in", "r")){
freopen(Fname".in", "r", stdin);
freopen(Fname".out", "w", stdout);
}
}
cll mxn = 3e4 + 5, _sqrt = 173;
ll n, m, dp[mxn][_sqrt + 4], initu, initp, finalu, finalp;
vec(ll) jump[mxn];
priority_queue<pp(ll, pp(ll, ll))> q;
void prc(ll u, ll cu, ll step){
if(u + step < n && dp[u + step][step] > cu + 1){
dp[u + step][step] = cu + 1;
q.push({-dp[u + step][step], {u + step, step}});
}
if(u - step >= 0 && dp[u - step][step] > cu + 1){
dp[u - step][step] = cu + 1;
q.push({-dp[u - step][step], {u - step, step}});
}
// if(u + step < n && dp[u + step][0] > cu + 1){
// dp[u + step][0] = cu + 1;
// q.push({-dp[u + step][0], {u + step, 0}});
// }
// if(u - step >= 0 && dp[u - step][0] > cu + 1){
// dp[u - step][0] = cu + 1;
// q.push({-dp[u - step][0], {u - step, 0}});
// }
}
int main(){
giuncute();
#ifndef ONLINE_JUDGE
OF();
#endif
cin >> n >> m;
lp(i, 1, m){
static ll u, p;
cin >> u >> p;
if(i == 1) initu = u, initp = p;
if(i == 2) finalu = u, finalp = p;
jump[u].push_back(p);
}
lp(i, 0, n) lp(j, 0, _sqrt) dp[i][j] = 1e16;
dp[initu][0] = 0;
q.push({0, {initu, 0}});
ll ans = 1e16;
while(q.size()){
ll cu = -q.top().first, u = q.top().second.first, step = q.top().second.second;
q.pop();
if(cu != dp[u][step]) continue;
if(u == finalu) ans = min(ans, cu);
// maintain last step
prc(u, cu, step);
// change step
for(ll i : jump[u]){
if(i <= _sqrt)
prc(u, cu, i);
else{
for(ll v = u + i, cnt = 1; v < n; v += i, ++cnt)
if(dp[v][0] > cu + cnt){
dp[v][0] = cu + cnt;
q.push({-dp[v][0], {v, 0}});
}
for(ll v = u - i, cnt = 1; v >= 0; v -= i, ++cnt)
if(dp[v][0] > cu + cnt){
dp[v][0] = cu + cnt;
q.push({-dp[v][0], {v, 0}});
}
}
}
}
if(ans > 1e15) ans = -1;
cout << ans;
}
Compilation message
skyscraper.cpp: In function 'void OF()':
skyscraper.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen(Fname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen(Fname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen(Fname".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen(Fname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1172 KB |
Output is correct |
13 |
Correct |
1 ms |
1160 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1160 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1292 KB |
Output is correct |
15 |
Correct |
2 ms |
1288 KB |
Output is correct |
16 |
Correct |
1 ms |
1296 KB |
Output is correct |
17 |
Correct |
3 ms |
2124 KB |
Output is correct |
18 |
Correct |
2 ms |
3404 KB |
Output is correct |
19 |
Correct |
2 ms |
3788 KB |
Output is correct |
20 |
Correct |
2 ms |
3788 KB |
Output is correct |
21 |
Correct |
1 ms |
1556 KB |
Output is correct |
22 |
Correct |
2 ms |
3480 KB |
Output is correct |
23 |
Correct |
2 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3660 KB |
Output is correct |
25 |
Correct |
3 ms |
3856 KB |
Output is correct |
26 |
Correct |
3 ms |
3788 KB |
Output is correct |
27 |
Correct |
3 ms |
3788 KB |
Output is correct |
28 |
Correct |
3 ms |
3756 KB |
Output is correct |
29 |
Correct |
9 ms |
3788 KB |
Output is correct |
30 |
Correct |
3 ms |
3788 KB |
Output is correct |
31 |
Correct |
5 ms |
3788 KB |
Output is correct |
32 |
Correct |
4 ms |
3788 KB |
Output is correct |
33 |
Correct |
14 ms |
3980 KB |
Output is correct |
34 |
Correct |
15 ms |
3968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
1292 KB |
Output is correct |
17 |
Correct |
3 ms |
2196 KB |
Output is correct |
18 |
Correct |
2 ms |
3404 KB |
Output is correct |
19 |
Correct |
2 ms |
3788 KB |
Output is correct |
20 |
Correct |
2 ms |
3788 KB |
Output is correct |
21 |
Correct |
1 ms |
1552 KB |
Output is correct |
22 |
Correct |
2 ms |
3464 KB |
Output is correct |
23 |
Correct |
2 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3660 KB |
Output is correct |
25 |
Correct |
3 ms |
3864 KB |
Output is correct |
26 |
Correct |
2 ms |
3788 KB |
Output is correct |
27 |
Correct |
2 ms |
3788 KB |
Output is correct |
28 |
Correct |
3 ms |
3788 KB |
Output is correct |
29 |
Correct |
8 ms |
3788 KB |
Output is correct |
30 |
Correct |
4 ms |
3708 KB |
Output is correct |
31 |
Correct |
5 ms |
3856 KB |
Output is correct |
32 |
Correct |
4 ms |
3720 KB |
Output is correct |
33 |
Correct |
14 ms |
3916 KB |
Output is correct |
34 |
Correct |
16 ms |
3916 KB |
Output is correct |
35 |
Correct |
25 ms |
3852 KB |
Output is correct |
36 |
Correct |
4 ms |
2636 KB |
Output is correct |
37 |
Correct |
42 ms |
4700 KB |
Output is correct |
38 |
Correct |
38 ms |
4744 KB |
Output is correct |
39 |
Correct |
39 ms |
4764 KB |
Output is correct |
40 |
Correct |
38 ms |
4764 KB |
Output is correct |
41 |
Correct |
47 ms |
4780 KB |
Output is correct |
42 |
Correct |
7 ms |
4304 KB |
Output is correct |
43 |
Correct |
6 ms |
4348 KB |
Output is correct |
44 |
Correct |
6 ms |
4340 KB |
Output is correct |
45 |
Correct |
111 ms |
5256 KB |
Output is correct |
46 |
Correct |
109 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1296 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
1272 KB |
Output is correct |
17 |
Correct |
4 ms |
2124 KB |
Output is correct |
18 |
Correct |
2 ms |
3404 KB |
Output is correct |
19 |
Correct |
3 ms |
3724 KB |
Output is correct |
20 |
Correct |
2 ms |
3788 KB |
Output is correct |
21 |
Correct |
1 ms |
1520 KB |
Output is correct |
22 |
Correct |
2 ms |
3468 KB |
Output is correct |
23 |
Correct |
2 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3660 KB |
Output is correct |
25 |
Correct |
3 ms |
3788 KB |
Output is correct |
26 |
Correct |
3 ms |
3788 KB |
Output is correct |
27 |
Correct |
3 ms |
3732 KB |
Output is correct |
28 |
Correct |
4 ms |
3768 KB |
Output is correct |
29 |
Correct |
8 ms |
3900 KB |
Output is correct |
30 |
Correct |
4 ms |
3720 KB |
Output is correct |
31 |
Correct |
8 ms |
3788 KB |
Output is correct |
32 |
Correct |
4 ms |
3716 KB |
Output is correct |
33 |
Correct |
15 ms |
3984 KB |
Output is correct |
34 |
Correct |
15 ms |
3916 KB |
Output is correct |
35 |
Correct |
25 ms |
3852 KB |
Output is correct |
36 |
Correct |
4 ms |
2628 KB |
Output is correct |
37 |
Correct |
42 ms |
4624 KB |
Output is correct |
38 |
Correct |
41 ms |
4788 KB |
Output is correct |
39 |
Correct |
40 ms |
4748 KB |
Output is correct |
40 |
Correct |
38 ms |
4760 KB |
Output is correct |
41 |
Correct |
37 ms |
4728 KB |
Output is correct |
42 |
Correct |
6 ms |
4300 KB |
Output is correct |
43 |
Correct |
6 ms |
4312 KB |
Output is correct |
44 |
Correct |
6 ms |
4344 KB |
Output is correct |
45 |
Correct |
111 ms |
5320 KB |
Output is correct |
46 |
Correct |
117 ms |
5324 KB |
Output is correct |
47 |
Correct |
162 ms |
19652 KB |
Output is correct |
48 |
Correct |
20 ms |
33868 KB |
Output is correct |
49 |
Correct |
26 ms |
36796 KB |
Output is correct |
50 |
Correct |
22 ms |
40140 KB |
Output is correct |
51 |
Correct |
77 ms |
44384 KB |
Output is correct |
52 |
Correct |
69 ms |
44352 KB |
Output is correct |
53 |
Correct |
29 ms |
43472 KB |
Output is correct |
54 |
Correct |
18 ms |
42560 KB |
Output is correct |
55 |
Correct |
18 ms |
42572 KB |
Output is correct |
56 |
Correct |
30 ms |
43740 KB |
Output is correct |
57 |
Correct |
92 ms |
42728 KB |
Output is correct |
58 |
Correct |
27 ms |
42952 KB |
Output is correct |
59 |
Correct |
30 ms |
43176 KB |
Output is correct |
60 |
Correct |
35 ms |
43164 KB |
Output is correct |
61 |
Correct |
31 ms |
43120 KB |
Output is correct |
62 |
Correct |
66 ms |
43716 KB |
Output is correct |
63 |
Correct |
63 ms |
44892 KB |
Output is correct |
64 |
Correct |
68 ms |
44908 KB |
Output is correct |
65 |
Execution timed out |
1062 ms |
46460 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |