#include <deque>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> B(M), P(M);
vector<vector<int> > vmod(N);
for (int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
if(P[i] < N) vmod[P[i]].push_back(B[i]);
}
vector<vector<int> > g(N);
for (int i = 1; i < N; i++) {
sort(vmod[i].begin(), vmod[i].end(), [&](int j, int k) { return j % i != k % i ? j % i < k % i : j < k; });
vmod[i].erase(unique(vmod[i].begin(), vmod[i].end()), vmod[i].end());
int pl = 0;
for (int j = 1; j <= vmod[i].size(); j++) {
if (j != vmod[i].size() && vmod[i][j] % i == vmod[i][j - 1] % i) continue;
int pre = -1;
for (int k = vmod[i][j - 1] % i; k < N; k += i) {
if (pre != -1) g[pre].push_back(g.size() * 2 + 1);
g.push_back(pre == -1 ? vector<int>({ k * 2 }) : vector<int>({ k * 2, pre * 2 + 1 }));
pre = g.size() - 1;
while (pl != j && vmod[i][pl] < k) pl++;
if (pl != j && vmod[i][pl] == k) g[k].push_back(pre * 2);
}
pl = j;
}
}
vector<int> dist(g.size(), -1); dist[B[0]] = 0;
deque<int> que; que.push_back(B[0]);
while (!que.empty()) {
int u = que.front(); que.pop_front();
for (int e : g[u]) {
if (dist[e >> 1] == -1) {
dist[e >> 1] = dist[u] + (e & 1);
if (e & 1) que.push_back(e >> 1);
else que.push_front(e >> 1);
}
}
}
cout << dist[B[1]] << '\n';
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:22:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 1; j <= vmod[i].size(); j++) {
~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:23:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j != vmod[i].size() && vmod[i][j] % i == vmod[i][j - 1] % i) continue;
~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
2 ms |
456 KB |
Output is correct |
4 |
Correct |
2 ms |
456 KB |
Output is correct |
5 |
Correct |
2 ms |
500 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
564 KB |
Output is correct |
2 |
Correct |
2 ms |
564 KB |
Output is correct |
3 |
Correct |
2 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
748 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
2 ms |
748 KB |
Output is correct |
11 |
Correct |
3 ms |
892 KB |
Output is correct |
12 |
Correct |
2 ms |
892 KB |
Output is correct |
13 |
Correct |
2 ms |
892 KB |
Output is correct |
14 |
Correct |
3 ms |
1020 KB |
Output is correct |
15 |
Correct |
3 ms |
1020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1020 KB |
Output is correct |
2 |
Correct |
2 ms |
1020 KB |
Output is correct |
3 |
Correct |
2 ms |
1020 KB |
Output is correct |
4 |
Correct |
2 ms |
1020 KB |
Output is correct |
5 |
Correct |
2 ms |
1020 KB |
Output is correct |
6 |
Correct |
2 ms |
1020 KB |
Output is correct |
7 |
Correct |
2 ms |
1020 KB |
Output is correct |
8 |
Correct |
2 ms |
1020 KB |
Output is correct |
9 |
Correct |
2 ms |
1020 KB |
Output is correct |
10 |
Correct |
3 ms |
1020 KB |
Output is correct |
11 |
Correct |
4 ms |
1020 KB |
Output is correct |
12 |
Correct |
3 ms |
1020 KB |
Output is correct |
13 |
Correct |
2 ms |
1020 KB |
Output is correct |
14 |
Correct |
4 ms |
1020 KB |
Output is correct |
15 |
Correct |
4 ms |
1020 KB |
Output is correct |
16 |
Correct |
2 ms |
1020 KB |
Output is correct |
17 |
Correct |
5 ms |
1520 KB |
Output is correct |
18 |
Correct |
3 ms |
1520 KB |
Output is correct |
19 |
Correct |
3 ms |
1520 KB |
Output is correct |
20 |
Correct |
3 ms |
1520 KB |
Output is correct |
21 |
Correct |
2 ms |
1520 KB |
Output is correct |
22 |
Correct |
4 ms |
1520 KB |
Output is correct |
23 |
Correct |
3 ms |
1520 KB |
Output is correct |
24 |
Correct |
5 ms |
1520 KB |
Output is correct |
25 |
Correct |
4 ms |
1520 KB |
Output is correct |
26 |
Correct |
4 ms |
1520 KB |
Output is correct |
27 |
Correct |
3 ms |
1520 KB |
Output is correct |
28 |
Correct |
6 ms |
2072 KB |
Output is correct |
29 |
Correct |
12 ms |
4308 KB |
Output is correct |
30 |
Correct |
5 ms |
4308 KB |
Output is correct |
31 |
Correct |
8 ms |
4308 KB |
Output is correct |
32 |
Correct |
7 ms |
4308 KB |
Output is correct |
33 |
Correct |
25 ms |
7796 KB |
Output is correct |
34 |
Correct |
24 ms |
7796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7796 KB |
Output is correct |
2 |
Correct |
2 ms |
7796 KB |
Output is correct |
3 |
Correct |
2 ms |
7796 KB |
Output is correct |
4 |
Correct |
2 ms |
7796 KB |
Output is correct |
5 |
Correct |
2 ms |
7796 KB |
Output is correct |
6 |
Correct |
2 ms |
7796 KB |
Output is correct |
7 |
Correct |
2 ms |
7796 KB |
Output is correct |
8 |
Correct |
2 ms |
7796 KB |
Output is correct |
9 |
Correct |
2 ms |
7796 KB |
Output is correct |
10 |
Correct |
2 ms |
7796 KB |
Output is correct |
11 |
Correct |
3 ms |
7796 KB |
Output is correct |
12 |
Correct |
2 ms |
7796 KB |
Output is correct |
13 |
Correct |
2 ms |
7796 KB |
Output is correct |
14 |
Correct |
5 ms |
7796 KB |
Output is correct |
15 |
Correct |
3 ms |
7796 KB |
Output is correct |
16 |
Correct |
3 ms |
7796 KB |
Output is correct |
17 |
Correct |
5 ms |
7796 KB |
Output is correct |
18 |
Correct |
3 ms |
7796 KB |
Output is correct |
19 |
Correct |
3 ms |
7796 KB |
Output is correct |
20 |
Correct |
3 ms |
7796 KB |
Output is correct |
21 |
Correct |
2 ms |
7796 KB |
Output is correct |
22 |
Correct |
3 ms |
7796 KB |
Output is correct |
23 |
Correct |
3 ms |
7796 KB |
Output is correct |
24 |
Correct |
5 ms |
7796 KB |
Output is correct |
25 |
Correct |
4 ms |
7796 KB |
Output is correct |
26 |
Correct |
4 ms |
7796 KB |
Output is correct |
27 |
Correct |
3 ms |
7796 KB |
Output is correct |
28 |
Correct |
7 ms |
7796 KB |
Output is correct |
29 |
Correct |
13 ms |
7796 KB |
Output is correct |
30 |
Correct |
5 ms |
7796 KB |
Output is correct |
31 |
Correct |
8 ms |
7796 KB |
Output is correct |
32 |
Correct |
6 ms |
7796 KB |
Output is correct |
33 |
Correct |
25 ms |
7796 KB |
Output is correct |
34 |
Correct |
26 ms |
7796 KB |
Output is correct |
35 |
Correct |
31 ms |
8204 KB |
Output is correct |
36 |
Correct |
6 ms |
8204 KB |
Output is correct |
37 |
Correct |
50 ms |
11320 KB |
Output is correct |
38 |
Correct |
51 ms |
11320 KB |
Output is correct |
39 |
Correct |
54 ms |
11320 KB |
Output is correct |
40 |
Correct |
74 ms |
11320 KB |
Output is correct |
41 |
Correct |
53 ms |
11320 KB |
Output is correct |
42 |
Correct |
11 ms |
11320 KB |
Output is correct |
43 |
Correct |
10 ms |
11320 KB |
Output is correct |
44 |
Correct |
10 ms |
11320 KB |
Output is correct |
45 |
Correct |
160 ms |
29604 KB |
Output is correct |
46 |
Correct |
130 ms |
29616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
29616 KB |
Output is correct |
2 |
Correct |
2 ms |
29616 KB |
Output is correct |
3 |
Correct |
2 ms |
29616 KB |
Output is correct |
4 |
Correct |
2 ms |
29616 KB |
Output is correct |
5 |
Correct |
2 ms |
29616 KB |
Output is correct |
6 |
Correct |
2 ms |
29616 KB |
Output is correct |
7 |
Correct |
2 ms |
29616 KB |
Output is correct |
8 |
Correct |
2 ms |
29616 KB |
Output is correct |
9 |
Correct |
2 ms |
29616 KB |
Output is correct |
10 |
Correct |
2 ms |
29616 KB |
Output is correct |
11 |
Correct |
3 ms |
29616 KB |
Output is correct |
12 |
Correct |
2 ms |
29616 KB |
Output is correct |
13 |
Correct |
2 ms |
29616 KB |
Output is correct |
14 |
Correct |
3 ms |
29616 KB |
Output is correct |
15 |
Correct |
4 ms |
29616 KB |
Output is correct |
16 |
Correct |
2 ms |
29616 KB |
Output is correct |
17 |
Correct |
5 ms |
29616 KB |
Output is correct |
18 |
Correct |
3 ms |
29616 KB |
Output is correct |
19 |
Correct |
3 ms |
29616 KB |
Output is correct |
20 |
Correct |
3 ms |
29616 KB |
Output is correct |
21 |
Correct |
2 ms |
29616 KB |
Output is correct |
22 |
Correct |
3 ms |
29616 KB |
Output is correct |
23 |
Correct |
3 ms |
29616 KB |
Output is correct |
24 |
Correct |
5 ms |
29616 KB |
Output is correct |
25 |
Correct |
4 ms |
29616 KB |
Output is correct |
26 |
Correct |
4 ms |
29616 KB |
Output is correct |
27 |
Correct |
3 ms |
29616 KB |
Output is correct |
28 |
Correct |
6 ms |
29616 KB |
Output is correct |
29 |
Correct |
13 ms |
29616 KB |
Output is correct |
30 |
Correct |
5 ms |
29616 KB |
Output is correct |
31 |
Correct |
8 ms |
29616 KB |
Output is correct |
32 |
Correct |
7 ms |
29616 KB |
Output is correct |
33 |
Correct |
26 ms |
29616 KB |
Output is correct |
34 |
Correct |
44 ms |
29616 KB |
Output is correct |
35 |
Correct |
33 ms |
29616 KB |
Output is correct |
36 |
Correct |
6 ms |
29616 KB |
Output is correct |
37 |
Correct |
52 ms |
29616 KB |
Output is correct |
38 |
Correct |
54 ms |
29616 KB |
Output is correct |
39 |
Correct |
54 ms |
29616 KB |
Output is correct |
40 |
Correct |
55 ms |
29616 KB |
Output is correct |
41 |
Correct |
54 ms |
29616 KB |
Output is correct |
42 |
Correct |
11 ms |
29616 KB |
Output is correct |
43 |
Correct |
11 ms |
29616 KB |
Output is correct |
44 |
Correct |
10 ms |
29616 KB |
Output is correct |
45 |
Correct |
132 ms |
29616 KB |
Output is correct |
46 |
Correct |
138 ms |
29616 KB |
Output is correct |
47 |
Correct |
246 ms |
45340 KB |
Output is correct |
48 |
Correct |
58 ms |
45340 KB |
Output is correct |
49 |
Correct |
36 ms |
45340 KB |
Output is correct |
50 |
Correct |
36 ms |
45340 KB |
Output is correct |
51 |
Correct |
102 ms |
45340 KB |
Output is correct |
52 |
Correct |
115 ms |
45340 KB |
Output is correct |
53 |
Correct |
37 ms |
45340 KB |
Output is correct |
54 |
Correct |
9 ms |
45340 KB |
Output is correct |
55 |
Correct |
12 ms |
45340 KB |
Output is correct |
56 |
Correct |
26 ms |
45340 KB |
Output is correct |
57 |
Correct |
106 ms |
45340 KB |
Output is correct |
58 |
Correct |
23 ms |
45340 KB |
Output is correct |
59 |
Correct |
35 ms |
45340 KB |
Output is correct |
60 |
Correct |
35 ms |
45340 KB |
Output is correct |
61 |
Correct |
35 ms |
45340 KB |
Output is correct |
62 |
Correct |
123 ms |
45340 KB |
Output is correct |
63 |
Correct |
827 ms |
151320 KB |
Output is correct |
64 |
Correct |
999 ms |
167332 KB |
Output is correct |
65 |
Execution timed out |
1099 ms |
262144 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |