#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <ctime>
#include <cassert>
using namespace std;
#pragma warning (disable: 4996)
const int MAX_N = 1000000;
vector<pair<int, int>>X[30009]; map<int, int>Map[30009], Map2[30009];
int N, M, B[30009], P[30009], dist[30009], cntv; unsigned int Z[35000000];
vector<int>Q[MAX_N];
int check(int p1, int p2) {
if ((Z[p1 * 1024 + (p2 >> 5)] & (1U << (p2 & 31))) == 0) return 0;
return 1;
}
int main() {
srand((unsigned)time(NULL));
cin >> N >> M;
for (int i = 0; i < M; i++) {
scanf("%d%d", &B[i], &P[i]);
Z[(B[i] * 1024 + (P[i] >> 5))] |= (1U << (P[i] & 31));
}
for (int i = 0; i < M; i++) {
if (Map2[B[i]][P[i]] == 1) continue;
int cnt = 0;
for (int j = B[i] - P[i]; j >= 0; j -= P[i]) {
cnt++;
X[B[i]].push_back(make_pair(j, cnt)); cntv++;
if (check(j, P[i]) == 1) break;
}
cnt = 0;
for (int j = B[i] + P[i]; j < N; j += P[i]) {
cnt++;
X[B[i]].push_back(make_pair(j, cnt)); cntv++;
if (check(j, P[i]) == 1) break;
}
Map2[B[i]][P[i]] = 1;
}
for (int i = 0; i < N; i++) dist[i] = (1 << 30);
dist[B[0]] = 0; Q[0].push_back(B[0]);
cntv = 0;
for (int i = 0; i < MAX_N - 1; i++) {
for (int j = 0; j < Q[i].size(); j++) {
int pos = Q[i][j];
for (int k = 0; k < X[pos].size(); k++) {
int to = X[pos][k].first, cost = X[pos][k].second;
if (dist[to] > dist[pos] + cost) {
dist[to] = dist[pos] + cost;
Q[dist[to]].push_back(to);
}
}
}
cntv += Q[i].size();
}
if (dist[B[1]] == (1 << 30)) cout << "-1" << endl;
else cout << dist[B[1]] << endl;
return 0;
}
Compilation message
skyscraper.cpp:8:0: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning (disable: 4996)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:50:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < Q[i].size(); j++) {
~~^~~~~~~~~~~~~
skyscraper.cpp:52:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < X[pos].size(); k++) {
~~^~~~~~~~~~~~~~~
skyscraper.cpp:25:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &B[i], &P[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
27512 KB |
Output is correct |
2 |
Correct |
32 ms |
27392 KB |
Output is correct |
3 |
Correct |
34 ms |
27380 KB |
Output is correct |
4 |
Correct |
37 ms |
27384 KB |
Output is correct |
5 |
Correct |
30 ms |
27392 KB |
Output is correct |
6 |
Correct |
29 ms |
27392 KB |
Output is correct |
7 |
Correct |
30 ms |
27392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
27384 KB |
Output is correct |
2 |
Correct |
34 ms |
27384 KB |
Output is correct |
3 |
Correct |
35 ms |
27384 KB |
Output is correct |
4 |
Correct |
33 ms |
27392 KB |
Output is correct |
5 |
Correct |
35 ms |
27392 KB |
Output is correct |
6 |
Correct |
33 ms |
27520 KB |
Output is correct |
7 |
Correct |
38 ms |
27392 KB |
Output is correct |
8 |
Correct |
31 ms |
27384 KB |
Output is correct |
9 |
Correct |
32 ms |
27520 KB |
Output is correct |
10 |
Correct |
33 ms |
27776 KB |
Output is correct |
11 |
Correct |
33 ms |
27904 KB |
Output is correct |
12 |
Correct |
33 ms |
27388 KB |
Output is correct |
13 |
Correct |
36 ms |
27776 KB |
Output is correct |
14 |
Correct |
35 ms |
27768 KB |
Output is correct |
15 |
Correct |
35 ms |
27904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
27392 KB |
Output is correct |
2 |
Correct |
29 ms |
27392 KB |
Output is correct |
3 |
Correct |
31 ms |
27392 KB |
Output is correct |
4 |
Correct |
32 ms |
27392 KB |
Output is correct |
5 |
Correct |
34 ms |
27324 KB |
Output is correct |
6 |
Correct |
39 ms |
27384 KB |
Output is correct |
7 |
Correct |
33 ms |
27480 KB |
Output is correct |
8 |
Correct |
37 ms |
27392 KB |
Output is correct |
9 |
Correct |
32 ms |
27460 KB |
Output is correct |
10 |
Correct |
33 ms |
27768 KB |
Output is correct |
11 |
Correct |
36 ms |
27896 KB |
Output is correct |
12 |
Correct |
35 ms |
27392 KB |
Output is correct |
13 |
Correct |
31 ms |
27776 KB |
Output is correct |
14 |
Correct |
32 ms |
27776 KB |
Output is correct |
15 |
Correct |
32 ms |
27768 KB |
Output is correct |
16 |
Correct |
34 ms |
28288 KB |
Output is correct |
17 |
Correct |
37 ms |
30300 KB |
Output is correct |
18 |
Correct |
40 ms |
30460 KB |
Output is correct |
19 |
Correct |
33 ms |
29404 KB |
Output is correct |
20 |
Correct |
41 ms |
35584 KB |
Output is correct |
21 |
Correct |
31 ms |
28928 KB |
Output is correct |
22 |
Correct |
33 ms |
29824 KB |
Output is correct |
23 |
Correct |
36 ms |
30404 KB |
Output is correct |
24 |
Correct |
35 ms |
32376 KB |
Output is correct |
25 |
Correct |
37 ms |
32796 KB |
Output is correct |
26 |
Correct |
37 ms |
27844 KB |
Output is correct |
27 |
Correct |
39 ms |
27776 KB |
Output is correct |
28 |
Correct |
43 ms |
35704 KB |
Output is correct |
29 |
Correct |
41 ms |
28280 KB |
Output is correct |
30 |
Correct |
36 ms |
27768 KB |
Output is correct |
31 |
Correct |
40 ms |
28024 KB |
Output is correct |
32 |
Correct |
35 ms |
27904 KB |
Output is correct |
33 |
Correct |
38 ms |
29056 KB |
Output is correct |
34 |
Correct |
37 ms |
29056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
27488 KB |
Output is correct |
2 |
Correct |
31 ms |
27264 KB |
Output is correct |
3 |
Correct |
37 ms |
27384 KB |
Output is correct |
4 |
Correct |
30 ms |
27392 KB |
Output is correct |
5 |
Correct |
32 ms |
27368 KB |
Output is correct |
6 |
Correct |
31 ms |
27364 KB |
Output is correct |
7 |
Correct |
35 ms |
27384 KB |
Output is correct |
8 |
Correct |
32 ms |
27392 KB |
Output is correct |
9 |
Correct |
33 ms |
27512 KB |
Output is correct |
10 |
Correct |
33 ms |
27776 KB |
Output is correct |
11 |
Correct |
35 ms |
27896 KB |
Output is correct |
12 |
Correct |
30 ms |
27384 KB |
Output is correct |
13 |
Correct |
33 ms |
27768 KB |
Output is correct |
14 |
Correct |
31 ms |
27768 KB |
Output is correct |
15 |
Correct |
32 ms |
27776 KB |
Output is correct |
16 |
Correct |
31 ms |
28288 KB |
Output is correct |
17 |
Correct |
38 ms |
30424 KB |
Output is correct |
18 |
Correct |
36 ms |
30592 KB |
Output is correct |
19 |
Correct |
39 ms |
29440 KB |
Output is correct |
20 |
Correct |
48 ms |
35576 KB |
Output is correct |
21 |
Correct |
35 ms |
28928 KB |
Output is correct |
22 |
Correct |
36 ms |
29816 KB |
Output is correct |
23 |
Correct |
32 ms |
30464 KB |
Output is correct |
24 |
Correct |
36 ms |
32504 KB |
Output is correct |
25 |
Correct |
37 ms |
32768 KB |
Output is correct |
26 |
Correct |
34 ms |
27904 KB |
Output is correct |
27 |
Correct |
40 ms |
27772 KB |
Output is correct |
28 |
Correct |
47 ms |
35704 KB |
Output is correct |
29 |
Correct |
41 ms |
28288 KB |
Output is correct |
30 |
Correct |
36 ms |
27776 KB |
Output is correct |
31 |
Correct |
37 ms |
28024 KB |
Output is correct |
32 |
Correct |
33 ms |
27904 KB |
Output is correct |
33 |
Correct |
38 ms |
29048 KB |
Output is correct |
34 |
Correct |
36 ms |
29048 KB |
Output is correct |
35 |
Correct |
57 ms |
35576 KB |
Output is correct |
36 |
Correct |
44 ms |
32120 KB |
Output is correct |
37 |
Correct |
66 ms |
38140 KB |
Output is correct |
38 |
Correct |
66 ms |
38776 KB |
Output is correct |
39 |
Correct |
81 ms |
38780 KB |
Output is correct |
40 |
Correct |
65 ms |
38904 KB |
Output is correct |
41 |
Correct |
63 ms |
38876 KB |
Output is correct |
42 |
Correct |
40 ms |
28032 KB |
Output is correct |
43 |
Correct |
57 ms |
28024 KB |
Output is correct |
44 |
Correct |
44 ms |
35832 KB |
Output is correct |
45 |
Correct |
74 ms |
34432 KB |
Output is correct |
46 |
Correct |
75 ms |
34424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
27420 KB |
Output is correct |
2 |
Correct |
37 ms |
27384 KB |
Output is correct |
3 |
Correct |
31 ms |
27392 KB |
Output is correct |
4 |
Correct |
34 ms |
27520 KB |
Output is correct |
5 |
Correct |
34 ms |
27284 KB |
Output is correct |
6 |
Correct |
32 ms |
27392 KB |
Output is correct |
7 |
Correct |
36 ms |
27392 KB |
Output is correct |
8 |
Correct |
33 ms |
27384 KB |
Output is correct |
9 |
Correct |
32 ms |
27520 KB |
Output is correct |
10 |
Correct |
36 ms |
27768 KB |
Output is correct |
11 |
Correct |
33 ms |
27896 KB |
Output is correct |
12 |
Correct |
40 ms |
27384 KB |
Output is correct |
13 |
Correct |
33 ms |
27768 KB |
Output is correct |
14 |
Correct |
31 ms |
27776 KB |
Output is correct |
15 |
Correct |
33 ms |
27768 KB |
Output is correct |
16 |
Correct |
40 ms |
28152 KB |
Output is correct |
17 |
Correct |
36 ms |
30328 KB |
Output is correct |
18 |
Correct |
33 ms |
30592 KB |
Output is correct |
19 |
Correct |
32 ms |
29436 KB |
Output is correct |
20 |
Correct |
38 ms |
35584 KB |
Output is correct |
21 |
Correct |
37 ms |
28928 KB |
Output is correct |
22 |
Correct |
33 ms |
29792 KB |
Output is correct |
23 |
Correct |
32 ms |
30464 KB |
Output is correct |
24 |
Correct |
36 ms |
32384 KB |
Output is correct |
25 |
Correct |
34 ms |
32632 KB |
Output is correct |
26 |
Correct |
34 ms |
27904 KB |
Output is correct |
27 |
Correct |
33 ms |
27768 KB |
Output is correct |
28 |
Correct |
40 ms |
35840 KB |
Output is correct |
29 |
Correct |
36 ms |
28260 KB |
Output is correct |
30 |
Correct |
33 ms |
27776 KB |
Output is correct |
31 |
Correct |
35 ms |
28160 KB |
Output is correct |
32 |
Correct |
37 ms |
27896 KB |
Output is correct |
33 |
Correct |
36 ms |
29048 KB |
Output is correct |
34 |
Correct |
38 ms |
29080 KB |
Output is correct |
35 |
Correct |
53 ms |
35576 KB |
Output is correct |
36 |
Correct |
36 ms |
32120 KB |
Output is correct |
37 |
Correct |
59 ms |
38264 KB |
Output is correct |
38 |
Correct |
83 ms |
38680 KB |
Output is correct |
39 |
Correct |
81 ms |
38776 KB |
Output is correct |
40 |
Correct |
71 ms |
38720 KB |
Output is correct |
41 |
Correct |
66 ms |
38776 KB |
Output is correct |
42 |
Correct |
43 ms |
28032 KB |
Output is correct |
43 |
Correct |
49 ms |
27896 KB |
Output is correct |
44 |
Correct |
48 ms |
35832 KB |
Output is correct |
45 |
Correct |
70 ms |
34428 KB |
Output is correct |
46 |
Correct |
69 ms |
34424 KB |
Output is correct |
47 |
Correct |
164 ms |
80252 KB |
Output is correct |
48 |
Correct |
119 ms |
88728 KB |
Output is correct |
49 |
Correct |
120 ms |
87632 KB |
Output is correct |
50 |
Correct |
98 ms |
76124 KB |
Output is correct |
51 |
Correct |
158 ms |
109268 KB |
Output is correct |
52 |
Correct |
174 ms |
109980 KB |
Output is correct |
53 |
Correct |
140 ms |
106740 KB |
Output is correct |
54 |
Correct |
70 ms |
28536 KB |
Output is correct |
55 |
Correct |
69 ms |
28988 KB |
Output is correct |
56 |
Correct |
161 ms |
151264 KB |
Output is correct |
57 |
Correct |
92 ms |
45944 KB |
Output is correct |
58 |
Correct |
73 ms |
29644 KB |
Output is correct |
59 |
Correct |
69 ms |
30080 KB |
Output is correct |
60 |
Correct |
78 ms |
31604 KB |
Output is correct |
61 |
Correct |
71 ms |
31604 KB |
Output is correct |
62 |
Correct |
199 ms |
154116 KB |
Output is correct |
63 |
Correct |
204 ms |
55252 KB |
Output is correct |
64 |
Correct |
221 ms |
63440 KB |
Output is correct |
65 |
Correct |
228 ms |
74864 KB |
Output is correct |
66 |
Correct |
334 ms |
102332 KB |
Output is correct |
67 |
Correct |
354 ms |
101820 KB |
Output is correct |