#include <bits/stdc++.h>
using namespace std;
#define int long long
#define iii tuple<int, int, int>
const int N = 50007;
const int block = 316;
int n, m, b[N], v[N], dp[N][block + 7], ans = -1;
vector<int> pw[N];
priority_queue<iii, vector<iii>, greater<iii>> pq;
int32_t main (){
ios::sync_with_stdio(false); cin.tie(nullptr);
if (fopen("test.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin >> n >> m;
for (int i = 0; i < n; ++i)
for (int j = 0; j <= block + 1; ++j)
dp[i][j] = -1;
for (int i = 1; i <= m; ++i){
cin >> b[i] >> v[i];
pw[b[i]].push_back(v[i]);
if (i == 1)
pq.push({0, b[i], v[i]});
}
while (pq.size()){
auto P = pq.top(); pq.pop();
int D = get<0>(P);
int u = get<1>(P);
int t = get<2>(P); t = min(t, block + 1);
if (dp[u][t] != -1)
continue;
dp[u][t] = D;
if (u == b[2])
ans = min((ans == -1 ? (int)1e16 : ans), D);
if (t <= block){
if (u - t >= 0 && dp[u - t][t] == -1)
pq.push(make_tuple(D + 1, u - t, t));
if (u + t < n && dp[u + t][t] == -1)
pq.push(make_tuple(D + 1, u + t, t));
}
for (int X : pw[u]){
if (X <= block){
if (u - X >= 0 && dp[u - X][X] == -1)
pq.push(make_tuple(D + 1, u - X, X));
if (u + X < n && dp[u + X][X] == -1)
pq.push(make_tuple(D + 1, u + X, X));
} else {
for (int i = u - X; i >= 0; i -= X)
if (dp[i][block + 1] == -1)
pq.push(make_tuple(D + (u - i) / X, i, X));
for (int i = u + X; i < n; i += X)
if (dp[i][block + 1] == -1)
pq.push(make_tuple(D + (i - u) / X, i, X));
}
}
}
cout << ans << "\n";
}
Compilation message
skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2816 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
1 ms |
2808 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
5 ms |
3168 KB |
Output is correct |
12 |
Correct |
2 ms |
2896 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
5 ms |
3660 KB |
Output is correct |
15 |
Correct |
5 ms |
3660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2808 KB |
Output is correct |
4 |
Correct |
1 ms |
2640 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
4 ms |
3316 KB |
Output is correct |
12 |
Correct |
2 ms |
2896 KB |
Output is correct |
13 |
Correct |
1 ms |
2640 KB |
Output is correct |
14 |
Correct |
5 ms |
3828 KB |
Output is correct |
15 |
Correct |
4 ms |
3660 KB |
Output is correct |
16 |
Correct |
1 ms |
2640 KB |
Output is correct |
17 |
Correct |
4 ms |
4944 KB |
Output is correct |
18 |
Correct |
1 ms |
6736 KB |
Output is correct |
19 |
Correct |
2 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
6736 KB |
Output is correct |
21 |
Correct |
1 ms |
2640 KB |
Output is correct |
22 |
Correct |
2 ms |
6736 KB |
Output is correct |
23 |
Correct |
2 ms |
6736 KB |
Output is correct |
24 |
Correct |
3 ms |
6788 KB |
Output is correct |
25 |
Correct |
2 ms |
6736 KB |
Output is correct |
26 |
Correct |
2 ms |
6736 KB |
Output is correct |
27 |
Correct |
2 ms |
7160 KB |
Output is correct |
28 |
Correct |
2 ms |
6736 KB |
Output is correct |
29 |
Correct |
6 ms |
6908 KB |
Output is correct |
30 |
Correct |
3 ms |
6736 KB |
Output is correct |
31 |
Correct |
4 ms |
6736 KB |
Output is correct |
32 |
Correct |
3 ms |
6736 KB |
Output is correct |
33 |
Correct |
16 ms |
7760 KB |
Output is correct |
34 |
Correct |
14 ms |
7756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2808 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2640 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2808 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
5 ms |
3148 KB |
Output is correct |
12 |
Correct |
2 ms |
2896 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
5 ms |
3660 KB |
Output is correct |
15 |
Correct |
5 ms |
3660 KB |
Output is correct |
16 |
Correct |
2 ms |
2640 KB |
Output is correct |
17 |
Correct |
4 ms |
5112 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
2 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
2640 KB |
Output is correct |
22 |
Correct |
2 ms |
6736 KB |
Output is correct |
23 |
Correct |
2 ms |
6736 KB |
Output is correct |
24 |
Correct |
3 ms |
6736 KB |
Output is correct |
25 |
Correct |
2 ms |
6736 KB |
Output is correct |
26 |
Correct |
3 ms |
6736 KB |
Output is correct |
27 |
Correct |
2 ms |
6992 KB |
Output is correct |
28 |
Correct |
3 ms |
6736 KB |
Output is correct |
29 |
Correct |
7 ms |
6736 KB |
Output is correct |
30 |
Correct |
3 ms |
6736 KB |
Output is correct |
31 |
Correct |
4 ms |
6736 KB |
Output is correct |
32 |
Correct |
4 ms |
6736 KB |
Output is correct |
33 |
Correct |
15 ms |
7760 KB |
Output is correct |
34 |
Correct |
15 ms |
7756 KB |
Output is correct |
35 |
Correct |
31 ms |
9332 KB |
Output is correct |
36 |
Correct |
4 ms |
4944 KB |
Output is correct |
37 |
Correct |
61 ms |
10948 KB |
Output is correct |
38 |
Correct |
53 ms |
9264 KB |
Output is correct |
39 |
Correct |
45 ms |
9164 KB |
Output is correct |
40 |
Correct |
46 ms |
9160 KB |
Output is correct |
41 |
Correct |
47 ms |
9160 KB |
Output is correct |
42 |
Correct |
9 ms |
8396 KB |
Output is correct |
43 |
Correct |
12 ms |
9036 KB |
Output is correct |
44 |
Correct |
10 ms |
8440 KB |
Output is correct |
45 |
Correct |
329 ms |
59020 KB |
Output is correct |
46 |
Correct |
322 ms |
59048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
3148 KB |
Output is correct |
12 |
Correct |
2 ms |
3068 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
5 ms |
3584 KB |
Output is correct |
15 |
Correct |
5 ms |
3660 KB |
Output is correct |
16 |
Correct |
2 ms |
2640 KB |
Output is correct |
17 |
Correct |
4 ms |
4944 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
2 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
6904 KB |
Output is correct |
21 |
Correct |
2 ms |
2640 KB |
Output is correct |
22 |
Correct |
2 ms |
6736 KB |
Output is correct |
23 |
Correct |
3 ms |
6736 KB |
Output is correct |
24 |
Correct |
3 ms |
6736 KB |
Output is correct |
25 |
Correct |
3 ms |
6736 KB |
Output is correct |
26 |
Correct |
3 ms |
6904 KB |
Output is correct |
27 |
Correct |
3 ms |
6992 KB |
Output is correct |
28 |
Correct |
2 ms |
6736 KB |
Output is correct |
29 |
Correct |
7 ms |
6736 KB |
Output is correct |
30 |
Correct |
4 ms |
6736 KB |
Output is correct |
31 |
Correct |
5 ms |
6736 KB |
Output is correct |
32 |
Correct |
5 ms |
6852 KB |
Output is correct |
33 |
Correct |
16 ms |
7776 KB |
Output is correct |
34 |
Correct |
15 ms |
7756 KB |
Output is correct |
35 |
Correct |
31 ms |
9172 KB |
Output is correct |
36 |
Correct |
4 ms |
4944 KB |
Output is correct |
37 |
Correct |
58 ms |
10948 KB |
Output is correct |
38 |
Correct |
45 ms |
9164 KB |
Output is correct |
39 |
Correct |
46 ms |
9164 KB |
Output is correct |
40 |
Correct |
50 ms |
9160 KB |
Output is correct |
41 |
Correct |
50 ms |
9328 KB |
Output is correct |
42 |
Correct |
8 ms |
8396 KB |
Output is correct |
43 |
Correct |
13 ms |
9036 KB |
Output is correct |
44 |
Correct |
8 ms |
8440 KB |
Output is correct |
45 |
Correct |
337 ms |
59048 KB |
Output is correct |
46 |
Correct |
323 ms |
58932 KB |
Output is correct |
47 |
Correct |
149 ms |
41412 KB |
Output is correct |
48 |
Correct |
12 ms |
62800 KB |
Output is correct |
49 |
Correct |
13 ms |
66896 KB |
Output is correct |
50 |
Correct |
21 ms |
72784 KB |
Output is correct |
51 |
Correct |
74 ms |
79436 KB |
Output is correct |
52 |
Correct |
76 ms |
79436 KB |
Output is correct |
53 |
Correct |
29 ms |
78416 KB |
Output is correct |
54 |
Correct |
23 ms |
77324 KB |
Output is correct |
55 |
Correct |
24 ms |
77392 KB |
Output is correct |
56 |
Correct |
31 ms |
78664 KB |
Output is correct |
57 |
Correct |
71 ms |
77640 KB |
Output is correct |
58 |
Correct |
28 ms |
79692 KB |
Output is correct |
59 |
Correct |
22 ms |
79052 KB |
Output is correct |
60 |
Correct |
44 ms |
79684 KB |
Output is correct |
61 |
Correct |
33 ms |
79936 KB |
Output is correct |
62 |
Correct |
36 ms |
78672 KB |
Output is correct |
63 |
Runtime error |
283 ms |
262144 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |