#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) begin(x), end(x)
#define SZ(x) (int)(x).size()
#define cps(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int n0 = 30003, inf = 1e9, k0 = 7500000;
int n, m, num, ind[n0];
vector <int> rem[n0], vec[n0], out[n0];
int g1[k0], g2[k0], g3[k0], b[k0], d[k0];
deque <int> q;
void relax0(int v, int u) {
if (d[u] > d[v]) {
d[u] = d[v];
q.push_front(u);
}
}
void relax1(int v, int u) {
if (d[u] > d[v] + 1) {
d[u] = d[v] + 1;
q.pb(u);
}
}
void bfs(int x) {
q.pb(x);
for (int i = 0; i < num; i++)
d[i] = inf;
d[x] = 0;
while (!q.empty()) {
int v = q.front();
q.pop_front();
if (v < n) {
for (int i : out[v]) relax0(v, i);
} else {
if (g1[v]) relax1(v, v - 1);
if (g2[v]) relax1(v, v + 1);
relax0(v, g3[v]);
}
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
cin >> n >> m;
int s, f;
for (int i = 0; i < m; i++) {
int B, P;
cin >> B >> P;
if (i == 0) s = B;
if (i == 1) f = B;
vec[P].pb(B);
rem[P].pb(B % P);
}
num = n;
for (int i = 1; i < n0; i++) {
cps(rem[i]);
for (int j : rem[i])
num += ((n - 1) - j) / i + 1;
}
assert(num < 7500000);
int cur = n;
for (int i = 1; i < n0; i++) {
for (int j : rem[i]) {
for (int k = j; k < n; k += i) {
g1[cur] = (k != j);
g2[cur] = (k + i < n);
g3[cur] = k;
ind[k] = cur++;
}
}
cps(vec[i]);
for (int j : vec[i])
out[j].pb(ind[j]);
}
bfs(s);
if (d[f] == inf) cout << -1;
else cout << d[f];
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:83:9: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
83 | if (d[f] == inf) cout << -1;
| ~~~^
skyscraper.cpp:82:5: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
82 | bfs(s);
| ~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2560 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
2 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
2 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
2 ms |
2464 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
3 ms |
2432 KB |
Output is correct |
8 |
Correct |
2 ms |
2432 KB |
Output is correct |
9 |
Correct |
2 ms |
2432 KB |
Output is correct |
10 |
Correct |
3 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2560 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
2 ms |
2432 KB |
Output is correct |
6 |
Correct |
3 ms |
2432 KB |
Output is correct |
7 |
Correct |
2 ms |
2432 KB |
Output is correct |
8 |
Correct |
2 ms |
2432 KB |
Output is correct |
9 |
Correct |
2 ms |
2432 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2560 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
4 ms |
2688 KB |
Output is correct |
16 |
Correct |
3 ms |
2560 KB |
Output is correct |
17 |
Correct |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
3 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2560 KB |
Output is correct |
20 |
Correct |
3 ms |
2560 KB |
Output is correct |
21 |
Correct |
3 ms |
2560 KB |
Output is correct |
22 |
Correct |
3 ms |
2688 KB |
Output is correct |
23 |
Correct |
3 ms |
2688 KB |
Output is correct |
24 |
Correct |
4 ms |
2816 KB |
Output is correct |
25 |
Correct |
3 ms |
2688 KB |
Output is correct |
26 |
Correct |
4 ms |
2688 KB |
Output is correct |
27 |
Correct |
3 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2944 KB |
Output is correct |
29 |
Correct |
4 ms |
3456 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
31 |
Correct |
3 ms |
3072 KB |
Output is correct |
32 |
Correct |
3 ms |
2944 KB |
Output is correct |
33 |
Correct |
7 ms |
4480 KB |
Output is correct |
34 |
Correct |
8 ms |
4480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
3 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
2 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
2 ms |
2432 KB |
Output is correct |
8 |
Correct |
2 ms |
2432 KB |
Output is correct |
9 |
Correct |
2 ms |
2432 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2560 KB |
Output is correct |
12 |
Correct |
3 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
3 ms |
2560 KB |
Output is correct |
17 |
Correct |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
3 ms |
2720 KB |
Output is correct |
19 |
Correct |
3 ms |
2688 KB |
Output is correct |
20 |
Correct |
3 ms |
2560 KB |
Output is correct |
21 |
Correct |
3 ms |
2560 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
3 ms |
2688 KB |
Output is correct |
24 |
Correct |
4 ms |
2816 KB |
Output is correct |
25 |
Correct |
4 ms |
2688 KB |
Output is correct |
26 |
Correct |
3 ms |
2688 KB |
Output is correct |
27 |
Correct |
4 ms |
2688 KB |
Output is correct |
28 |
Correct |
4 ms |
2944 KB |
Output is correct |
29 |
Correct |
4 ms |
3456 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
31 |
Correct |
4 ms |
3072 KB |
Output is correct |
32 |
Correct |
3 ms |
2944 KB |
Output is correct |
33 |
Correct |
8 ms |
4480 KB |
Output is correct |
34 |
Correct |
7 ms |
4480 KB |
Output is correct |
35 |
Correct |
14 ms |
4736 KB |
Output is correct |
36 |
Correct |
4 ms |
2944 KB |
Output is correct |
37 |
Correct |
15 ms |
5760 KB |
Output is correct |
38 |
Correct |
19 ms |
5760 KB |
Output is correct |
39 |
Correct |
18 ms |
5760 KB |
Output is correct |
40 |
Correct |
18 ms |
5760 KB |
Output is correct |
41 |
Correct |
18 ms |
5760 KB |
Output is correct |
42 |
Correct |
7 ms |
2944 KB |
Output is correct |
43 |
Correct |
8 ms |
2944 KB |
Output is correct |
44 |
Correct |
8 ms |
2816 KB |
Output is correct |
45 |
Correct |
34 ms |
10624 KB |
Output is correct |
46 |
Correct |
34 ms |
10624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
2 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
2 ms |
2432 KB |
Output is correct |
8 |
Correct |
3 ms |
2432 KB |
Output is correct |
9 |
Correct |
2 ms |
2432 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2560 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
3 ms |
2560 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
3 ms |
2688 KB |
Output is correct |
19 |
Correct |
3 ms |
2560 KB |
Output is correct |
20 |
Correct |
3 ms |
2560 KB |
Output is correct |
21 |
Correct |
3 ms |
2560 KB |
Output is correct |
22 |
Correct |
3 ms |
2688 KB |
Output is correct |
23 |
Correct |
3 ms |
2688 KB |
Output is correct |
24 |
Correct |
4 ms |
2816 KB |
Output is correct |
25 |
Correct |
4 ms |
2688 KB |
Output is correct |
26 |
Correct |
3 ms |
2688 KB |
Output is correct |
27 |
Correct |
3 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2944 KB |
Output is correct |
29 |
Correct |
4 ms |
3456 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
31 |
Correct |
3 ms |
3072 KB |
Output is correct |
32 |
Correct |
3 ms |
2944 KB |
Output is correct |
33 |
Correct |
6 ms |
4480 KB |
Output is correct |
34 |
Correct |
6 ms |
4480 KB |
Output is correct |
35 |
Correct |
14 ms |
4736 KB |
Output is correct |
36 |
Correct |
5 ms |
2944 KB |
Output is correct |
37 |
Correct |
15 ms |
5760 KB |
Output is correct |
38 |
Correct |
19 ms |
5760 KB |
Output is correct |
39 |
Correct |
18 ms |
5760 KB |
Output is correct |
40 |
Correct |
18 ms |
5760 KB |
Output is correct |
41 |
Correct |
18 ms |
5760 KB |
Output is correct |
42 |
Correct |
8 ms |
2944 KB |
Output is correct |
43 |
Correct |
8 ms |
2944 KB |
Output is correct |
44 |
Correct |
8 ms |
2944 KB |
Output is correct |
45 |
Correct |
34 ms |
10652 KB |
Output is correct |
46 |
Correct |
36 ms |
10744 KB |
Output is correct |
47 |
Correct |
49 ms |
14976 KB |
Output is correct |
48 |
Correct |
16 ms |
7680 KB |
Output is correct |
49 |
Correct |
16 ms |
6400 KB |
Output is correct |
50 |
Correct |
12 ms |
6016 KB |
Output is correct |
51 |
Correct |
34 ms |
9472 KB |
Output is correct |
52 |
Correct |
40 ms |
9984 KB |
Output is correct |
53 |
Correct |
21 ms |
5236 KB |
Output is correct |
54 |
Correct |
3 ms |
3200 KB |
Output is correct |
55 |
Correct |
3 ms |
3584 KB |
Output is correct |
56 |
Correct |
14 ms |
4352 KB |
Output is correct |
57 |
Correct |
20 ms |
9600 KB |
Output is correct |
58 |
Correct |
9 ms |
4096 KB |
Output is correct |
59 |
Correct |
13 ms |
4608 KB |
Output is correct |
60 |
Correct |
14 ms |
4992 KB |
Output is correct |
61 |
Correct |
11 ms |
4992 KB |
Output is correct |
62 |
Correct |
35 ms |
11776 KB |
Output is correct |
63 |
Correct |
170 ms |
39160 KB |
Output is correct |
64 |
Correct |
200 ms |
47224 KB |
Output is correct |
65 |
Correct |
410 ms |
66696 KB |
Output is correct |
66 |
Correct |
636 ms |
116276 KB |
Output is correct |
67 |
Correct |
667 ms |
116360 KB |
Output is correct |