// #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];
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}});
}
}
bool Visit[mxn];
int main(){
giuncute();
#ifndef ONLINE_JUDGE
// OF();
// freopen("test.inp","r",stdin);
#endif
cin >> n >> m;
//_sqrt=sqrt(n);
int st,ed;
lp(i, 1, m){
ll u, p;
cin >> u >> p;
if (i==1) st=u;
if (i==2) ed=u;
jump[u].push_back(p);
}
// if (n==1)
// {
// cout<<-1;
// return 0;
// }
lp(i, 0, n) lp(j, 0, _sqrt) dp[i][j] = 1e16;
dp[st][0]=0;
q.push({0, {st, 0}});
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;
// maintain last step
prc(u, cu, step);
// change step
if (Visit[u]) continue;
Visit[u]=true;
if (dp[u][0]>cu)
{
dp[u][0]=cu;
q.push({-dp[u][0],{u,0}});
}
for(ll i : jump[u]){
if(i <= _sqrt)
{
prc(u, cu, i);
if (dp[u][i]>cu)
{
dp[u][i]=cu;
q.push({-dp[u][i],{u,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}});
}
}
}
}
ll ans = 1e16;
lp(i, 0, _sqrt) ans = min(ans, dp[ed][i]);
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:71:12: warning: 'ed' may be used uninitialized in this function [-Wmaybe-uninitialized]
71 | int st,ed;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
984 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 |
0 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 |
1 ms |
1228 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 |
0 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 |
1228 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 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 |
1484 KB |
Output is correct |
22 |
Correct |
2 ms |
3404 KB |
Output is correct |
23 |
Correct |
2 ms |
3148 KB |
Output is correct |
24 |
Correct |
3 ms |
3660 KB |
Output is correct |
25 |
Correct |
3 ms |
3788 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 |
3 ms |
3788 KB |
Output is correct |
31 |
Correct |
5 ms |
3852 KB |
Output is correct |
32 |
Correct |
4 ms |
3788 KB |
Output is correct |
33 |
Correct |
15 ms |
3932 KB |
Output is correct |
34 |
Correct |
15 ms |
3928 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 |
1228 KB |
Output is correct |
11 |
Correct |
3 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 |
1228 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 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 |
1484 KB |
Output is correct |
22 |
Correct |
2 ms |
3404 KB |
Output is correct |
23 |
Correct |
2 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3732 KB |
Output is correct |
25 |
Correct |
2 ms |
3788 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 |
3 ms |
3788 KB |
Output is correct |
31 |
Correct |
5 ms |
3860 KB |
Output is correct |
32 |
Correct |
4 ms |
3788 KB |
Output is correct |
33 |
Correct |
15 ms |
3916 KB |
Output is correct |
34 |
Correct |
14 ms |
3932 KB |
Output is correct |
35 |
Correct |
13 ms |
3660 KB |
Output is correct |
36 |
Correct |
3 ms |
2636 KB |
Output is correct |
37 |
Correct |
21 ms |
4272 KB |
Output is correct |
38 |
Correct |
20 ms |
4484 KB |
Output is correct |
39 |
Correct |
20 ms |
4492 KB |
Output is correct |
40 |
Correct |
18 ms |
4536 KB |
Output is correct |
41 |
Correct |
19 ms |
4496 KB |
Output is correct |
42 |
Correct |
6 ms |
4172 KB |
Output is correct |
43 |
Correct |
6 ms |
4172 KB |
Output is correct |
44 |
Correct |
6 ms |
4084 KB |
Output is correct |
45 |
Correct |
52 ms |
5068 KB |
Output is correct |
46 |
Correct |
53 ms |
5132 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 |
1228 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 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 |
1612 KB |
Output is correct |
22 |
Correct |
3 ms |
3404 KB |
Output is correct |
23 |
Correct |
2 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3664 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 |
3788 KB |
Output is correct |
28 |
Correct |
3 ms |
3788 KB |
Output is correct |
29 |
Correct |
7 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 |
15 ms |
3928 KB |
Output is correct |
34 |
Correct |
14 ms |
3932 KB |
Output is correct |
35 |
Correct |
14 ms |
3660 KB |
Output is correct |
36 |
Correct |
3 ms |
2636 KB |
Output is correct |
37 |
Correct |
19 ms |
4300 KB |
Output is correct |
38 |
Correct |
18 ms |
4492 KB |
Output is correct |
39 |
Correct |
18 ms |
4492 KB |
Output is correct |
40 |
Correct |
18 ms |
4532 KB |
Output is correct |
41 |
Correct |
18 ms |
4496 KB |
Output is correct |
42 |
Correct |
5 ms |
4172 KB |
Output is correct |
43 |
Correct |
5 ms |
4172 KB |
Output is correct |
44 |
Correct |
6 ms |
4212 KB |
Output is correct |
45 |
Correct |
54 ms |
5068 KB |
Output is correct |
46 |
Correct |
54 ms |
5016 KB |
Output is correct |
47 |
Correct |
82 ms |
19404 KB |
Output is correct |
48 |
Correct |
18 ms |
33612 KB |
Output is correct |
49 |
Correct |
19 ms |
36640 KB |
Output is correct |
50 |
Correct |
19 ms |
39956 KB |
Output is correct |
51 |
Correct |
61 ms |
44048 KB |
Output is correct |
52 |
Correct |
65 ms |
44092 KB |
Output is correct |
53 |
Correct |
34 ms |
43212 KB |
Output is correct |
54 |
Correct |
22 ms |
42604 KB |
Output is correct |
55 |
Correct |
24 ms |
42692 KB |
Output is correct |
56 |
Correct |
25 ms |
43468 KB |
Output is correct |
57 |
Correct |
83 ms |
42684 KB |
Output is correct |
58 |
Correct |
25 ms |
42940 KB |
Output is correct |
59 |
Correct |
28 ms |
42960 KB |
Output is correct |
60 |
Correct |
32 ms |
42960 KB |
Output is correct |
61 |
Correct |
29 ms |
42956 KB |
Output is correct |
62 |
Correct |
51 ms |
43452 KB |
Output is correct |
63 |
Correct |
51 ms |
44740 KB |
Output is correct |
64 |
Correct |
55 ms |
44640 KB |
Output is correct |
65 |
Correct |
263 ms |
46276 KB |
Output is correct |
66 |
Correct |
748 ms |
46144 KB |
Output is correct |
67 |
Correct |
718 ms |
46208 KB |
Output is correct |