#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,int> ii;
const int N = 3e4;
const ll INF = 1e18;
int n, m;
int b[N+2], p[N+2];
ll dis[N+2];
vector<int> adj[N+2];
unordered_map<int,bool> lf[N+2], rg[N+2];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
adj[b[i]].push_back(p[i]);
}
for (int i = 0; i < n; i++) {
sort(adj[i].begin(),adj[i].end());
adj[i].resize(unique(adj[i].begin(),adj[i].end())-adj[i].begin());
}
for (int i = 0; i < n; i++) dis[i] = INF;
priority_queue<ii,vector<ii>,greater<ii>> pq;
pq.push({0,b[0]}); dis[b[0]] = 0;
while (!pq.empty()) {
ii cur = pq.top(); pq.pop();
ll dist = cur.fi, node = cur.se;
if (node == b[1]) {
cout << dist << '\n';
return 0;
}
if (dis[node] != dist) continue;
for (int i : adj[node]) {
if (!lf[node][i]) {
for (int j = node-i, k = 1; j >= 0; j -= i, k++) {
ll nxDist = dist+k, nxNode = j;
if (dis[nxNode] > nxDist) {
dis[nxNode] = nxDist;
pq.push({nxDist,nxNode});
}
int p = lower_bound(adj[nxNode].begin(),adj[nxNode].end(),i)-adj[nxNode].begin();
if (p < adj[nxNode].size() && adj[nxNode][p] == i) {
lf[nxNode][i] = 1;
rg[nxNode][i] = 1;
}
}
}
if (!rg[node][i]) {
for (int j = node+i, k = 1; j < n; j += i, k++) {
ll nxDist = dist+k, nxNode = j;
if (dis[nxNode] > nxDist) {
dis[nxNode] = nxDist;
pq.push({nxDist,nxNode});
}
int p = lower_bound(adj[nxNode].begin(),adj[nxNode].end(),i)-adj[nxNode].begin();
if (p < adj[nxNode].size() && adj[nxNode][p] == i) {
lf[nxNode][i] = 1;
rg[nxNode][i] = 1;
}
}
}
}
}
cout << "-1\n";
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | if (p < adj[nxNode].size() && adj[nxNode][p] == i) {
| ~~^~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:63:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if (p < adj[nxNode].size() && adj[nxNode][p] == i) {
| ~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4300 KB |
Output is correct |
2 |
Correct |
3 ms |
4328 KB |
Output is correct |
3 |
Correct |
4 ms |
4300 KB |
Output is correct |
4 |
Correct |
3 ms |
4300 KB |
Output is correct |
5 |
Correct |
3 ms |
4300 KB |
Output is correct |
6 |
Correct |
3 ms |
4300 KB |
Output is correct |
7 |
Correct |
4 ms |
4300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4300 KB |
Output is correct |
2 |
Correct |
3 ms |
4300 KB |
Output is correct |
3 |
Correct |
3 ms |
4300 KB |
Output is correct |
4 |
Correct |
3 ms |
4300 KB |
Output is correct |
5 |
Correct |
3 ms |
4300 KB |
Output is correct |
6 |
Correct |
3 ms |
4300 KB |
Output is correct |
7 |
Correct |
3 ms |
4300 KB |
Output is correct |
8 |
Correct |
3 ms |
4300 KB |
Output is correct |
9 |
Correct |
3 ms |
4300 KB |
Output is correct |
10 |
Correct |
3 ms |
4300 KB |
Output is correct |
11 |
Correct |
5 ms |
4300 KB |
Output is correct |
12 |
Correct |
4 ms |
4300 KB |
Output is correct |
13 |
Correct |
3 ms |
4300 KB |
Output is correct |
14 |
Correct |
4 ms |
4428 KB |
Output is correct |
15 |
Correct |
5 ms |
4428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4300 KB |
Output is correct |
2 |
Correct |
4 ms |
4300 KB |
Output is correct |
3 |
Correct |
3 ms |
4300 KB |
Output is correct |
4 |
Correct |
3 ms |
4300 KB |
Output is correct |
5 |
Correct |
3 ms |
4300 KB |
Output is correct |
6 |
Correct |
3 ms |
4300 KB |
Output is correct |
7 |
Correct |
3 ms |
4300 KB |
Output is correct |
8 |
Correct |
4 ms |
4216 KB |
Output is correct |
9 |
Correct |
3 ms |
4300 KB |
Output is correct |
10 |
Correct |
4 ms |
4300 KB |
Output is correct |
11 |
Correct |
3 ms |
4300 KB |
Output is correct |
12 |
Correct |
5 ms |
4300 KB |
Output is correct |
13 |
Correct |
3 ms |
4300 KB |
Output is correct |
14 |
Correct |
5 ms |
4428 KB |
Output is correct |
15 |
Correct |
5 ms |
4428 KB |
Output is correct |
16 |
Correct |
3 ms |
4300 KB |
Output is correct |
17 |
Correct |
4 ms |
4428 KB |
Output is correct |
18 |
Correct |
3 ms |
4300 KB |
Output is correct |
19 |
Correct |
3 ms |
4300 KB |
Output is correct |
20 |
Correct |
5 ms |
4684 KB |
Output is correct |
21 |
Correct |
4 ms |
4300 KB |
Output is correct |
22 |
Correct |
3 ms |
4300 KB |
Output is correct |
23 |
Correct |
4 ms |
4428 KB |
Output is correct |
24 |
Correct |
5 ms |
4556 KB |
Output is correct |
25 |
Correct |
4 ms |
4428 KB |
Output is correct |
26 |
Correct |
4 ms |
4404 KB |
Output is correct |
27 |
Correct |
4 ms |
4300 KB |
Output is correct |
28 |
Correct |
5 ms |
4556 KB |
Output is correct |
29 |
Correct |
7 ms |
4692 KB |
Output is correct |
30 |
Correct |
4 ms |
4484 KB |
Output is correct |
31 |
Correct |
6 ms |
4480 KB |
Output is correct |
32 |
Correct |
4 ms |
4556 KB |
Output is correct |
33 |
Correct |
8 ms |
4944 KB |
Output is correct |
34 |
Correct |
6 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4300 KB |
Output is correct |
2 |
Correct |
4 ms |
4260 KB |
Output is correct |
3 |
Correct |
3 ms |
4300 KB |
Output is correct |
4 |
Correct |
3 ms |
4300 KB |
Output is correct |
5 |
Correct |
3 ms |
4300 KB |
Output is correct |
6 |
Correct |
4 ms |
4300 KB |
Output is correct |
7 |
Correct |
3 ms |
4300 KB |
Output is correct |
8 |
Correct |
3 ms |
4300 KB |
Output is correct |
9 |
Correct |
3 ms |
4300 KB |
Output is correct |
10 |
Correct |
3 ms |
4300 KB |
Output is correct |
11 |
Correct |
4 ms |
4300 KB |
Output is correct |
12 |
Correct |
3 ms |
4300 KB |
Output is correct |
13 |
Correct |
4 ms |
4352 KB |
Output is correct |
14 |
Correct |
4 ms |
4428 KB |
Output is correct |
15 |
Correct |
5 ms |
4428 KB |
Output is correct |
16 |
Correct |
3 ms |
4300 KB |
Output is correct |
17 |
Correct |
4 ms |
4500 KB |
Output is correct |
18 |
Correct |
3 ms |
4300 KB |
Output is correct |
19 |
Correct |
3 ms |
4300 KB |
Output is correct |
20 |
Correct |
4 ms |
4684 KB |
Output is correct |
21 |
Correct |
3 ms |
4300 KB |
Output is correct |
22 |
Correct |
3 ms |
4300 KB |
Output is correct |
23 |
Correct |
4 ms |
4392 KB |
Output is correct |
24 |
Correct |
5 ms |
4556 KB |
Output is correct |
25 |
Correct |
5 ms |
4428 KB |
Output is correct |
26 |
Correct |
4 ms |
4300 KB |
Output is correct |
27 |
Correct |
4 ms |
4300 KB |
Output is correct |
28 |
Correct |
5 ms |
4556 KB |
Output is correct |
29 |
Correct |
6 ms |
4692 KB |
Output is correct |
30 |
Correct |
4 ms |
4428 KB |
Output is correct |
31 |
Correct |
4 ms |
4556 KB |
Output is correct |
32 |
Correct |
4 ms |
4516 KB |
Output is correct |
33 |
Correct |
8 ms |
4944 KB |
Output is correct |
34 |
Correct |
6 ms |
4952 KB |
Output is correct |
35 |
Correct |
18 ms |
6680 KB |
Output is correct |
36 |
Correct |
5 ms |
4536 KB |
Output is correct |
37 |
Correct |
15 ms |
5836 KB |
Output is correct |
38 |
Correct |
14 ms |
5580 KB |
Output is correct |
39 |
Correct |
10 ms |
4764 KB |
Output is correct |
40 |
Correct |
10 ms |
4812 KB |
Output is correct |
41 |
Correct |
12 ms |
5052 KB |
Output is correct |
42 |
Correct |
8 ms |
4812 KB |
Output is correct |
43 |
Correct |
8 ms |
4812 KB |
Output is correct |
44 |
Correct |
11 ms |
4940 KB |
Output is correct |
45 |
Correct |
22 ms |
7620 KB |
Output is correct |
46 |
Correct |
21 ms |
7624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4300 KB |
Output is correct |
2 |
Correct |
3 ms |
4300 KB |
Output is correct |
3 |
Correct |
3 ms |
4300 KB |
Output is correct |
4 |
Correct |
3 ms |
4300 KB |
Output is correct |
5 |
Correct |
3 ms |
4300 KB |
Output is correct |
6 |
Correct |
3 ms |
4300 KB |
Output is correct |
7 |
Correct |
4 ms |
4300 KB |
Output is correct |
8 |
Correct |
3 ms |
4300 KB |
Output is correct |
9 |
Correct |
3 ms |
4300 KB |
Output is correct |
10 |
Correct |
3 ms |
4300 KB |
Output is correct |
11 |
Correct |
4 ms |
4368 KB |
Output is correct |
12 |
Correct |
3 ms |
4300 KB |
Output is correct |
13 |
Correct |
3 ms |
4300 KB |
Output is correct |
14 |
Correct |
4 ms |
4428 KB |
Output is correct |
15 |
Correct |
4 ms |
4428 KB |
Output is correct |
16 |
Correct |
3 ms |
4300 KB |
Output is correct |
17 |
Correct |
4 ms |
4428 KB |
Output is correct |
18 |
Correct |
3 ms |
4300 KB |
Output is correct |
19 |
Correct |
3 ms |
4300 KB |
Output is correct |
20 |
Correct |
4 ms |
4684 KB |
Output is correct |
21 |
Correct |
4 ms |
4340 KB |
Output is correct |
22 |
Correct |
3 ms |
4300 KB |
Output is correct |
23 |
Correct |
4 ms |
4428 KB |
Output is correct |
24 |
Correct |
5 ms |
4556 KB |
Output is correct |
25 |
Correct |
4 ms |
4428 KB |
Output is correct |
26 |
Correct |
4 ms |
4300 KB |
Output is correct |
27 |
Correct |
4 ms |
4300 KB |
Output is correct |
28 |
Correct |
5 ms |
4556 KB |
Output is correct |
29 |
Correct |
6 ms |
4692 KB |
Output is correct |
30 |
Correct |
4 ms |
4556 KB |
Output is correct |
31 |
Correct |
5 ms |
4560 KB |
Output is correct |
32 |
Correct |
4 ms |
4516 KB |
Output is correct |
33 |
Correct |
8 ms |
4944 KB |
Output is correct |
34 |
Correct |
6 ms |
4944 KB |
Output is correct |
35 |
Correct |
17 ms |
6600 KB |
Output is correct |
36 |
Correct |
5 ms |
4428 KB |
Output is correct |
37 |
Correct |
14 ms |
5708 KB |
Output is correct |
38 |
Correct |
14 ms |
5580 KB |
Output is correct |
39 |
Correct |
10 ms |
4784 KB |
Output is correct |
40 |
Correct |
10 ms |
4772 KB |
Output is correct |
41 |
Correct |
12 ms |
5068 KB |
Output is correct |
42 |
Correct |
8 ms |
4792 KB |
Output is correct |
43 |
Correct |
8 ms |
4812 KB |
Output is correct |
44 |
Correct |
9 ms |
4976 KB |
Output is correct |
45 |
Correct |
21 ms |
7572 KB |
Output is correct |
46 |
Correct |
19 ms |
7620 KB |
Output is correct |
47 |
Correct |
28 ms |
6456 KB |
Output is correct |
48 |
Correct |
12 ms |
5072 KB |
Output is correct |
49 |
Correct |
10 ms |
5124 KB |
Output is correct |
50 |
Correct |
8 ms |
4988 KB |
Output is correct |
51 |
Correct |
37 ms |
8252 KB |
Output is correct |
52 |
Correct |
32 ms |
8248 KB |
Output is correct |
53 |
Correct |
15 ms |
5896 KB |
Output is correct |
54 |
Correct |
5 ms |
4940 KB |
Output is correct |
55 |
Correct |
5 ms |
4940 KB |
Output is correct |
56 |
Correct |
27 ms |
9928 KB |
Output is correct |
57 |
Correct |
6 ms |
5804 KB |
Output is correct |
58 |
Correct |
11 ms |
5444 KB |
Output is correct |
59 |
Correct |
11 ms |
5576 KB |
Output is correct |
60 |
Correct |
11 ms |
5580 KB |
Output is correct |
61 |
Correct |
14 ms |
5636 KB |
Output is correct |
62 |
Correct |
41 ms |
9768 KB |
Output is correct |
63 |
Correct |
60 ms |
9788 KB |
Output is correct |
64 |
Correct |
61 ms |
9896 KB |
Output is correct |
65 |
Correct |
73 ms |
10648 KB |
Output is correct |
66 |
Correct |
138 ms |
14364 KB |
Output is correct |
67 |
Correct |
95 ms |
14292 KB |
Output is correct |