#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e4 + 5, S = 100;
vector<int> adj[N];
int n, m;
vector<int> dog[N];
int dis[N][S + 5], b[N], p[N];
struct State {
int u, l, d;
State(int u, int l, int d): u(u), l(l), d(d) {}
bool operator < (const State& s) const {
return d > s.d;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0); cout.tie(0);
cin >> n >> m;
for (int i = 0, x; i < m; ++i)
cin >> b[i] >> p[i], dog[b[i]].emplace_back(p[i]);
priority_queue<State> q;
memset(dis, 0x3f, sizeof(dis));
if (p[0] <= S) q.emplace(b[0], p[0], 0), dis[b[0]][p[0]] = 0;
else {
for (int i = 0; b[0] + i * p[0] < n; ++i)
q.emplace(b[0] + i * p[0], S + 1, i), dis[b[0] + i * p[0]][S + 1] = i;
for (int i = 1; b[0] - i * p[0] >= 0; ++i)
q.emplace(b[0] - i * p[0], S + 1, i), dis[b[0] - i * p[0]][S + 1] = i;
}
while (!q.empty()) {
auto s = q.top(); q.pop();
if (s.d != dis[s.u][s.l]) continue;
if (s.u == b[1]) return cout << s.d, 0;
if (s.l == S + 1) {
for (int d : dog[s.u]) {
if (d <= S) {
if (s.d < dis[s.u][d]) dis[s.u][d] = s.d, q.emplace(s.u, d, s.d);
} else {
for (int i = 1; s.u + i * d < n; ++i) if (s.d + i < dis[s.u + i * d][s.l])
q.emplace(s.u + i * d, s.l, s.d + i), dis[s.u + i*d][s.l] = s.d + i;
for (int i = 1; s.u - i * d >= 0; ++i) if (s.d + i < dis[s.u - i * d][s.l])
q.emplace(s.u - i * d, s.l, s.d + i), dis[s.u - i*d][s.l] = s.d + i;
}
}
} else {
if (s.d < dis[s.u][S + 1])
dis[s.u][S+1] = s.d, q.emplace(s.u, S+1, s.d);
if (s.u + s.l < n && s.d + 1 < dis[s.u + s.l][s.l])
dis[s.u + s.l][s.l] = s.d + 1,
q.emplace(s.u + s.l, s.l, s.d + 1);
if (s.u - s.l >= 0 && s.d + 1 < dis[s.u - s.l][s.l])
dis[s.u - s.l][s.l] = s.d + 1,
q.emplace(s.u - s.l, s.l, s.d + 1);
}
}
cout << -1;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:24:19: warning: unused variable 'x' [-Wunused-variable]
24 | for (int i = 0, x; i < m; ++i)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23124 KB |
Output is correct |
2 |
Correct |
10 ms |
23224 KB |
Output is correct |
3 |
Correct |
12 ms |
23124 KB |
Output is correct |
4 |
Correct |
12 ms |
23156 KB |
Output is correct |
5 |
Correct |
9 ms |
23124 KB |
Output is correct |
6 |
Correct |
9 ms |
23144 KB |
Output is correct |
7 |
Correct |
10 ms |
23228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23124 KB |
Output is correct |
2 |
Correct |
10 ms |
23160 KB |
Output is correct |
3 |
Correct |
9 ms |
23192 KB |
Output is correct |
4 |
Correct |
13 ms |
23156 KB |
Output is correct |
5 |
Correct |
10 ms |
23128 KB |
Output is correct |
6 |
Correct |
10 ms |
23124 KB |
Output is correct |
7 |
Correct |
10 ms |
23124 KB |
Output is correct |
8 |
Correct |
11 ms |
23228 KB |
Output is correct |
9 |
Correct |
10 ms |
23124 KB |
Output is correct |
10 |
Correct |
9 ms |
23124 KB |
Output is correct |
11 |
Correct |
11 ms |
23252 KB |
Output is correct |
12 |
Correct |
9 ms |
23224 KB |
Output is correct |
13 |
Correct |
11 ms |
23252 KB |
Output is correct |
14 |
Correct |
10 ms |
23296 KB |
Output is correct |
15 |
Correct |
10 ms |
23244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23156 KB |
Output is correct |
2 |
Correct |
9 ms |
23124 KB |
Output is correct |
3 |
Correct |
10 ms |
23124 KB |
Output is correct |
4 |
Correct |
10 ms |
23220 KB |
Output is correct |
5 |
Correct |
11 ms |
23224 KB |
Output is correct |
6 |
Correct |
10 ms |
23112 KB |
Output is correct |
7 |
Correct |
10 ms |
23124 KB |
Output is correct |
8 |
Correct |
10 ms |
23148 KB |
Output is correct |
9 |
Correct |
9 ms |
23124 KB |
Output is correct |
10 |
Correct |
11 ms |
23128 KB |
Output is correct |
11 |
Correct |
10 ms |
23252 KB |
Output is correct |
12 |
Correct |
12 ms |
23160 KB |
Output is correct |
13 |
Correct |
10 ms |
23168 KB |
Output is correct |
14 |
Correct |
11 ms |
23252 KB |
Output is correct |
15 |
Correct |
11 ms |
23248 KB |
Output is correct |
16 |
Correct |
11 ms |
23248 KB |
Output is correct |
17 |
Correct |
10 ms |
23296 KB |
Output is correct |
18 |
Correct |
10 ms |
23272 KB |
Output is correct |
19 |
Correct |
10 ms |
23160 KB |
Output is correct |
20 |
Correct |
10 ms |
23252 KB |
Output is correct |
21 |
Correct |
10 ms |
23164 KB |
Output is correct |
22 |
Correct |
10 ms |
23240 KB |
Output is correct |
23 |
Correct |
10 ms |
23252 KB |
Output is correct |
24 |
Correct |
11 ms |
23216 KB |
Output is correct |
25 |
Correct |
10 ms |
23252 KB |
Output is correct |
26 |
Correct |
10 ms |
23172 KB |
Output is correct |
27 |
Correct |
10 ms |
23252 KB |
Output is correct |
28 |
Correct |
12 ms |
23252 KB |
Output is correct |
29 |
Correct |
14 ms |
23252 KB |
Output is correct |
30 |
Correct |
11 ms |
23156 KB |
Output is correct |
31 |
Correct |
12 ms |
23260 KB |
Output is correct |
32 |
Correct |
11 ms |
23252 KB |
Output is correct |
33 |
Correct |
19 ms |
23236 KB |
Output is correct |
34 |
Correct |
16 ms |
23288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23152 KB |
Output is correct |
2 |
Correct |
10 ms |
23192 KB |
Output is correct |
3 |
Correct |
9 ms |
23160 KB |
Output is correct |
4 |
Correct |
10 ms |
23156 KB |
Output is correct |
5 |
Correct |
11 ms |
23120 KB |
Output is correct |
6 |
Correct |
10 ms |
23116 KB |
Output is correct |
7 |
Correct |
10 ms |
23220 KB |
Output is correct |
8 |
Correct |
10 ms |
23124 KB |
Output is correct |
9 |
Correct |
9 ms |
23124 KB |
Output is correct |
10 |
Correct |
11 ms |
23124 KB |
Output is correct |
11 |
Correct |
10 ms |
23252 KB |
Output is correct |
12 |
Correct |
10 ms |
23252 KB |
Output is correct |
13 |
Correct |
10 ms |
23160 KB |
Output is correct |
14 |
Correct |
11 ms |
23288 KB |
Output is correct |
15 |
Correct |
10 ms |
23296 KB |
Output is correct |
16 |
Correct |
10 ms |
23160 KB |
Output is correct |
17 |
Correct |
10 ms |
23156 KB |
Output is correct |
18 |
Correct |
9 ms |
23244 KB |
Output is correct |
19 |
Correct |
10 ms |
23252 KB |
Output is correct |
20 |
Correct |
11 ms |
23244 KB |
Output is correct |
21 |
Correct |
9 ms |
23244 KB |
Output is correct |
22 |
Correct |
10 ms |
23156 KB |
Output is correct |
23 |
Correct |
11 ms |
23280 KB |
Output is correct |
24 |
Correct |
10 ms |
23252 KB |
Output is correct |
25 |
Correct |
9 ms |
23296 KB |
Output is correct |
26 |
Correct |
10 ms |
23252 KB |
Output is correct |
27 |
Correct |
10 ms |
23196 KB |
Output is correct |
28 |
Correct |
10 ms |
23300 KB |
Output is correct |
29 |
Correct |
14 ms |
23156 KB |
Output is correct |
30 |
Correct |
11 ms |
23148 KB |
Output is correct |
31 |
Correct |
13 ms |
23260 KB |
Output is correct |
32 |
Correct |
12 ms |
23124 KB |
Output is correct |
33 |
Correct |
20 ms |
23296 KB |
Output is correct |
34 |
Correct |
14 ms |
23316 KB |
Output is correct |
35 |
Correct |
15 ms |
23844 KB |
Output is correct |
36 |
Correct |
10 ms |
23292 KB |
Output is correct |
37 |
Correct |
13 ms |
23796 KB |
Output is correct |
38 |
Correct |
16 ms |
23992 KB |
Output is correct |
39 |
Correct |
15 ms |
23892 KB |
Output is correct |
40 |
Correct |
15 ms |
23932 KB |
Output is correct |
41 |
Correct |
18 ms |
23904 KB |
Output is correct |
42 |
Correct |
17 ms |
23848 KB |
Output is correct |
43 |
Correct |
14 ms |
23872 KB |
Output is correct |
44 |
Correct |
15 ms |
23840 KB |
Output is correct |
45 |
Correct |
35 ms |
24056 KB |
Output is correct |
46 |
Correct |
18 ms |
24052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23124 KB |
Output is correct |
2 |
Correct |
11 ms |
23124 KB |
Output is correct |
3 |
Correct |
9 ms |
23124 KB |
Output is correct |
4 |
Correct |
10 ms |
23124 KB |
Output is correct |
5 |
Correct |
10 ms |
23152 KB |
Output is correct |
6 |
Correct |
10 ms |
23124 KB |
Output is correct |
7 |
Correct |
10 ms |
23124 KB |
Output is correct |
8 |
Correct |
10 ms |
23124 KB |
Output is correct |
9 |
Correct |
9 ms |
23224 KB |
Output is correct |
10 |
Correct |
9 ms |
23248 KB |
Output is correct |
11 |
Correct |
10 ms |
23252 KB |
Output is correct |
12 |
Correct |
10 ms |
23188 KB |
Output is correct |
13 |
Correct |
10 ms |
23252 KB |
Output is correct |
14 |
Correct |
10 ms |
23252 KB |
Output is correct |
15 |
Correct |
11 ms |
23252 KB |
Output is correct |
16 |
Correct |
10 ms |
23252 KB |
Output is correct |
17 |
Correct |
10 ms |
23296 KB |
Output is correct |
18 |
Correct |
10 ms |
23204 KB |
Output is correct |
19 |
Correct |
9 ms |
23160 KB |
Output is correct |
20 |
Correct |
10 ms |
23260 KB |
Output is correct |
21 |
Correct |
10 ms |
23160 KB |
Output is correct |
22 |
Correct |
9 ms |
23256 KB |
Output is correct |
23 |
Correct |
10 ms |
23252 KB |
Output is correct |
24 |
Correct |
10 ms |
23268 KB |
Output is correct |
25 |
Correct |
9 ms |
23252 KB |
Output is correct |
26 |
Correct |
10 ms |
23228 KB |
Output is correct |
27 |
Correct |
10 ms |
23168 KB |
Output is correct |
28 |
Correct |
10 ms |
23212 KB |
Output is correct |
29 |
Correct |
14 ms |
23160 KB |
Output is correct |
30 |
Correct |
11 ms |
23124 KB |
Output is correct |
31 |
Correct |
12 ms |
23252 KB |
Output is correct |
32 |
Correct |
13 ms |
23160 KB |
Output is correct |
33 |
Correct |
19 ms |
23248 KB |
Output is correct |
34 |
Correct |
15 ms |
23324 KB |
Output is correct |
35 |
Correct |
15 ms |
23872 KB |
Output is correct |
36 |
Correct |
10 ms |
23252 KB |
Output is correct |
37 |
Correct |
14 ms |
23764 KB |
Output is correct |
38 |
Correct |
15 ms |
24020 KB |
Output is correct |
39 |
Correct |
14 ms |
23892 KB |
Output is correct |
40 |
Correct |
17 ms |
24020 KB |
Output is correct |
41 |
Correct |
14 ms |
23932 KB |
Output is correct |
42 |
Correct |
13 ms |
23892 KB |
Output is correct |
43 |
Correct |
14 ms |
23808 KB |
Output is correct |
44 |
Correct |
13 ms |
23876 KB |
Output is correct |
45 |
Correct |
30 ms |
24064 KB |
Output is correct |
46 |
Correct |
18 ms |
24148 KB |
Output is correct |
47 |
Correct |
16 ms |
24468 KB |
Output is correct |
48 |
Correct |
14 ms |
24020 KB |
Output is correct |
49 |
Correct |
14 ms |
23976 KB |
Output is correct |
50 |
Correct |
18 ms |
23804 KB |
Output is correct |
51 |
Correct |
23 ms |
24884 KB |
Output is correct |
52 |
Correct |
24 ms |
24860 KB |
Output is correct |
53 |
Correct |
17 ms |
24336 KB |
Output is correct |
54 |
Correct |
11 ms |
23124 KB |
Output is correct |
55 |
Correct |
11 ms |
23152 KB |
Output is correct |
56 |
Correct |
17 ms |
24584 KB |
Output is correct |
57 |
Correct |
10 ms |
23304 KB |
Output is correct |
58 |
Correct |
17 ms |
23764 KB |
Output is correct |
59 |
Correct |
16 ms |
23892 KB |
Output is correct |
60 |
Correct |
15 ms |
23892 KB |
Output is correct |
61 |
Correct |
15 ms |
23892 KB |
Output is correct |
62 |
Correct |
34 ms |
24708 KB |
Output is correct |
63 |
Correct |
42 ms |
25600 KB |
Output is correct |
64 |
Correct |
41 ms |
25528 KB |
Output is correct |
65 |
Correct |
51 ms |
25552 KB |
Output is correct |
66 |
Correct |
333 ms |
25548 KB |
Output is correct |
67 |
Correct |
120 ms |
25552 KB |
Output is correct |