#include <deque>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct edge {
int to, cost;
edge() : to(-1), cost(-1) {};
edge(int to_, int cost_) : to(to_), cost(cost_) {};
};
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<edge> > 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(edge(g.size(), 1));
g.push_back(pre == -1 ? vector<edge>({ edge(k, 0) }) : vector<edge>({ edge(k, 0), edge(pre, 1) }));
pre = g.size() - 1;
while (pl != j && vmod[i][pl] < k) pl++;
if (pl != j && vmod[i][pl] == k) g[k].push_back(edge(pre, 0));
}
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 (edge e : g[u]) {
if (dist[e.to] == -1) {
dist[e.to] = dist[u] + e.cost;
if (e.cost == 0) que.push_front(e.to);
else que.push_back(e.to);
}
}
}
cout << dist[B[1]] << '\n';
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:27:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 1; j <= vmod[i].size(); j++) {
~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:28: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 |
244 KB |
Output is correct |
2 |
Correct |
2 ms |
356 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
692 KB |
Output is correct |
3 |
Correct |
2 ms |
692 KB |
Output is correct |
4 |
Correct |
2 ms |
692 KB |
Output is correct |
5 |
Correct |
2 ms |
692 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
2 ms |
696 KB |
Output is correct |
8 |
Correct |
2 ms |
696 KB |
Output is correct |
9 |
Correct |
2 ms |
696 KB |
Output is correct |
10 |
Correct |
2 ms |
696 KB |
Output is correct |
11 |
Correct |
3 ms |
836 KB |
Output is correct |
12 |
Correct |
2 ms |
836 KB |
Output is correct |
13 |
Correct |
2 ms |
836 KB |
Output is correct |
14 |
Correct |
3 ms |
1008 KB |
Output is correct |
15 |
Correct |
3 ms |
1008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1008 KB |
Output is correct |
2 |
Correct |
2 ms |
1008 KB |
Output is correct |
3 |
Correct |
2 ms |
1008 KB |
Output is correct |
4 |
Correct |
2 ms |
1008 KB |
Output is correct |
5 |
Correct |
2 ms |
1008 KB |
Output is correct |
6 |
Correct |
2 ms |
1008 KB |
Output is correct |
7 |
Correct |
2 ms |
1008 KB |
Output is correct |
8 |
Correct |
2 ms |
1008 KB |
Output is correct |
9 |
Correct |
2 ms |
1008 KB |
Output is correct |
10 |
Correct |
2 ms |
1008 KB |
Output is correct |
11 |
Correct |
4 ms |
1008 KB |
Output is correct |
12 |
Correct |
2 ms |
1008 KB |
Output is correct |
13 |
Correct |
2 ms |
1008 KB |
Output is correct |
14 |
Correct |
3 ms |
1096 KB |
Output is correct |
15 |
Correct |
3 ms |
1096 KB |
Output is correct |
16 |
Correct |
3 ms |
1096 KB |
Output is correct |
17 |
Correct |
6 ms |
1884 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1884 KB |
Output is correct |
21 |
Correct |
3 ms |
1884 KB |
Output is correct |
22 |
Correct |
5 ms |
1884 KB |
Output is correct |
23 |
Correct |
4 ms |
1884 KB |
Output is correct |
24 |
Correct |
5 ms |
1884 KB |
Output is correct |
25 |
Correct |
5 ms |
1884 KB |
Output is correct |
26 |
Correct |
4 ms |
1884 KB |
Output is correct |
27 |
Correct |
4 ms |
1884 KB |
Output is correct |
28 |
Correct |
11 ms |
2376 KB |
Output is correct |
29 |
Correct |
14 ms |
5208 KB |
Output is correct |
30 |
Correct |
5 ms |
5208 KB |
Output is correct |
31 |
Correct |
14 ms |
5208 KB |
Output is correct |
32 |
Correct |
7 ms |
5208 KB |
Output is correct |
33 |
Correct |
28 ms |
9588 KB |
Output is correct |
34 |
Correct |
28 ms |
9588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9588 KB |
Output is correct |
2 |
Correct |
2 ms |
9588 KB |
Output is correct |
3 |
Correct |
2 ms |
9588 KB |
Output is correct |
4 |
Correct |
2 ms |
9588 KB |
Output is correct |
5 |
Correct |
2 ms |
9588 KB |
Output is correct |
6 |
Correct |
2 ms |
9588 KB |
Output is correct |
7 |
Correct |
2 ms |
9588 KB |
Output is correct |
8 |
Correct |
2 ms |
9588 KB |
Output is correct |
9 |
Correct |
2 ms |
9588 KB |
Output is correct |
10 |
Correct |
2 ms |
9588 KB |
Output is correct |
11 |
Correct |
4 ms |
9588 KB |
Output is correct |
12 |
Correct |
2 ms |
9588 KB |
Output is correct |
13 |
Correct |
2 ms |
9588 KB |
Output is correct |
14 |
Correct |
5 ms |
9588 KB |
Output is correct |
15 |
Correct |
3 ms |
9588 KB |
Output is correct |
16 |
Correct |
3 ms |
9588 KB |
Output is correct |
17 |
Correct |
5 ms |
9588 KB |
Output is correct |
18 |
Correct |
4 ms |
9588 KB |
Output is correct |
19 |
Correct |
3 ms |
9588 KB |
Output is correct |
20 |
Correct |
3 ms |
9588 KB |
Output is correct |
21 |
Correct |
2 ms |
9588 KB |
Output is correct |
22 |
Correct |
3 ms |
9588 KB |
Output is correct |
23 |
Correct |
4 ms |
9588 KB |
Output is correct |
24 |
Correct |
5 ms |
9588 KB |
Output is correct |
25 |
Correct |
4 ms |
9588 KB |
Output is correct |
26 |
Correct |
4 ms |
9588 KB |
Output is correct |
27 |
Correct |
4 ms |
9588 KB |
Output is correct |
28 |
Correct |
7 ms |
9588 KB |
Output is correct |
29 |
Correct |
14 ms |
9588 KB |
Output is correct |
30 |
Correct |
5 ms |
9588 KB |
Output is correct |
31 |
Correct |
9 ms |
9588 KB |
Output is correct |
32 |
Correct |
7 ms |
9588 KB |
Output is correct |
33 |
Correct |
29 ms |
9588 KB |
Output is correct |
34 |
Correct |
28 ms |
9596 KB |
Output is correct |
35 |
Correct |
32 ms |
9636 KB |
Output is correct |
36 |
Correct |
6 ms |
9636 KB |
Output is correct |
37 |
Correct |
57 ms |
13960 KB |
Output is correct |
38 |
Correct |
58 ms |
13960 KB |
Output is correct |
39 |
Correct |
61 ms |
13960 KB |
Output is correct |
40 |
Correct |
72 ms |
14140 KB |
Output is correct |
41 |
Correct |
53 ms |
14272 KB |
Output is correct |
42 |
Correct |
11 ms |
14272 KB |
Output is correct |
43 |
Correct |
11 ms |
14272 KB |
Output is correct |
44 |
Correct |
10 ms |
14272 KB |
Output is correct |
45 |
Correct |
135 ms |
38652 KB |
Output is correct |
46 |
Correct |
141 ms |
38996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
38996 KB |
Output is correct |
2 |
Correct |
2 ms |
38996 KB |
Output is correct |
3 |
Correct |
2 ms |
38996 KB |
Output is correct |
4 |
Correct |
2 ms |
38996 KB |
Output is correct |
5 |
Correct |
2 ms |
38996 KB |
Output is correct |
6 |
Correct |
2 ms |
38996 KB |
Output is correct |
7 |
Correct |
2 ms |
38996 KB |
Output is correct |
8 |
Correct |
2 ms |
38996 KB |
Output is correct |
9 |
Correct |
2 ms |
38996 KB |
Output is correct |
10 |
Correct |
2 ms |
38996 KB |
Output is correct |
11 |
Correct |
3 ms |
38996 KB |
Output is correct |
12 |
Correct |
2 ms |
38996 KB |
Output is correct |
13 |
Correct |
2 ms |
38996 KB |
Output is correct |
14 |
Correct |
3 ms |
38996 KB |
Output is correct |
15 |
Correct |
3 ms |
38996 KB |
Output is correct |
16 |
Correct |
3 ms |
38996 KB |
Output is correct |
17 |
Correct |
5 ms |
38996 KB |
Output is correct |
18 |
Correct |
4 ms |
38996 KB |
Output is correct |
19 |
Correct |
3 ms |
38996 KB |
Output is correct |
20 |
Correct |
3 ms |
38996 KB |
Output is correct |
21 |
Correct |
3 ms |
38996 KB |
Output is correct |
22 |
Correct |
3 ms |
38996 KB |
Output is correct |
23 |
Correct |
3 ms |
38996 KB |
Output is correct |
24 |
Correct |
5 ms |
38996 KB |
Output is correct |
25 |
Correct |
4 ms |
38996 KB |
Output is correct |
26 |
Correct |
4 ms |
38996 KB |
Output is correct |
27 |
Correct |
5 ms |
38996 KB |
Output is correct |
28 |
Correct |
7 ms |
38996 KB |
Output is correct |
29 |
Correct |
13 ms |
38996 KB |
Output is correct |
30 |
Correct |
5 ms |
38996 KB |
Output is correct |
31 |
Correct |
12 ms |
38996 KB |
Output is correct |
32 |
Correct |
6 ms |
38996 KB |
Output is correct |
33 |
Correct |
29 ms |
38996 KB |
Output is correct |
34 |
Correct |
27 ms |
38996 KB |
Output is correct |
35 |
Correct |
36 ms |
38996 KB |
Output is correct |
36 |
Correct |
6 ms |
38996 KB |
Output is correct |
37 |
Correct |
56 ms |
38996 KB |
Output is correct |
38 |
Correct |
55 ms |
38996 KB |
Output is correct |
39 |
Correct |
56 ms |
38996 KB |
Output is correct |
40 |
Correct |
58 ms |
38996 KB |
Output is correct |
41 |
Correct |
53 ms |
38996 KB |
Output is correct |
42 |
Correct |
11 ms |
38996 KB |
Output is correct |
43 |
Correct |
11 ms |
38996 KB |
Output is correct |
44 |
Correct |
10 ms |
38996 KB |
Output is correct |
45 |
Correct |
144 ms |
41208 KB |
Output is correct |
46 |
Correct |
147 ms |
41432 KB |
Output is correct |
47 |
Correct |
279 ms |
61432 KB |
Output is correct |
48 |
Correct |
76 ms |
61432 KB |
Output is correct |
49 |
Correct |
38 ms |
61432 KB |
Output is correct |
50 |
Correct |
35 ms |
61432 KB |
Output is correct |
51 |
Correct |
109 ms |
61432 KB |
Output is correct |
52 |
Correct |
123 ms |
61432 KB |
Output is correct |
53 |
Correct |
37 ms |
61432 KB |
Output is correct |
54 |
Correct |
13 ms |
61432 KB |
Output is correct |
55 |
Correct |
12 ms |
61432 KB |
Output is correct |
56 |
Correct |
23 ms |
61432 KB |
Output is correct |
57 |
Correct |
114 ms |
61432 KB |
Output is correct |
58 |
Correct |
23 ms |
61432 KB |
Output is correct |
59 |
Correct |
31 ms |
61432 KB |
Output is correct |
60 |
Correct |
37 ms |
61432 KB |
Output is correct |
61 |
Correct |
33 ms |
61432 KB |
Output is correct |
62 |
Correct |
126 ms |
61432 KB |
Output is correct |
63 |
Correct |
933 ms |
185552 KB |
Output is correct |
64 |
Execution timed out |
1080 ms |
215720 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |