#include <bits/stdc++.h>
using namespace std;
const int N = 30005;
const int inf = 1e9;
int n, m;
int b[N], p[N];
vector<int> G[N], a[N];
vector< pair<int,int> > imp;
int *f[N];
int pt[N], *le[N], *ri[N];
int pos(int val, int d) {
int it = lower_bound(G[d].begin(), G[d].end(), val) - G[d].begin();
assert(G[d][it] == val);
return it;
}
int bfs() {
for (int i = 1; i < N; ++i) {
f[i] = new int[G[i].size()];
for (int j = 0; j < G[i].size(); ++j) f[i][j] = inf;
}
deque < pair<int,int> > q;
f[p[0]][pos(b[0], p[0])] = 0;
q.push_back(make_pair(p[0], pos(b[0], p[0])));
while(!q.empty()) {
int d = q.front().first, x = q.front().second; q.pop_front();
int cur = f[d][x];
int realX = G[d][x];
if (realX == b[1]) return cur;
if (le[d][x] > -1) {
if (f[d][le[d][x]] == inf) f[d][le[d][x]] = cur + 1, q.push_back(make_pair(d, le[d][x]));
}
if (ri[d][x] < G[d].size()) {
if (f[d][ri[d][x]] == inf) f[d][ri[d][x]] = cur + 1, q.push_back(make_pair(d, ri[d][x]));
}
while(!a[realX].empty()) {
int new_d = a[realX].back(); a[realX].pop_back();
int new_pos = pos(realX, new_d);
if (f[new_d][new_pos] == inf) {
f[new_d][new_pos] = cur;
q.push_front(make_pair(new_d, new_pos));
}
}
}
return -1;
}
void reset(int d, int val) { for (int u : G[d]) pt[u % d] = val; }
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
imp.push_back(make_pair(p[i], b[i] % p[i]));
a[b[i]].push_back(p[i]);
}
sort(imp.begin(), imp.end());
imp.resize(distance(imp.begin(), unique(imp.begin(), imp.end())));
for (auto &it : imp) {
int d = it.first, mod = it.second;
for (int i = mod; i < n; i += d) {
G[d].push_back(i);
}
}
for (int i = 1; i < N; ++i) {
sort(G[i].begin(), G[i].end());
le[i] = new int[G[i].size()];
ri[i] = new int[G[i].size()];
reset(i, -1);
for (int j = 0; j < G[i].size(); ++j) {
int cur = G[i][j]; le[i][j] = pt[cur % i]; pt[cur % i] = j;
}
reset(i, G[i].size());
for (int j = G[i].size() - 1; j >= 0; --j) {
int cur = G[i][j]; ri[i][j] = pt[cur % i]; pt[cur % i] = j;
}
}
printf("%d\n", bfs());
}
Compilation message
skyscraper.cpp: In function 'int bfs()':
skyscraper.cpp:23:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < G[i].size(); ++j) f[i][j] = inf;
^
skyscraper.cpp:39:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (ri[d][x] < G[d].size()) {
^
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:81:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < G[i].size(); ++j) {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7416 KB |
Output is correct |
2 |
Correct |
0 ms |
7416 KB |
Output is correct |
3 |
Correct |
0 ms |
7416 KB |
Output is correct |
4 |
Correct |
3 ms |
7416 KB |
Output is correct |
5 |
Correct |
0 ms |
7416 KB |
Output is correct |
6 |
Correct |
6 ms |
7416 KB |
Output is correct |
7 |
Correct |
0 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7416 KB |
Output is correct |
2 |
Correct |
6 ms |
7416 KB |
Output is correct |
3 |
Correct |
6 ms |
7416 KB |
Output is correct |
4 |
Correct |
6 ms |
7416 KB |
Output is correct |
5 |
Correct |
3 ms |
7416 KB |
Output is correct |
6 |
Correct |
6 ms |
7416 KB |
Output is correct |
7 |
Correct |
6 ms |
7416 KB |
Output is correct |
8 |
Correct |
3 ms |
7416 KB |
Output is correct |
9 |
Correct |
6 ms |
7416 KB |
Output is correct |
10 |
Correct |
6 ms |
7416 KB |
Output is correct |
11 |
Correct |
3 ms |
7548 KB |
Output is correct |
12 |
Correct |
3 ms |
7416 KB |
Output is correct |
13 |
Correct |
6 ms |
7416 KB |
Output is correct |
14 |
Correct |
3 ms |
7548 KB |
Output is correct |
15 |
Correct |
3 ms |
7548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7416 KB |
Output is correct |
2 |
Correct |
0 ms |
7416 KB |
Output is correct |
3 |
Correct |
0 ms |
7416 KB |
Output is correct |
4 |
Correct |
0 ms |
7416 KB |
Output is correct |
5 |
Correct |
3 ms |
7416 KB |
Output is correct |
6 |
Correct |
3 ms |
7416 KB |
Output is correct |
7 |
Correct |
0 ms |
7416 KB |
Output is correct |
8 |
Correct |
6 ms |
7416 KB |
Output is correct |
9 |
Correct |
6 ms |
7416 KB |
Output is correct |
10 |
Correct |
3 ms |
7416 KB |
Output is correct |
11 |
Correct |
3 ms |
7548 KB |
Output is correct |
12 |
Correct |
3 ms |
7416 KB |
Output is correct |
13 |
Correct |
0 ms |
7416 KB |
Output is correct |
14 |
Correct |
0 ms |
7548 KB |
Output is correct |
15 |
Correct |
3 ms |
7548 KB |
Output is correct |
16 |
Correct |
3 ms |
7548 KB |
Output is correct |
17 |
Correct |
6 ms |
7680 KB |
Output is correct |
18 |
Correct |
0 ms |
7548 KB |
Output is correct |
19 |
Correct |
3 ms |
7548 KB |
Output is correct |
20 |
Correct |
6 ms |
7552 KB |
Output is correct |
21 |
Correct |
6 ms |
7548 KB |
Output is correct |
22 |
Correct |
6 ms |
7548 KB |
Output is correct |
23 |
Correct |
6 ms |
7548 KB |
Output is correct |
24 |
Correct |
9 ms |
7680 KB |
Output is correct |
25 |
Correct |
6 ms |
7548 KB |
Output is correct |
26 |
Correct |
3 ms |
7548 KB |
Output is correct |
27 |
Correct |
3 ms |
7548 KB |
Output is correct |
28 |
Correct |
9 ms |
7816 KB |
Output is correct |
29 |
Correct |
9 ms |
8360 KB |
Output is correct |
30 |
Correct |
3 ms |
7680 KB |
Output is correct |
31 |
Correct |
3 ms |
7944 KB |
Output is correct |
32 |
Correct |
6 ms |
7816 KB |
Output is correct |
33 |
Correct |
16 ms |
9436 KB |
Output is correct |
34 |
Correct |
16 ms |
9436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7416 KB |
Output is correct |
2 |
Correct |
3 ms |
7416 KB |
Output is correct |
3 |
Correct |
6 ms |
7416 KB |
Output is correct |
4 |
Correct |
6 ms |
7416 KB |
Output is correct |
5 |
Correct |
6 ms |
7416 KB |
Output is correct |
6 |
Correct |
3 ms |
7416 KB |
Output is correct |
7 |
Correct |
3 ms |
7416 KB |
Output is correct |
8 |
Correct |
0 ms |
7416 KB |
Output is correct |
9 |
Correct |
6 ms |
7416 KB |
Output is correct |
10 |
Correct |
0 ms |
7416 KB |
Output is correct |
11 |
Correct |
3 ms |
7548 KB |
Output is correct |
12 |
Correct |
3 ms |
7416 KB |
Output is correct |
13 |
Correct |
6 ms |
7416 KB |
Output is correct |
14 |
Correct |
3 ms |
7548 KB |
Output is correct |
15 |
Correct |
6 ms |
7548 KB |
Output is correct |
16 |
Correct |
6 ms |
7548 KB |
Output is correct |
17 |
Correct |
0 ms |
7680 KB |
Output is correct |
18 |
Correct |
3 ms |
7548 KB |
Output is correct |
19 |
Correct |
6 ms |
7548 KB |
Output is correct |
20 |
Correct |
0 ms |
7552 KB |
Output is correct |
21 |
Correct |
3 ms |
7548 KB |
Output is correct |
22 |
Correct |
6 ms |
7548 KB |
Output is correct |
23 |
Correct |
6 ms |
7548 KB |
Output is correct |
24 |
Correct |
3 ms |
7680 KB |
Output is correct |
25 |
Correct |
6 ms |
7548 KB |
Output is correct |
26 |
Correct |
6 ms |
7548 KB |
Output is correct |
27 |
Correct |
0 ms |
7548 KB |
Output is correct |
28 |
Correct |
9 ms |
7816 KB |
Output is correct |
29 |
Correct |
6 ms |
8360 KB |
Output is correct |
30 |
Correct |
6 ms |
7680 KB |
Output is correct |
31 |
Correct |
9 ms |
7944 KB |
Output is correct |
32 |
Correct |
9 ms |
7816 KB |
Output is correct |
33 |
Correct |
19 ms |
9436 KB |
Output is correct |
34 |
Correct |
23 ms |
9436 KB |
Output is correct |
35 |
Correct |
19 ms |
9708 KB |
Output is correct |
36 |
Correct |
3 ms |
7856 KB |
Output is correct |
37 |
Correct |
23 ms |
10900 KB |
Output is correct |
38 |
Correct |
26 ms |
10772 KB |
Output is correct |
39 |
Correct |
29 ms |
10640 KB |
Output is correct |
40 |
Correct |
29 ms |
10636 KB |
Output is correct |
41 |
Correct |
29 ms |
10640 KB |
Output is correct |
42 |
Correct |
13 ms |
7968 KB |
Output is correct |
43 |
Correct |
6 ms |
7968 KB |
Output is correct |
44 |
Correct |
6 ms |
7968 KB |
Output is correct |
45 |
Correct |
73 ms |
15692 KB |
Output is correct |
46 |
Correct |
59 ms |
15684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7416 KB |
Output is correct |
2 |
Correct |
6 ms |
7416 KB |
Output is correct |
3 |
Correct |
6 ms |
7416 KB |
Output is correct |
4 |
Correct |
6 ms |
7416 KB |
Output is correct |
5 |
Correct |
6 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7416 KB |
Output is correct |
7 |
Correct |
3 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
3 ms |
7416 KB |
Output is correct |
10 |
Correct |
3 ms |
7416 KB |
Output is correct |
11 |
Correct |
6 ms |
7548 KB |
Output is correct |
12 |
Correct |
3 ms |
7416 KB |
Output is correct |
13 |
Correct |
6 ms |
7416 KB |
Output is correct |
14 |
Correct |
6 ms |
7548 KB |
Output is correct |
15 |
Correct |
3 ms |
7548 KB |
Output is correct |
16 |
Correct |
6 ms |
7548 KB |
Output is correct |
17 |
Correct |
6 ms |
7680 KB |
Output is correct |
18 |
Correct |
3 ms |
7548 KB |
Output is correct |
19 |
Correct |
0 ms |
7548 KB |
Output is correct |
20 |
Correct |
6 ms |
7552 KB |
Output is correct |
21 |
Correct |
6 ms |
7548 KB |
Output is correct |
22 |
Correct |
6 ms |
7548 KB |
Output is correct |
23 |
Correct |
3 ms |
7548 KB |
Output is correct |
24 |
Correct |
6 ms |
7680 KB |
Output is correct |
25 |
Correct |
0 ms |
7548 KB |
Output is correct |
26 |
Correct |
6 ms |
7548 KB |
Output is correct |
27 |
Correct |
3 ms |
7548 KB |
Output is correct |
28 |
Correct |
3 ms |
7816 KB |
Output is correct |
29 |
Correct |
9 ms |
8360 KB |
Output is correct |
30 |
Correct |
6 ms |
7680 KB |
Output is correct |
31 |
Correct |
6 ms |
7944 KB |
Output is correct |
32 |
Correct |
3 ms |
7816 KB |
Output is correct |
33 |
Correct |
13 ms |
9436 KB |
Output is correct |
34 |
Correct |
16 ms |
9436 KB |
Output is correct |
35 |
Correct |
19 ms |
9708 KB |
Output is correct |
36 |
Correct |
6 ms |
7856 KB |
Output is correct |
37 |
Correct |
23 ms |
10900 KB |
Output is correct |
38 |
Correct |
33 ms |
10772 KB |
Output is correct |
39 |
Correct |
29 ms |
10640 KB |
Output is correct |
40 |
Correct |
19 ms |
10636 KB |
Output is correct |
41 |
Correct |
19 ms |
10640 KB |
Output is correct |
42 |
Correct |
13 ms |
7968 KB |
Output is correct |
43 |
Correct |
13 ms |
7968 KB |
Output is correct |
44 |
Correct |
9 ms |
7968 KB |
Output is correct |
45 |
Correct |
86 ms |
15692 KB |
Output is correct |
46 |
Correct |
66 ms |
15684 KB |
Output is correct |
47 |
Correct |
63 ms |
20832 KB |
Output is correct |
48 |
Correct |
26 ms |
11840 KB |
Output is correct |
49 |
Correct |
19 ms |
10360 KB |
Output is correct |
50 |
Correct |
13 ms |
10300 KB |
Output is correct |
51 |
Correct |
39 ms |
13460 KB |
Output is correct |
52 |
Correct |
46 ms |
13924 KB |
Output is correct |
53 |
Correct |
26 ms |
9636 KB |
Output is correct |
54 |
Correct |
6 ms |
7956 KB |
Output is correct |
55 |
Correct |
3 ms |
8280 KB |
Output is correct |
56 |
Correct |
19 ms |
9152 KB |
Output is correct |
57 |
Correct |
26 ms |
14552 KB |
Output is correct |
58 |
Correct |
9 ms |
8956 KB |
Output is correct |
59 |
Correct |
9 ms |
9500 KB |
Output is correct |
60 |
Correct |
16 ms |
9876 KB |
Output is correct |
61 |
Correct |
13 ms |
9664 KB |
Output is correct |
62 |
Correct |
69 ms |
16916 KB |
Output is correct |
63 |
Correct |
523 ms |
47580 KB |
Output is correct |
64 |
Correct |
536 ms |
56960 KB |
Output is correct |
65 |
Correct |
689 ms |
77092 KB |
Output is correct |
66 |
Correct |
929 ms |
124940 KB |
Output is correct |
67 |
Correct |
776 ms |
124936 KB |
Output is correct |