#include <bits/stdc++.h>
#define ii pair <int, int>
#define iii pair <ii, int>
#define fi first.first
#define se first.second
#define th second
using namespace std;
#define task "code"
#define all(s) s.begin(), s.end()
typedef long long ll;
const int ar = 3e5+2;
const ll mod = 998244353;
const int oo = 1e9;
const int LG = 9;
const int BLOCK = 100;
int n, m, b[ar], p[ar], d[ar][BLOCK + 1];
priority_queue <iii, vector <iii>, greater <iii>> q;
vector <int> pos[ar];
void dijkstra() {
q.push({{0, b[0]}, 0});
int ans = 1e9;
memset(d, 0x3f, sizeof d);
d[b[0]][0] = 0;
while (!q.empty()) {
iii x = q.top();
int dis = x.fi , pk = x.th , i = x.se;
if (i == b[1]) ans = min(ans , dis);
q.pop();
if (dis != d[i][pk]) continue;
if (pk == 0) {
for (auto x : pos[i])
if (p[x] <= BLOCK) {
if (d[i][p[x]] > dis) {
d[i][p[x]] = dis;
q.push({{dis , i} , p[x]});
}
} else {
int pp = p[x];
int cnt = 0;
for (int x = i ; x < n ; x += pp) {
if (d[x][0] > dis + cnt) {
d[x][0] = dis + cnt;
q.push({{dis + cnt , x} , 0});
}
cnt++;
}
cnt = 0;
for (int x = i ; x >= 0 ; x -= pp) {
if (d[x][0] > dis + cnt) {
d[x][0] = dis + cnt;
q.push({{dis + cnt , x} , 0});
}
cnt++;
}
}
} else {
if (i + pk < n && d[i + pk][pk] > dis + 1) {
d[i + pk][pk] = dis + 1;
q.push({{dis + 1 , i + pk} , pk});
}
if (i - pk >= 0 && d[i - pk][pk] > dis + 1) {
d[i - pk][pk] = dis + 1;
q.push({{dis + 1 , i - pk} , pk});
}
if (d[i][0] > dis) {
d[i][0] = dis;
q.push({{dis , i} , 0});
}
}
}
if (ans == 1e9) cout << -1;
else cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> m;
for(int i = 0; i < m; ++i)
cin >> b[i] >> p[i], pos[b[i]].push_back(i);
dijkstra();
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
127824 KB |
Output is correct |
2 |
Correct |
16 ms |
127836 KB |
Output is correct |
3 |
Correct |
22 ms |
128088 KB |
Output is correct |
4 |
Correct |
16 ms |
127832 KB |
Output is correct |
5 |
Correct |
21 ms |
127856 KB |
Output is correct |
6 |
Correct |
16 ms |
127836 KB |
Output is correct |
7 |
Correct |
16 ms |
127836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
127824 KB |
Output is correct |
2 |
Correct |
19 ms |
127832 KB |
Output is correct |
3 |
Correct |
15 ms |
127832 KB |
Output is correct |
4 |
Correct |
19 ms |
127832 KB |
Output is correct |
5 |
Correct |
24 ms |
127832 KB |
Output is correct |
6 |
Correct |
15 ms |
127832 KB |
Output is correct |
7 |
Correct |
16 ms |
127836 KB |
Output is correct |
8 |
Correct |
20 ms |
127724 KB |
Output is correct |
9 |
Correct |
19 ms |
127824 KB |
Output is correct |
10 |
Correct |
15 ms |
127788 KB |
Output is correct |
11 |
Correct |
17 ms |
128088 KB |
Output is correct |
12 |
Correct |
23 ms |
127836 KB |
Output is correct |
13 |
Correct |
16 ms |
127832 KB |
Output is correct |
14 |
Correct |
16 ms |
127832 KB |
Output is correct |
15 |
Correct |
17 ms |
127720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
127832 KB |
Output is correct |
2 |
Correct |
21 ms |
127836 KB |
Output is correct |
3 |
Correct |
17 ms |
127836 KB |
Output is correct |
4 |
Correct |
15 ms |
127832 KB |
Output is correct |
5 |
Correct |
24 ms |
127824 KB |
Output is correct |
6 |
Correct |
20 ms |
127824 KB |
Output is correct |
7 |
Correct |
15 ms |
127648 KB |
Output is correct |
8 |
Correct |
18 ms |
127836 KB |
Output is correct |
9 |
Correct |
15 ms |
128088 KB |
Output is correct |
10 |
Correct |
15 ms |
127836 KB |
Output is correct |
11 |
Correct |
16 ms |
127836 KB |
Output is correct |
12 |
Correct |
16 ms |
127836 KB |
Output is correct |
13 |
Correct |
18 ms |
127836 KB |
Output is correct |
14 |
Correct |
16 ms |
127832 KB |
Output is correct |
15 |
Correct |
16 ms |
127836 KB |
Output is correct |
16 |
Correct |
22 ms |
128088 KB |
Output is correct |
17 |
Correct |
17 ms |
127836 KB |
Output is correct |
18 |
Correct |
16 ms |
127832 KB |
Output is correct |
19 |
Correct |
17 ms |
127752 KB |
Output is correct |
20 |
Correct |
19 ms |
127836 KB |
Output is correct |
21 |
Correct |
15 ms |
127836 KB |
Output is correct |
22 |
Correct |
15 ms |
127832 KB |
Output is correct |
23 |
Correct |
17 ms |
127836 KB |
Output is correct |
24 |
Correct |
17 ms |
127836 KB |
Output is correct |
25 |
Correct |
16 ms |
127832 KB |
Output is correct |
26 |
Correct |
19 ms |
127836 KB |
Output is correct |
27 |
Correct |
16 ms |
127832 KB |
Output is correct |
28 |
Correct |
17 ms |
127832 KB |
Output is correct |
29 |
Correct |
24 ms |
127836 KB |
Output is correct |
30 |
Correct |
17 ms |
127832 KB |
Output is correct |
31 |
Correct |
28 ms |
127836 KB |
Output is correct |
32 |
Correct |
17 ms |
127836 KB |
Output is correct |
33 |
Correct |
28 ms |
127832 KB |
Output is correct |
34 |
Correct |
30 ms |
127832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
127836 KB |
Output is correct |
2 |
Correct |
20 ms |
127836 KB |
Output is correct |
3 |
Correct |
15 ms |
127832 KB |
Output is correct |
4 |
Correct |
17 ms |
127836 KB |
Output is correct |
5 |
Correct |
16 ms |
127832 KB |
Output is correct |
6 |
Correct |
16 ms |
127832 KB |
Output is correct |
7 |
Correct |
15 ms |
127836 KB |
Output is correct |
8 |
Correct |
15 ms |
127832 KB |
Output is correct |
9 |
Correct |
15 ms |
127832 KB |
Output is correct |
10 |
Correct |
15 ms |
127832 KB |
Output is correct |
11 |
Correct |
16 ms |
127836 KB |
Output is correct |
12 |
Correct |
17 ms |
127668 KB |
Output is correct |
13 |
Correct |
16 ms |
127796 KB |
Output is correct |
14 |
Correct |
18 ms |
127832 KB |
Output is correct |
15 |
Correct |
16 ms |
127832 KB |
Output is correct |
16 |
Correct |
16 ms |
127832 KB |
Output is correct |
17 |
Correct |
16 ms |
127832 KB |
Output is correct |
18 |
Correct |
15 ms |
127836 KB |
Output is correct |
19 |
Correct |
15 ms |
127832 KB |
Output is correct |
20 |
Correct |
16 ms |
127832 KB |
Output is correct |
21 |
Correct |
15 ms |
127832 KB |
Output is correct |
22 |
Correct |
15 ms |
127832 KB |
Output is correct |
23 |
Correct |
16 ms |
127832 KB |
Output is correct |
24 |
Correct |
16 ms |
127832 KB |
Output is correct |
25 |
Correct |
16 ms |
127832 KB |
Output is correct |
26 |
Correct |
15 ms |
127836 KB |
Output is correct |
27 |
Correct |
16 ms |
127836 KB |
Output is correct |
28 |
Correct |
17 ms |
127832 KB |
Output is correct |
29 |
Correct |
20 ms |
127832 KB |
Output is correct |
30 |
Correct |
23 ms |
128336 KB |
Output is correct |
31 |
Correct |
26 ms |
127832 KB |
Output is correct |
32 |
Correct |
17 ms |
127832 KB |
Output is correct |
33 |
Correct |
27 ms |
127832 KB |
Output is correct |
34 |
Correct |
27 ms |
127824 KB |
Output is correct |
35 |
Correct |
25 ms |
128092 KB |
Output is correct |
36 |
Correct |
17 ms |
127836 KB |
Output is correct |
37 |
Correct |
28 ms |
128088 KB |
Output is correct |
38 |
Correct |
28 ms |
128088 KB |
Output is correct |
39 |
Correct |
28 ms |
128092 KB |
Output is correct |
40 |
Correct |
28 ms |
128088 KB |
Output is correct |
41 |
Correct |
28 ms |
128288 KB |
Output is correct |
42 |
Correct |
18 ms |
128092 KB |
Output is correct |
43 |
Correct |
18 ms |
128088 KB |
Output is correct |
44 |
Correct |
18 ms |
128088 KB |
Output is correct |
45 |
Correct |
50 ms |
128336 KB |
Output is correct |
46 |
Correct |
46 ms |
128348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
127832 KB |
Output is correct |
2 |
Correct |
15 ms |
127832 KB |
Output is correct |
3 |
Correct |
15 ms |
127832 KB |
Output is correct |
4 |
Correct |
15 ms |
127832 KB |
Output is correct |
5 |
Correct |
16 ms |
127832 KB |
Output is correct |
6 |
Correct |
15 ms |
127836 KB |
Output is correct |
7 |
Correct |
15 ms |
127832 KB |
Output is correct |
8 |
Correct |
15 ms |
128088 KB |
Output is correct |
9 |
Correct |
16 ms |
127832 KB |
Output is correct |
10 |
Correct |
16 ms |
127784 KB |
Output is correct |
11 |
Correct |
16 ms |
127832 KB |
Output is correct |
12 |
Correct |
15 ms |
127832 KB |
Output is correct |
13 |
Correct |
16 ms |
127832 KB |
Output is correct |
14 |
Correct |
16 ms |
127832 KB |
Output is correct |
15 |
Correct |
16 ms |
127832 KB |
Output is correct |
16 |
Correct |
17 ms |
127832 KB |
Output is correct |
17 |
Correct |
17 ms |
127832 KB |
Output is correct |
18 |
Correct |
15 ms |
127832 KB |
Output is correct |
19 |
Correct |
15 ms |
127836 KB |
Output is correct |
20 |
Correct |
17 ms |
127832 KB |
Output is correct |
21 |
Correct |
15 ms |
127832 KB |
Output is correct |
22 |
Correct |
15 ms |
127832 KB |
Output is correct |
23 |
Correct |
16 ms |
127832 KB |
Output is correct |
24 |
Correct |
17 ms |
127832 KB |
Output is correct |
25 |
Correct |
16 ms |
127836 KB |
Output is correct |
26 |
Correct |
15 ms |
127832 KB |
Output is correct |
27 |
Correct |
15 ms |
127836 KB |
Output is correct |
28 |
Correct |
17 ms |
127832 KB |
Output is correct |
29 |
Correct |
20 ms |
127780 KB |
Output is correct |
30 |
Correct |
16 ms |
127832 KB |
Output is correct |
31 |
Correct |
19 ms |
127832 KB |
Output is correct |
32 |
Correct |
18 ms |
127832 KB |
Output is correct |
33 |
Correct |
27 ms |
127848 KB |
Output is correct |
34 |
Correct |
26 ms |
127832 KB |
Output is correct |
35 |
Correct |
24 ms |
128088 KB |
Output is correct |
36 |
Correct |
16 ms |
127836 KB |
Output is correct |
37 |
Correct |
30 ms |
128448 KB |
Output is correct |
38 |
Correct |
28 ms |
128088 KB |
Output is correct |
39 |
Correct |
29 ms |
128088 KB |
Output is correct |
40 |
Correct |
29 ms |
128084 KB |
Output is correct |
41 |
Correct |
28 ms |
128088 KB |
Output is correct |
42 |
Correct |
18 ms |
128088 KB |
Output is correct |
43 |
Correct |
18 ms |
128088 KB |
Output is correct |
44 |
Correct |
19 ms |
128088 KB |
Output is correct |
45 |
Correct |
40 ms |
128488 KB |
Output is correct |
46 |
Correct |
40 ms |
128336 KB |
Output is correct |
47 |
Correct |
61 ms |
128588 KB |
Output is correct |
48 |
Correct |
20 ms |
128088 KB |
Output is correct |
49 |
Correct |
18 ms |
128088 KB |
Output is correct |
50 |
Correct |
18 ms |
128080 KB |
Output is correct |
51 |
Correct |
44 ms |
128856 KB |
Output is correct |
52 |
Correct |
50 ms |
128844 KB |
Output is correct |
53 |
Correct |
26 ms |
128344 KB |
Output is correct |
54 |
Correct |
16 ms |
127832 KB |
Output is correct |
55 |
Correct |
16 ms |
127724 KB |
Output is correct |
56 |
Correct |
22 ms |
128600 KB |
Output is correct |
57 |
Correct |
64 ms |
127936 KB |
Output is correct |
58 |
Correct |
23 ms |
128088 KB |
Output is correct |
59 |
Correct |
22 ms |
128076 KB |
Output is correct |
60 |
Correct |
25 ms |
128088 KB |
Output is correct |
61 |
Correct |
23 ms |
128092 KB |
Output is correct |
62 |
Correct |
35 ms |
128600 KB |
Output is correct |
63 |
Correct |
43 ms |
129824 KB |
Output is correct |
64 |
Correct |
42 ms |
129744 KB |
Output is correct |
65 |
Correct |
50 ms |
131268 KB |
Output is correct |
66 |
Correct |
374 ms |
131116 KB |
Output is correct |
67 |
Correct |
368 ms |
132296 KB |
Output is correct |