#include <iostream>
#include <queue>
#include <vector>
#include <map>
using namespace std;
int N, M, A[30009], P[30009]; vector<short>G[30009]; vector<int>dist[30009]; vector<vector<int>> X[30009];
map<pair<int, int>, int>M1; queue<pair<int, int>>Q;
void connect(int px, int py, int qx, int qy) {
X[px][py].emplace_back((qx << 15) + qy);
}
int constant(int u1, int u2) {
return (u1 << 15) + u2;
}
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++) G[0].push_back(i);
X[0].resize(G[0].size());
dist[0].resize(G[0].size(), (1 << 30));
for (int i = 1; i <= M; i++) cin >> A[i] >> P[i];
for (int i = 1; i <= M; i++) {
int J = M1[make_pair(A[i] % P[i], P[i])];
if (J == 0) {
M1[make_pair(A[i] % P[i], P[i])] = i; J = i; int BB = A[i] % P[i];
G[i].resize(((N - 1) - BB) / P[i] + 1);
int CC = 0; for (int j = A[i] % P[i]; j < N; j += P[i]) { G[i][CC] = j; CC++; }
X[i].resize(G[i].size());
dist[i].resize(G[i].size(), (1 << 30));
for (int j = 0; j < G[i].size(); j++) {
if (j == 0 || j + 1 == G[i].size()) X[i][j].resize(1);
else X[i][j].resize(2);
int cnt = 0;
if (j + 1 < G[i].size()) {
//X[i][j][cnt] = constant(i, j + 1); cnt++;
X[i][j][cnt] = constant(0, G[i][j + 1]); cnt++;
}
if (j >= 1) {
//X[i][j][cnt] = constant(i, j - 1); cnt++;
X[i][j][cnt] = constant(0, G[i][j - 1]); cnt++;
}
}
}
int pos1 = lower_bound(G[J].begin(), G[J].end(), A[i]) - G[J].begin();
if (A[i] + P[i] < N) connect(0, A[i], 0, A[i] + P[i]);
if (A[i] - P[i] >= 0) connect(0, A[i], 0, A[i] - P[i]);
if (pos1 >= 1) connect(0, A[i], J, pos1 - 1);
if (pos1 + 1 < G[J].size()) connect(0, A[i], J, pos1 + 1);
}
dist[0][A[1]] = 0; Q.push(make_pair(0, A[1]));
while (!Q.empty()) {
int pos1 = Q.front().first, pos2 = Q.front().second; Q.pop();
if (pos1 == 0 && pos2 == A[2]) break;
for (int i = 0; i < X[pos1][pos2].size(); i++) {
int D = X[pos1][pos2][i];
int to1 = (D >> 15), to2 = (D & 32767);
if (dist[to1][to2] > dist[pos1][pos2] + 1) {
dist[to1][to2] = dist[pos1][pos2] + 1;
Q.push(make_pair(to1, to2));
}
}
if (pos1 >= 1) {
for (int j = -1; j <= 1; j += 2) {
int to1 = pos1, to2 = pos2 + j; if (to2 < 0 || to2 >= G[pos1].size()) continue;
if (dist[to1][to2] > dist[pos1][pos2] + 1) {
dist[to1][to2] = dist[pos1][pos2] + 1;
Q.push(make_pair(to1, to2));
}
}
}
}
if (dist[0][A[2]] == (1 << 30)) dist[0][A[2]] = -1;
cout << dist[0][A[2]] << endl;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:33:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < G[i].size(); j++) {
~~^~~~~~~~~~~~~
skyscraper.cpp:34:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j == 0 || j + 1 == G[i].size()) X[i][j].resize(1);
~~~~~~^~~~~~~~~~~~~~
skyscraper.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j + 1 < G[i].size()) {
~~~~~~^~~~~~~~~~~~~
skyscraper.cpp:52:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (pos1 + 1 < G[J].size()) connect(0, A[i], J, pos1 + 1);
~~~~~~~~~^~~~~~~~~~~~~
skyscraper.cpp:59:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < X[pos1][pos2].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:69:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
int to1 = pos1, to2 = pos2 + j; if (to2 < 0 || to2 >= G[pos1].size()) continue;
~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2424 KB |
Output is correct |
2 |
Correct |
3 ms |
2536 KB |
Output is correct |
3 |
Correct |
3 ms |
2536 KB |
Output is correct |
4 |
Correct |
3 ms |
2536 KB |
Output is correct |
5 |
Correct |
4 ms |
2692 KB |
Output is correct |
6 |
Correct |
3 ms |
2696 KB |
Output is correct |
7 |
Correct |
3 ms |
2696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2696 KB |
Output is correct |
2 |
Correct |
3 ms |
2704 KB |
Output is correct |
3 |
Correct |
3 ms |
2704 KB |
Output is correct |
4 |
Correct |
3 ms |
2704 KB |
Output is correct |
5 |
Correct |
3 ms |
2704 KB |
Output is correct |
6 |
Correct |
3 ms |
2704 KB |
Output is correct |
7 |
Correct |
3 ms |
2704 KB |
Output is correct |
8 |
Correct |
4 ms |
2704 KB |
Output is correct |
9 |
Correct |
3 ms |
2704 KB |
Output is correct |
10 |
Correct |
4 ms |
2752 KB |
Output is correct |
11 |
Correct |
6 ms |
3152 KB |
Output is correct |
12 |
Correct |
4 ms |
3152 KB |
Output is correct |
13 |
Correct |
4 ms |
3152 KB |
Output is correct |
14 |
Correct |
7 ms |
3324 KB |
Output is correct |
15 |
Correct |
6 ms |
3324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3324 KB |
Output is correct |
2 |
Correct |
3 ms |
3324 KB |
Output is correct |
3 |
Correct |
3 ms |
3324 KB |
Output is correct |
4 |
Correct |
3 ms |
3324 KB |
Output is correct |
5 |
Correct |
3 ms |
3324 KB |
Output is correct |
6 |
Correct |
3 ms |
3324 KB |
Output is correct |
7 |
Correct |
3 ms |
3324 KB |
Output is correct |
8 |
Correct |
3 ms |
3324 KB |
Output is correct |
9 |
Correct |
3 ms |
3324 KB |
Output is correct |
10 |
Correct |
4 ms |
3324 KB |
Output is correct |
11 |
Correct |
6 ms |
3324 KB |
Output is correct |
12 |
Correct |
6 ms |
3324 KB |
Output is correct |
13 |
Correct |
4 ms |
3324 KB |
Output is correct |
14 |
Correct |
7 ms |
3324 KB |
Output is correct |
15 |
Correct |
6 ms |
3324 KB |
Output is correct |
16 |
Correct |
5 ms |
3324 KB |
Output is correct |
17 |
Correct |
8 ms |
3940 KB |
Output is correct |
18 |
Correct |
6 ms |
3940 KB |
Output is correct |
19 |
Correct |
5 ms |
3940 KB |
Output is correct |
20 |
Correct |
6 ms |
3940 KB |
Output is correct |
21 |
Correct |
5 ms |
3940 KB |
Output is correct |
22 |
Correct |
5 ms |
3940 KB |
Output is correct |
23 |
Correct |
6 ms |
3940 KB |
Output is correct |
24 |
Correct |
8 ms |
3940 KB |
Output is correct |
25 |
Correct |
7 ms |
3940 KB |
Output is correct |
26 |
Correct |
6 ms |
3940 KB |
Output is correct |
27 |
Correct |
6 ms |
3940 KB |
Output is correct |
28 |
Correct |
10 ms |
4468 KB |
Output is correct |
29 |
Correct |
14 ms |
6524 KB |
Output is correct |
30 |
Correct |
7 ms |
6524 KB |
Output is correct |
31 |
Correct |
9 ms |
6524 KB |
Output is correct |
32 |
Correct |
8 ms |
6524 KB |
Output is correct |
33 |
Correct |
49 ms |
10364 KB |
Output is correct |
34 |
Correct |
26 ms |
10364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10364 KB |
Output is correct |
2 |
Correct |
4 ms |
10364 KB |
Output is correct |
3 |
Correct |
3 ms |
10364 KB |
Output is correct |
4 |
Correct |
3 ms |
10364 KB |
Output is correct |
5 |
Correct |
3 ms |
10364 KB |
Output is correct |
6 |
Correct |
3 ms |
10364 KB |
Output is correct |
7 |
Correct |
3 ms |
10364 KB |
Output is correct |
8 |
Correct |
3 ms |
10364 KB |
Output is correct |
9 |
Correct |
3 ms |
10364 KB |
Output is correct |
10 |
Correct |
4 ms |
10364 KB |
Output is correct |
11 |
Correct |
6 ms |
10364 KB |
Output is correct |
12 |
Correct |
4 ms |
10364 KB |
Output is correct |
13 |
Correct |
4 ms |
10364 KB |
Output is correct |
14 |
Correct |
7 ms |
10364 KB |
Output is correct |
15 |
Correct |
6 ms |
10364 KB |
Output is correct |
16 |
Correct |
5 ms |
10364 KB |
Output is correct |
17 |
Correct |
7 ms |
10364 KB |
Output is correct |
18 |
Correct |
5 ms |
10364 KB |
Output is correct |
19 |
Correct |
5 ms |
10364 KB |
Output is correct |
20 |
Correct |
5 ms |
10364 KB |
Output is correct |
21 |
Correct |
5 ms |
10364 KB |
Output is correct |
22 |
Correct |
5 ms |
10364 KB |
Output is correct |
23 |
Correct |
6 ms |
10364 KB |
Output is correct |
24 |
Correct |
8 ms |
10364 KB |
Output is correct |
25 |
Correct |
7 ms |
10364 KB |
Output is correct |
26 |
Correct |
5 ms |
10364 KB |
Output is correct |
27 |
Correct |
6 ms |
10364 KB |
Output is correct |
28 |
Correct |
10 ms |
10364 KB |
Output is correct |
29 |
Correct |
15 ms |
10364 KB |
Output is correct |
30 |
Correct |
6 ms |
10364 KB |
Output is correct |
31 |
Correct |
9 ms |
10364 KB |
Output is correct |
32 |
Correct |
7 ms |
10364 KB |
Output is correct |
33 |
Correct |
28 ms |
10364 KB |
Output is correct |
34 |
Correct |
24 ms |
10364 KB |
Output is correct |
35 |
Correct |
68 ms |
12720 KB |
Output is correct |
36 |
Correct |
12 ms |
12720 KB |
Output is correct |
37 |
Correct |
66 ms |
15868 KB |
Output is correct |
38 |
Correct |
79 ms |
16884 KB |
Output is correct |
39 |
Correct |
76 ms |
16884 KB |
Output is correct |
40 |
Correct |
76 ms |
16884 KB |
Output is correct |
41 |
Correct |
90 ms |
16884 KB |
Output is correct |
42 |
Correct |
22 ms |
16884 KB |
Output is correct |
43 |
Correct |
22 ms |
16884 KB |
Output is correct |
44 |
Correct |
22 ms |
16884 KB |
Output is correct |
45 |
Correct |
220 ms |
35964 KB |
Output is correct |
46 |
Correct |
188 ms |
36092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
36092 KB |
Output is correct |
2 |
Correct |
3 ms |
36092 KB |
Output is correct |
3 |
Correct |
3 ms |
36092 KB |
Output is correct |
4 |
Correct |
3 ms |
36092 KB |
Output is correct |
5 |
Correct |
3 ms |
36092 KB |
Output is correct |
6 |
Correct |
3 ms |
36092 KB |
Output is correct |
7 |
Correct |
3 ms |
36092 KB |
Output is correct |
8 |
Correct |
3 ms |
36092 KB |
Output is correct |
9 |
Correct |
3 ms |
36092 KB |
Output is correct |
10 |
Correct |
4 ms |
36092 KB |
Output is correct |
11 |
Correct |
6 ms |
36092 KB |
Output is correct |
12 |
Correct |
4 ms |
36092 KB |
Output is correct |
13 |
Correct |
4 ms |
36092 KB |
Output is correct |
14 |
Correct |
7 ms |
36092 KB |
Output is correct |
15 |
Correct |
6 ms |
36092 KB |
Output is correct |
16 |
Correct |
5 ms |
36092 KB |
Output is correct |
17 |
Correct |
7 ms |
36092 KB |
Output is correct |
18 |
Correct |
5 ms |
36092 KB |
Output is correct |
19 |
Correct |
4 ms |
36092 KB |
Output is correct |
20 |
Correct |
5 ms |
36092 KB |
Output is correct |
21 |
Correct |
5 ms |
36092 KB |
Output is correct |
22 |
Correct |
5 ms |
36092 KB |
Output is correct |
23 |
Correct |
6 ms |
36092 KB |
Output is correct |
24 |
Correct |
8 ms |
36092 KB |
Output is correct |
25 |
Correct |
8 ms |
36092 KB |
Output is correct |
26 |
Correct |
5 ms |
36092 KB |
Output is correct |
27 |
Correct |
6 ms |
36092 KB |
Output is correct |
28 |
Correct |
9 ms |
36092 KB |
Output is correct |
29 |
Correct |
13 ms |
36092 KB |
Output is correct |
30 |
Correct |
6 ms |
36092 KB |
Output is correct |
31 |
Correct |
9 ms |
36092 KB |
Output is correct |
32 |
Correct |
7 ms |
36092 KB |
Output is correct |
33 |
Correct |
29 ms |
36092 KB |
Output is correct |
34 |
Correct |
24 ms |
36092 KB |
Output is correct |
35 |
Correct |
60 ms |
36092 KB |
Output is correct |
36 |
Correct |
11 ms |
36092 KB |
Output is correct |
37 |
Correct |
58 ms |
36092 KB |
Output is correct |
38 |
Correct |
81 ms |
36092 KB |
Output is correct |
39 |
Correct |
67 ms |
36092 KB |
Output is correct |
40 |
Correct |
83 ms |
36092 KB |
Output is correct |
41 |
Correct |
77 ms |
36092 KB |
Output is correct |
42 |
Correct |
22 ms |
36092 KB |
Output is correct |
43 |
Correct |
22 ms |
36092 KB |
Output is correct |
44 |
Correct |
21 ms |
36092 KB |
Output is correct |
45 |
Correct |
237 ms |
36164 KB |
Output is correct |
46 |
Correct |
186 ms |
36164 KB |
Output is correct |
47 |
Correct |
155 ms |
51068 KB |
Output is correct |
48 |
Correct |
80 ms |
51068 KB |
Output is correct |
49 |
Correct |
64 ms |
51068 KB |
Output is correct |
50 |
Correct |
48 ms |
51068 KB |
Output is correct |
51 |
Correct |
113 ms |
51068 KB |
Output is correct |
52 |
Correct |
135 ms |
51068 KB |
Output is correct |
53 |
Correct |
67 ms |
51068 KB |
Output is correct |
54 |
Correct |
8 ms |
51068 KB |
Output is correct |
55 |
Correct |
11 ms |
51068 KB |
Output is correct |
56 |
Correct |
37 ms |
51068 KB |
Output is correct |
57 |
Correct |
54 ms |
51068 KB |
Output is correct |
58 |
Correct |
27 ms |
51068 KB |
Output is correct |
59 |
Correct |
37 ms |
51068 KB |
Output is correct |
60 |
Correct |
38 ms |
51068 KB |
Output is correct |
61 |
Correct |
41 ms |
51068 KB |
Output is correct |
62 |
Correct |
164 ms |
51068 KB |
Output is correct |
63 |
Correct |
721 ms |
145824 KB |
Output is correct |
64 |
Correct |
866 ms |
177276 KB |
Output is correct |
65 |
Execution timed out |
1088 ms |
252156 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |