#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]; vector<pair<int, int>>vec, X[7400009]; vector<int> G[30009];
map<pair<int, int>, int>M1; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>Q;
void connect(int px, int py, int qx, int qy, int cost) {
int pos1 = lower_bound(vec.begin(), vec.end(), make_pair(px, py)) - vec.begin();
int pos2 = lower_bound(vec.begin(), vec.end(), make_pair(qx, qy)) - vec.begin();
X[pos1].push_back(make_pair(pos2, cost));
}
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.emplace_back(make_pair(j, P[i]));
}
}
for (int i = 0; i < N; i++) vec.push_back(make_pair(i, 0));
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); i++) {
int pos = vec[i].first, state = vec[i].second;
if (state == 0) {
for (int j = 0; j < G[pos].size(); j++) connect(pos, 0, pos, G[pos][j], 0);
}
else {
connect(pos, state, pos, 0, 0);
if (pos - state >= 0) connect(pos, state, pos - state, state, 1);
if (pos + state < N) connect(pos, state, pos + state, state, 1);
}
}
for (int i = 0; i < vec.size(); i++) dist[i] = (1 << 30);
int sx = lower_bound(vec.begin(), vec.end(), make_pair(A[1], 0)) - vec.begin();
int gx = lower_bound(vec.begin(), vec.end(), make_pair(A[2], 0)) - vec.begin();
dist[sx] = 0; Q.push(make_pair(dist[sx], sx));
while (!Q.empty()) {
int pos = Q.top().second; Q.pop();
for (int i = 0; i < X[pos].size(); i++) {
int to = X[pos][i].first, cost = X[pos][i].second;
if (dist[to] > dist[pos] + cost) {
dist[to] = dist[pos] + cost;
Q.push(make_pair(dist[to], 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:31:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); i++) {
~~^~~~~~~~~~~~
skyscraper.cpp:35:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < G[pos].size(); j++) connect(pos, 0, pos, G[pos][j], 0);
~~^~~~~~~~~~~~~~~
skyscraper.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); i++) dist[i] = (1 << 30);
~~^~~~~~~~~~~~
skyscraper.cpp:51:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < X[pos].size(); i++) {
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
174840 KB |
Output is correct |
2 |
Correct |
140 ms |
174840 KB |
Output is correct |
3 |
Correct |
144 ms |
174928 KB |
Output is correct |
4 |
Correct |
147 ms |
174940 KB |
Output is correct |
5 |
Correct |
144 ms |
175040 KB |
Output is correct |
6 |
Correct |
145 ms |
175040 KB |
Output is correct |
7 |
Correct |
141 ms |
175040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
175040 KB |
Output is correct |
2 |
Correct |
145 ms |
175040 KB |
Output is correct |
3 |
Correct |
149 ms |
175112 KB |
Output is correct |
4 |
Correct |
139 ms |
175112 KB |
Output is correct |
5 |
Correct |
146 ms |
175112 KB |
Output is correct |
6 |
Correct |
141 ms |
175112 KB |
Output is correct |
7 |
Correct |
146 ms |
175112 KB |
Output is correct |
8 |
Correct |
146 ms |
175112 KB |
Output is correct |
9 |
Correct |
139 ms |
175112 KB |
Output is correct |
10 |
Correct |
146 ms |
175176 KB |
Output is correct |
11 |
Correct |
145 ms |
175408 KB |
Output is correct |
12 |
Correct |
145 ms |
175408 KB |
Output is correct |
13 |
Correct |
148 ms |
175408 KB |
Output is correct |
14 |
Correct |
152 ms |
175572 KB |
Output is correct |
15 |
Correct |
154 ms |
175604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
175604 KB |
Output is correct |
2 |
Correct |
145 ms |
175604 KB |
Output is correct |
3 |
Correct |
144 ms |
175604 KB |
Output is correct |
4 |
Correct |
150 ms |
175604 KB |
Output is correct |
5 |
Correct |
146 ms |
175604 KB |
Output is correct |
6 |
Correct |
140 ms |
175604 KB |
Output is correct |
7 |
Correct |
142 ms |
175604 KB |
Output is correct |
8 |
Correct |
146 ms |
175604 KB |
Output is correct |
9 |
Correct |
146 ms |
175604 KB |
Output is correct |
10 |
Correct |
148 ms |
175604 KB |
Output is correct |
11 |
Correct |
151 ms |
175604 KB |
Output is correct |
12 |
Correct |
141 ms |
175604 KB |
Output is correct |
13 |
Correct |
144 ms |
175604 KB |
Output is correct |
14 |
Correct |
151 ms |
175612 KB |
Output is correct |
15 |
Correct |
149 ms |
175612 KB |
Output is correct |
16 |
Correct |
148 ms |
175612 KB |
Output is correct |
17 |
Correct |
161 ms |
176092 KB |
Output is correct |
18 |
Correct |
155 ms |
176092 KB |
Output is correct |
19 |
Correct |
145 ms |
176092 KB |
Output is correct |
20 |
Correct |
152 ms |
176092 KB |
Output is correct |
21 |
Correct |
146 ms |
176092 KB |
Output is correct |
22 |
Correct |
147 ms |
176092 KB |
Output is correct |
23 |
Correct |
148 ms |
176092 KB |
Output is correct |
24 |
Correct |
157 ms |
176092 KB |
Output is correct |
25 |
Correct |
150 ms |
176092 KB |
Output is correct |
26 |
Correct |
149 ms |
176092 KB |
Output is correct |
27 |
Correct |
147 ms |
176092 KB |
Output is correct |
28 |
Correct |
165 ms |
176688 KB |
Output is correct |
29 |
Correct |
197 ms |
178772 KB |
Output is correct |
30 |
Correct |
158 ms |
178772 KB |
Output is correct |
31 |
Correct |
171 ms |
178772 KB |
Output is correct |
32 |
Correct |
169 ms |
178772 KB |
Output is correct |
33 |
Correct |
262 ms |
182272 KB |
Output is correct |
34 |
Correct |
261 ms |
182272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
182272 KB |
Output is correct |
2 |
Correct |
141 ms |
182272 KB |
Output is correct |
3 |
Correct |
141 ms |
182272 KB |
Output is correct |
4 |
Correct |
139 ms |
182272 KB |
Output is correct |
5 |
Correct |
141 ms |
182272 KB |
Output is correct |
6 |
Correct |
142 ms |
182272 KB |
Output is correct |
7 |
Correct |
139 ms |
182272 KB |
Output is correct |
8 |
Correct |
140 ms |
182272 KB |
Output is correct |
9 |
Correct |
141 ms |
182272 KB |
Output is correct |
10 |
Correct |
142 ms |
182272 KB |
Output is correct |
11 |
Correct |
146 ms |
182272 KB |
Output is correct |
12 |
Correct |
141 ms |
182272 KB |
Output is correct |
13 |
Correct |
141 ms |
182272 KB |
Output is correct |
14 |
Correct |
147 ms |
182272 KB |
Output is correct |
15 |
Correct |
146 ms |
182272 KB |
Output is correct |
16 |
Correct |
145 ms |
182272 KB |
Output is correct |
17 |
Correct |
154 ms |
182272 KB |
Output is correct |
18 |
Correct |
151 ms |
182272 KB |
Output is correct |
19 |
Correct |
145 ms |
182272 KB |
Output is correct |
20 |
Correct |
145 ms |
182272 KB |
Output is correct |
21 |
Correct |
143 ms |
182272 KB |
Output is correct |
22 |
Correct |
147 ms |
182272 KB |
Output is correct |
23 |
Correct |
148 ms |
182272 KB |
Output is correct |
24 |
Correct |
154 ms |
182272 KB |
Output is correct |
25 |
Correct |
151 ms |
182272 KB |
Output is correct |
26 |
Correct |
150 ms |
182272 KB |
Output is correct |
27 |
Correct |
150 ms |
182272 KB |
Output is correct |
28 |
Correct |
164 ms |
182272 KB |
Output is correct |
29 |
Correct |
203 ms |
182272 KB |
Output is correct |
30 |
Correct |
158 ms |
182272 KB |
Output is correct |
31 |
Correct |
170 ms |
182272 KB |
Output is correct |
32 |
Correct |
160 ms |
182272 KB |
Output is correct |
33 |
Correct |
272 ms |
182304 KB |
Output is correct |
34 |
Correct |
261 ms |
182384 KB |
Output is correct |
35 |
Correct |
292 ms |
183020 KB |
Output is correct |
36 |
Correct |
161 ms |
183020 KB |
Output is correct |
37 |
Correct |
372 ms |
186756 KB |
Output is correct |
38 |
Correct |
374 ms |
186856 KB |
Output is correct |
39 |
Correct |
360 ms |
186856 KB |
Output is correct |
40 |
Correct |
363 ms |
186856 KB |
Output is correct |
41 |
Correct |
361 ms |
186884 KB |
Output is correct |
42 |
Correct |
164 ms |
186884 KB |
Output is correct |
43 |
Correct |
165 ms |
186884 KB |
Output is correct |
44 |
Correct |
162 ms |
186884 KB |
Output is correct |
45 |
Correct |
710 ms |
205496 KB |
Output is correct |
46 |
Correct |
708 ms |
205568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
205568 KB |
Output is correct |
2 |
Correct |
140 ms |
205568 KB |
Output is correct |
3 |
Correct |
144 ms |
205568 KB |
Output is correct |
4 |
Correct |
140 ms |
205568 KB |
Output is correct |
5 |
Correct |
141 ms |
205568 KB |
Output is correct |
6 |
Correct |
143 ms |
205568 KB |
Output is correct |
7 |
Correct |
151 ms |
205568 KB |
Output is correct |
8 |
Correct |
144 ms |
205568 KB |
Output is correct |
9 |
Correct |
142 ms |
205568 KB |
Output is correct |
10 |
Correct |
143 ms |
205568 KB |
Output is correct |
11 |
Correct |
148 ms |
205568 KB |
Output is correct |
12 |
Correct |
143 ms |
205568 KB |
Output is correct |
13 |
Correct |
150 ms |
205568 KB |
Output is correct |
14 |
Correct |
147 ms |
205568 KB |
Output is correct |
15 |
Correct |
146 ms |
205568 KB |
Output is correct |
16 |
Correct |
151 ms |
205568 KB |
Output is correct |
17 |
Correct |
163 ms |
205568 KB |
Output is correct |
18 |
Correct |
151 ms |
205568 KB |
Output is correct |
19 |
Correct |
150 ms |
205568 KB |
Output is correct |
20 |
Correct |
142 ms |
205568 KB |
Output is correct |
21 |
Correct |
157 ms |
205568 KB |
Output is correct |
22 |
Correct |
147 ms |
205568 KB |
Output is correct |
23 |
Correct |
149 ms |
205568 KB |
Output is correct |
24 |
Correct |
158 ms |
205568 KB |
Output is correct |
25 |
Correct |
154 ms |
205568 KB |
Output is correct |
26 |
Correct |
149 ms |
205568 KB |
Output is correct |
27 |
Correct |
151 ms |
205568 KB |
Output is correct |
28 |
Correct |
162 ms |
205568 KB |
Output is correct |
29 |
Correct |
199 ms |
205568 KB |
Output is correct |
30 |
Correct |
156 ms |
205568 KB |
Output is correct |
31 |
Correct |
172 ms |
205568 KB |
Output is correct |
32 |
Correct |
167 ms |
205568 KB |
Output is correct |
33 |
Correct |
265 ms |
205568 KB |
Output is correct |
34 |
Correct |
280 ms |
205568 KB |
Output is correct |
35 |
Correct |
277 ms |
205568 KB |
Output is correct |
36 |
Correct |
159 ms |
205568 KB |
Output is correct |
37 |
Correct |
365 ms |
205568 KB |
Output is correct |
38 |
Correct |
370 ms |
205568 KB |
Output is correct |
39 |
Correct |
366 ms |
205568 KB |
Output is correct |
40 |
Correct |
372 ms |
205568 KB |
Output is correct |
41 |
Correct |
367 ms |
205568 KB |
Output is correct |
42 |
Correct |
173 ms |
205568 KB |
Output is correct |
43 |
Correct |
166 ms |
205568 KB |
Output is correct |
44 |
Correct |
167 ms |
205568 KB |
Output is correct |
45 |
Correct |
702 ms |
205568 KB |
Output is correct |
46 |
Correct |
696 ms |
205568 KB |
Output is correct |
47 |
Execution timed out |
1089 ms |
221140 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |