#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <functional>
#include <algorithm>
#include <cmath>
using namespace std;
int N, M, A[30009], P[30009], dist[7400009], sz; pair<int, int>vec[2400009];
vector<int> G[30009], X[2400009];
map<pair<int, int>, int>M1; queue<int>Q;
void connect(int px, int py, int qx, int qy, int cost) {
int pos1 = lower_bound(vec, vec + sz, make_pair(px, py)) - vec;
int pos2 = lower_bound(vec, vec + sz, make_pair(qx, qy)) - vec;
X[pos1].push_back(pos2);
}
int main() {
cin >> N >> M;
for (int i = 1; i <= M; i++) {
cin >> A[i] >> P[i]; G[A[i]].push_back(P[i]);
if (M1[make_pair(A[i] % P[i], P[i])] == 0) {
M1[make_pair(A[i] % P[i], P[i])] = 1;
for (int j = A[i] % P[i]; j < N; j += P[i]) { vec[sz] = make_pair(j, P[i]); sz++; }
}
}
for (int i = 0; i < N; i++) { vec[sz] = make_pair(i, 0); sz++; }
sort(vec, vec + sz);
for (int i = 0; i < sz; i++) {
int pos = vec[i].first, state = vec[i].second;
if (state == 0) {
for (int j = 0; j < G[pos].size(); j++) {
if (pos - G[pos][j] >= 0) {
connect(pos, 0, pos - G[pos][j], G[pos][j], 1);
connect(pos, 0, pos - G[pos][j], 0, 1);
}
if (pos + G[pos][j] < N) {
connect(pos, 0, pos + G[pos][j], G[pos][j], 1);
connect(pos, 0, pos + G[pos][j], 0, 1);
}
}
}
else {
if (pos - state >= 0) {
connect(pos, state, pos - state, state, 1);
connect(pos, state, pos - state, 0, 1);
}
if (pos + state < N) {
connect(pos, state, pos + state, state, 1);
connect(pos, state, pos + state, 0, 1);
}
}
}
for (int i = 0; i < sz; i++) dist[i] = (1 << 30);
int sx = lower_bound(vec, vec + sz, make_pair(A[1], 0)) - vec;
int gx = lower_bound(vec, vec + sz, make_pair(A[2], 0)) - vec;
dist[sx] = 0; Q.push(sx);
while (!Q.empty()) {
int pos = Q.front(); Q.pop();
for (int i = 0; i < X[pos].size(); i++) {
int to = X[pos][i];
if (dist[to] > dist[pos] + 1) {
dist[to] = dist[pos] + 1;
Q.push(to);
}
}
}
int ans = dist[gx]; if (ans == (1 << 30)) ans = -1;
cout << ans << endl;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:36:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < G[pos].size(); j++) {
~~^~~~~~~~~~~~~~~
skyscraper.cpp:66:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < X[pos].size(); i++) {
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
57468 KB |
Output is correct |
2 |
Correct |
46 ms |
57692 KB |
Output is correct |
3 |
Correct |
47 ms |
57692 KB |
Output is correct |
4 |
Correct |
47 ms |
57692 KB |
Output is correct |
5 |
Correct |
47 ms |
57692 KB |
Output is correct |
6 |
Correct |
47 ms |
57692 KB |
Output is correct |
7 |
Correct |
47 ms |
57692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
57692 KB |
Output is correct |
2 |
Correct |
47 ms |
57692 KB |
Output is correct |
3 |
Correct |
47 ms |
57692 KB |
Output is correct |
4 |
Correct |
47 ms |
57692 KB |
Output is correct |
5 |
Correct |
46 ms |
57692 KB |
Output is correct |
6 |
Correct |
48 ms |
57692 KB |
Output is correct |
7 |
Correct |
47 ms |
57692 KB |
Output is correct |
8 |
Correct |
47 ms |
57700 KB |
Output is correct |
9 |
Correct |
47 ms |
57704 KB |
Output is correct |
10 |
Correct |
48 ms |
57704 KB |
Output is correct |
11 |
Correct |
52 ms |
57956 KB |
Output is correct |
12 |
Correct |
50 ms |
57956 KB |
Output is correct |
13 |
Correct |
52 ms |
57956 KB |
Output is correct |
14 |
Correct |
54 ms |
58084 KB |
Output is correct |
15 |
Correct |
55 ms |
58084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
58084 KB |
Output is correct |
2 |
Correct |
50 ms |
58084 KB |
Output is correct |
3 |
Correct |
50 ms |
58084 KB |
Output is correct |
4 |
Correct |
50 ms |
58084 KB |
Output is correct |
5 |
Correct |
49 ms |
58084 KB |
Output is correct |
6 |
Correct |
47 ms |
58084 KB |
Output is correct |
7 |
Correct |
46 ms |
58084 KB |
Output is correct |
8 |
Correct |
52 ms |
58084 KB |
Output is correct |
9 |
Correct |
50 ms |
58084 KB |
Output is correct |
10 |
Correct |
52 ms |
58084 KB |
Output is correct |
11 |
Correct |
55 ms |
58084 KB |
Output is correct |
12 |
Correct |
51 ms |
58084 KB |
Output is correct |
13 |
Correct |
49 ms |
58084 KB |
Output is correct |
14 |
Correct |
53 ms |
58240 KB |
Output is correct |
15 |
Correct |
57 ms |
58240 KB |
Output is correct |
16 |
Correct |
51 ms |
58240 KB |
Output is correct |
17 |
Correct |
63 ms |
58492 KB |
Output is correct |
18 |
Correct |
54 ms |
58492 KB |
Output is correct |
19 |
Correct |
56 ms |
58492 KB |
Output is correct |
20 |
Correct |
65 ms |
58492 KB |
Output is correct |
21 |
Correct |
50 ms |
58492 KB |
Output is correct |
22 |
Correct |
53 ms |
58492 KB |
Output is correct |
23 |
Correct |
55 ms |
58492 KB |
Output is correct |
24 |
Correct |
61 ms |
58492 KB |
Output is correct |
25 |
Correct |
59 ms |
58492 KB |
Output is correct |
26 |
Correct |
57 ms |
58492 KB |
Output is correct |
27 |
Correct |
54 ms |
58492 KB |
Output is correct |
28 |
Correct |
71 ms |
58876 KB |
Output is correct |
29 |
Correct |
104 ms |
60412 KB |
Output is correct |
30 |
Correct |
63 ms |
60412 KB |
Output is correct |
31 |
Correct |
77 ms |
60412 KB |
Output is correct |
32 |
Correct |
72 ms |
60412 KB |
Output is correct |
33 |
Correct |
169 ms |
63156 KB |
Output is correct |
34 |
Correct |
171 ms |
63156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
63156 KB |
Output is correct |
2 |
Correct |
47 ms |
63156 KB |
Output is correct |
3 |
Correct |
48 ms |
63156 KB |
Output is correct |
4 |
Correct |
47 ms |
63156 KB |
Output is correct |
5 |
Correct |
47 ms |
63156 KB |
Output is correct |
6 |
Correct |
46 ms |
63156 KB |
Output is correct |
7 |
Correct |
48 ms |
63156 KB |
Output is correct |
8 |
Correct |
47 ms |
63156 KB |
Output is correct |
9 |
Correct |
48 ms |
63156 KB |
Output is correct |
10 |
Correct |
49 ms |
63156 KB |
Output is correct |
11 |
Correct |
53 ms |
63156 KB |
Output is correct |
12 |
Correct |
49 ms |
63156 KB |
Output is correct |
13 |
Correct |
49 ms |
63156 KB |
Output is correct |
14 |
Correct |
54 ms |
63156 KB |
Output is correct |
15 |
Correct |
54 ms |
63156 KB |
Output is correct |
16 |
Correct |
52 ms |
63156 KB |
Output is correct |
17 |
Correct |
62 ms |
63156 KB |
Output is correct |
18 |
Correct |
54 ms |
63156 KB |
Output is correct |
19 |
Correct |
52 ms |
63156 KB |
Output is correct |
20 |
Correct |
52 ms |
63156 KB |
Output is correct |
21 |
Correct |
50 ms |
63156 KB |
Output is correct |
22 |
Correct |
55 ms |
63156 KB |
Output is correct |
23 |
Correct |
54 ms |
63156 KB |
Output is correct |
24 |
Correct |
62 ms |
63156 KB |
Output is correct |
25 |
Correct |
56 ms |
63156 KB |
Output is correct |
26 |
Correct |
56 ms |
63156 KB |
Output is correct |
27 |
Correct |
55 ms |
63156 KB |
Output is correct |
28 |
Correct |
72 ms |
63156 KB |
Output is correct |
29 |
Correct |
106 ms |
63156 KB |
Output is correct |
30 |
Correct |
63 ms |
63156 KB |
Output is correct |
31 |
Correct |
78 ms |
63156 KB |
Output is correct |
32 |
Correct |
68 ms |
63156 KB |
Output is correct |
33 |
Correct |
174 ms |
63156 KB |
Output is correct |
34 |
Correct |
169 ms |
63156 KB |
Output is correct |
35 |
Correct |
193 ms |
64024 KB |
Output is correct |
36 |
Correct |
69 ms |
64024 KB |
Output is correct |
37 |
Correct |
295 ms |
67164 KB |
Output is correct |
38 |
Correct |
270 ms |
67196 KB |
Output is correct |
39 |
Correct |
274 ms |
67196 KB |
Output is correct |
40 |
Correct |
273 ms |
67196 KB |
Output is correct |
41 |
Correct |
275 ms |
67348 KB |
Output is correct |
42 |
Correct |
77 ms |
67348 KB |
Output is correct |
43 |
Correct |
75 ms |
67348 KB |
Output is correct |
44 |
Correct |
70 ms |
67348 KB |
Output is correct |
45 |
Correct |
693 ms |
81404 KB |
Output is correct |
46 |
Correct |
706 ms |
81404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
81404 KB |
Output is correct |
2 |
Correct |
47 ms |
81404 KB |
Output is correct |
3 |
Correct |
47 ms |
81404 KB |
Output is correct |
4 |
Correct |
46 ms |
81404 KB |
Output is correct |
5 |
Correct |
47 ms |
81404 KB |
Output is correct |
6 |
Correct |
48 ms |
81404 KB |
Output is correct |
7 |
Correct |
49 ms |
81404 KB |
Output is correct |
8 |
Correct |
48 ms |
81404 KB |
Output is correct |
9 |
Correct |
46 ms |
81404 KB |
Output is correct |
10 |
Correct |
49 ms |
81404 KB |
Output is correct |
11 |
Correct |
53 ms |
81404 KB |
Output is correct |
12 |
Correct |
48 ms |
81404 KB |
Output is correct |
13 |
Correct |
48 ms |
81404 KB |
Output is correct |
14 |
Correct |
53 ms |
81404 KB |
Output is correct |
15 |
Correct |
55 ms |
81404 KB |
Output is correct |
16 |
Correct |
52 ms |
81404 KB |
Output is correct |
17 |
Correct |
65 ms |
81404 KB |
Output is correct |
18 |
Correct |
55 ms |
81404 KB |
Output is correct |
19 |
Correct |
51 ms |
81404 KB |
Output is correct |
20 |
Correct |
52 ms |
81404 KB |
Output is correct |
21 |
Correct |
51 ms |
81404 KB |
Output is correct |
22 |
Correct |
55 ms |
81404 KB |
Output is correct |
23 |
Correct |
55 ms |
81404 KB |
Output is correct |
24 |
Correct |
62 ms |
81404 KB |
Output is correct |
25 |
Correct |
56 ms |
81404 KB |
Output is correct |
26 |
Correct |
56 ms |
81404 KB |
Output is correct |
27 |
Correct |
54 ms |
81404 KB |
Output is correct |
28 |
Correct |
71 ms |
81404 KB |
Output is correct |
29 |
Correct |
105 ms |
81404 KB |
Output is correct |
30 |
Correct |
63 ms |
81404 KB |
Output is correct |
31 |
Correct |
77 ms |
81404 KB |
Output is correct |
32 |
Correct |
69 ms |
81404 KB |
Output is correct |
33 |
Correct |
169 ms |
81404 KB |
Output is correct |
34 |
Correct |
171 ms |
81404 KB |
Output is correct |
35 |
Correct |
186 ms |
81404 KB |
Output is correct |
36 |
Correct |
69 ms |
81404 KB |
Output is correct |
37 |
Correct |
290 ms |
81404 KB |
Output is correct |
38 |
Correct |
277 ms |
81404 KB |
Output is correct |
39 |
Correct |
273 ms |
81404 KB |
Output is correct |
40 |
Correct |
275 ms |
81404 KB |
Output is correct |
41 |
Correct |
279 ms |
81404 KB |
Output is correct |
42 |
Correct |
79 ms |
81404 KB |
Output is correct |
43 |
Correct |
80 ms |
81404 KB |
Output is correct |
44 |
Correct |
73 ms |
81404 KB |
Output is correct |
45 |
Correct |
696 ms |
81404 KB |
Output is correct |
46 |
Correct |
688 ms |
81404 KB |
Output is correct |
47 |
Execution timed out |
1090 ms |
92412 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |