#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <functional>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>
using namespace std;
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long
const int MXN = 30001, INF = 1e9 + 5;
void solve() {
int N, M;
cin >> N >> M;
int sq = 1;
while (sq * sq < N)
sq++;
vector<int> powers[N];
int location[M];
vector<vector<int>> has(N, vector<int>(sq + 1, 0));
for (int i = 0; i < M; i++) {
int B, P;
cin >> B >> P;
location[i] = B;
if (P > sq || P <= sq && !has[B][P])
powers[B].push_back(P);
if (P <= sq)
has[B][P] = 1;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
vector<int> dist(N + 1, INF);
dist[location[0]] = 0;
pq.emplace(0, location[0]);
while (!pq.empty()) {
auto [cur_dist, u] = pq.top();
pq.pop();
if (dist[u] != cur_dist) continue;
if (u == location[1]) {
cout << dist[u];
return;
}
for (const auto &p : powers[u]) {
for (int v = u + p, jumps = 1; v < N; v += p, jumps++) {
int new_dist = cur_dist + jumps;
if (new_dist < dist[v]) {
dist[v] = new_dist;
pq.emplace(new_dist, v);
}
if (p <= sq && has[v][p]) break;
}
for (int v = u - p, jumps = 1; v >= 0; v -= p, jumps++) {
int new_dist = cur_dist + jumps;
if (new_dist < dist[v]) {
dist[v] = new_dist;
pq.emplace(new_dist, v);
}
if (p <= sq && has[v][p]) break;
}
}
}
cout << -1;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int TC = 1;
// cin >> TC;
while (TC--) solve();
}
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:51:31: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
51 | if (P > sq || P <= sq && !has[B][P])
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
1 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
844 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
3 ms |
972 KB |
Output is correct |
30 |
Correct |
2 ms |
844 KB |
Output is correct |
31 |
Correct |
2 ms |
844 KB |
Output is correct |
32 |
Correct |
2 ms |
844 KB |
Output is correct |
33 |
Correct |
4 ms |
972 KB |
Output is correct |
34 |
Correct |
4 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
1 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
844 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
3 ms |
972 KB |
Output is correct |
30 |
Correct |
2 ms |
844 KB |
Output is correct |
31 |
Correct |
2 ms |
844 KB |
Output is correct |
32 |
Correct |
2 ms |
844 KB |
Output is correct |
33 |
Correct |
3 ms |
972 KB |
Output is correct |
34 |
Correct |
4 ms |
972 KB |
Output is correct |
35 |
Correct |
7 ms |
972 KB |
Output is correct |
36 |
Correct |
2 ms |
588 KB |
Output is correct |
37 |
Correct |
7 ms |
1100 KB |
Output is correct |
38 |
Correct |
8 ms |
1228 KB |
Output is correct |
39 |
Correct |
7 ms |
1088 KB |
Output is correct |
40 |
Correct |
7 ms |
1100 KB |
Output is correct |
41 |
Correct |
7 ms |
1228 KB |
Output is correct |
42 |
Correct |
5 ms |
844 KB |
Output is correct |
43 |
Correct |
6 ms |
844 KB |
Output is correct |
44 |
Correct |
5 ms |
972 KB |
Output is correct |
45 |
Correct |
9 ms |
1228 KB |
Output is correct |
46 |
Correct |
8 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
1 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
844 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
2 ms |
716 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
2 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
716 KB |
Output is correct |
27 |
Correct |
2 ms |
716 KB |
Output is correct |
28 |
Correct |
2 ms |
844 KB |
Output is correct |
29 |
Correct |
3 ms |
972 KB |
Output is correct |
30 |
Correct |
2 ms |
844 KB |
Output is correct |
31 |
Correct |
2 ms |
844 KB |
Output is correct |
32 |
Correct |
2 ms |
844 KB |
Output is correct |
33 |
Correct |
4 ms |
972 KB |
Output is correct |
34 |
Correct |
3 ms |
972 KB |
Output is correct |
35 |
Correct |
7 ms |
972 KB |
Output is correct |
36 |
Correct |
2 ms |
588 KB |
Output is correct |
37 |
Correct |
6 ms |
1100 KB |
Output is correct |
38 |
Correct |
8 ms |
1228 KB |
Output is correct |
39 |
Correct |
9 ms |
1100 KB |
Output is correct |
40 |
Correct |
8 ms |
1056 KB |
Output is correct |
41 |
Correct |
7 ms |
1228 KB |
Output is correct |
42 |
Correct |
5 ms |
920 KB |
Output is correct |
43 |
Correct |
5 ms |
844 KB |
Output is correct |
44 |
Correct |
5 ms |
972 KB |
Output is correct |
45 |
Correct |
9 ms |
1228 KB |
Output is correct |
46 |
Correct |
8 ms |
1228 KB |
Output is correct |
47 |
Correct |
19 ms |
7600 KB |
Output is correct |
48 |
Correct |
16 ms |
16208 KB |
Output is correct |
49 |
Correct |
18 ms |
18380 KB |
Output is correct |
50 |
Correct |
17 ms |
20912 KB |
Output is correct |
51 |
Correct |
34 ms |
24300 KB |
Output is correct |
52 |
Correct |
35 ms |
24256 KB |
Output is correct |
53 |
Correct |
25 ms |
23892 KB |
Output is correct |
54 |
Correct |
16 ms |
23116 KB |
Output is correct |
55 |
Correct |
16 ms |
23232 KB |
Output is correct |
56 |
Correct |
27 ms |
23980 KB |
Output is correct |
57 |
Correct |
16 ms |
23372 KB |
Output is correct |
58 |
Correct |
22 ms |
23244 KB |
Output is correct |
59 |
Correct |
27 ms |
23500 KB |
Output is correct |
60 |
Correct |
22 ms |
23504 KB |
Output is correct |
61 |
Correct |
28 ms |
23500 KB |
Output is correct |
62 |
Correct |
36 ms |
24320 KB |
Output is correct |
63 |
Correct |
51 ms |
24664 KB |
Output is correct |
64 |
Correct |
52 ms |
24644 KB |
Output is correct |
65 |
Correct |
89 ms |
25628 KB |
Output is correct |
66 |
Correct |
187 ms |
25588 KB |
Output is correct |
67 |
Correct |
174 ms |
25692 KB |
Output is correct |