#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
long long N, M, A[30009], P[30009];
vector<int>X[30009];
map<pair<long long, long long>, int>MM, MM2, dist; queue<pair<int, int>>Q;
void refresh(int to, int dst, int val) {
if (to < 0 || to >= N) return;
int F = dist[make_pair(to, dst)];
if (F == 0 || F > val) {
dist[make_pair(to, dst)] = val;
Q.push(make_pair(to, dst));
}
}
int main() {
cin >> N >> M;
for (int i = 1; i <= M; i++) {
cin >> A[i] >> P[i];
long long G = A[i] % P[i]; MM2[make_pair(A[i], P[i])] = 1;
if (MM[make_pair(G, P[i])] == 1) continue;
MM[make_pair(G, P[i])] = 1;
for (int j = G; j + P[i] < N; j++) {
X[j].push_back(j + P[i]);
X[j + P[i]].push_back(j);
}
}
Q.push(make_pair(A[1], 0)); dist[make_pair(A[1], 0)] = 1;
while (!Q.empty()) {
pair<int, int> pos1 = Q.front(); Q.pop();
int pos = pos1.first, state = pos1.second;
if (state == 0) {
for (int i = 0; i < X[pos].size(); i++) {
int to = X[pos][i], dst = abs(pos - to); if (MM2[make_pair(pos, dst)] == 0) continue;
refresh(to, dst, dist[pos1] + 1);
refresh(to, 0, dist[pos1] + 1);
}
}
else {
refresh(pos - state, state, dist[pos1] + 1);
refresh(pos + state, state, dist[pos1] + 1);
refresh(pos - state, 0, dist[pos1] + 1);
refresh(pos + state, 0, dist[pos1] + 1);
}
}
cout << dist[make_pair(A[2], 0)] - 1 << endl;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:41:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < X[pos].size(); i++) {
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1144 KB |
Output is correct |
2 |
Correct |
2 ms |
1252 KB |
Output is correct |
3 |
Correct |
2 ms |
1364 KB |
Output is correct |
4 |
Correct |
2 ms |
1364 KB |
Output is correct |
5 |
Correct |
2 ms |
1512 KB |
Output is correct |
6 |
Correct |
2 ms |
1532 KB |
Output is correct |
7 |
Correct |
2 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1540 KB |
Output is correct |
2 |
Correct |
3 ms |
1548 KB |
Output is correct |
3 |
Correct |
2 ms |
1552 KB |
Output is correct |
4 |
Correct |
2 ms |
1688 KB |
Output is correct |
5 |
Correct |
2 ms |
1736 KB |
Output is correct |
6 |
Correct |
2 ms |
1736 KB |
Output is correct |
7 |
Correct |
2 ms |
1736 KB |
Output is correct |
8 |
Correct |
2 ms |
1736 KB |
Output is correct |
9 |
Correct |
3 ms |
1816 KB |
Output is correct |
10 |
Correct |
7 ms |
2420 KB |
Output is correct |
11 |
Correct |
18 ms |
2976 KB |
Output is correct |
12 |
Correct |
4 ms |
2976 KB |
Output is correct |
13 |
Correct |
3 ms |
2976 KB |
Output is correct |
14 |
Correct |
25 ms |
3412 KB |
Output is correct |
15 |
Correct |
25 ms |
3428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3428 KB |
Output is correct |
2 |
Correct |
2 ms |
3428 KB |
Output is correct |
3 |
Correct |
2 ms |
3428 KB |
Output is correct |
4 |
Correct |
2 ms |
3428 KB |
Output is correct |
5 |
Correct |
2 ms |
3428 KB |
Output is correct |
6 |
Correct |
2 ms |
3428 KB |
Output is correct |
7 |
Correct |
2 ms |
3428 KB |
Output is correct |
8 |
Correct |
2 ms |
3428 KB |
Output is correct |
9 |
Correct |
3 ms |
3428 KB |
Output is correct |
10 |
Correct |
8 ms |
3428 KB |
Output is correct |
11 |
Correct |
18 ms |
3428 KB |
Output is correct |
12 |
Correct |
4 ms |
3428 KB |
Output is correct |
13 |
Correct |
3 ms |
3428 KB |
Output is correct |
14 |
Correct |
25 ms |
3544 KB |
Output is correct |
15 |
Correct |
25 ms |
3556 KB |
Output is correct |
16 |
Correct |
5 ms |
3556 KB |
Output is correct |
17 |
Correct |
236 ms |
28776 KB |
Output is correct |
18 |
Correct |
22 ms |
28776 KB |
Output is correct |
19 |
Correct |
17 ms |
28776 KB |
Output is correct |
20 |
Correct |
6 ms |
28776 KB |
Output is correct |
21 |
Correct |
5 ms |
28776 KB |
Output is correct |
22 |
Correct |
18 ms |
28776 KB |
Output is correct |
23 |
Correct |
216 ms |
39504 KB |
Output is correct |
24 |
Correct |
578 ms |
91028 KB |
Output is correct |
25 |
Correct |
408 ms |
91028 KB |
Output is correct |
26 |
Correct |
86 ms |
91028 KB |
Output is correct |
27 |
Correct |
128 ms |
91028 KB |
Output is correct |
28 |
Correct |
431 ms |
91028 KB |
Output is correct |
29 |
Correct |
364 ms |
91028 KB |
Output is correct |
30 |
Correct |
75 ms |
91028 KB |
Output is correct |
31 |
Correct |
226 ms |
91028 KB |
Output is correct |
32 |
Correct |
164 ms |
91028 KB |
Output is correct |
33 |
Execution timed out |
1004 ms |
91028 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
91028 KB |
Output is correct |
2 |
Correct |
2 ms |
91028 KB |
Output is correct |
3 |
Correct |
2 ms |
91028 KB |
Output is correct |
4 |
Correct |
3 ms |
91028 KB |
Output is correct |
5 |
Correct |
2 ms |
91028 KB |
Output is correct |
6 |
Correct |
2 ms |
91028 KB |
Output is correct |
7 |
Correct |
2 ms |
91028 KB |
Output is correct |
8 |
Correct |
2 ms |
91028 KB |
Output is correct |
9 |
Correct |
3 ms |
91028 KB |
Output is correct |
10 |
Correct |
7 ms |
91028 KB |
Output is correct |
11 |
Correct |
26 ms |
91028 KB |
Output is correct |
12 |
Correct |
4 ms |
91028 KB |
Output is correct |
13 |
Correct |
3 ms |
91028 KB |
Output is correct |
14 |
Correct |
25 ms |
91028 KB |
Output is correct |
15 |
Correct |
25 ms |
91028 KB |
Output is correct |
16 |
Correct |
5 ms |
91028 KB |
Output is correct |
17 |
Correct |
237 ms |
91028 KB |
Output is correct |
18 |
Correct |
23 ms |
91028 KB |
Output is correct |
19 |
Correct |
19 ms |
91028 KB |
Output is correct |
20 |
Correct |
6 ms |
91028 KB |
Output is correct |
21 |
Correct |
5 ms |
91028 KB |
Output is correct |
22 |
Correct |
18 ms |
91028 KB |
Output is correct |
23 |
Correct |
233 ms |
91028 KB |
Output is correct |
24 |
Correct |
590 ms |
91424 KB |
Output is correct |
25 |
Correct |
412 ms |
91424 KB |
Output is correct |
26 |
Correct |
86 ms |
91424 KB |
Output is correct |
27 |
Correct |
128 ms |
91424 KB |
Output is correct |
28 |
Correct |
450 ms |
91424 KB |
Output is correct |
29 |
Correct |
389 ms |
91424 KB |
Output is correct |
30 |
Correct |
79 ms |
91424 KB |
Output is correct |
31 |
Correct |
231 ms |
91424 KB |
Output is correct |
32 |
Correct |
165 ms |
91424 KB |
Output is correct |
33 |
Execution timed out |
1036 ms |
91424 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
91424 KB |
Output is correct |
2 |
Correct |
2 ms |
91424 KB |
Output is correct |
3 |
Correct |
2 ms |
91424 KB |
Output is correct |
4 |
Correct |
2 ms |
91424 KB |
Output is correct |
5 |
Correct |
2 ms |
91424 KB |
Output is correct |
6 |
Correct |
2 ms |
91424 KB |
Output is correct |
7 |
Correct |
2 ms |
91424 KB |
Output is correct |
8 |
Correct |
2 ms |
91424 KB |
Output is correct |
9 |
Correct |
3 ms |
91424 KB |
Output is correct |
10 |
Correct |
8 ms |
91424 KB |
Output is correct |
11 |
Correct |
18 ms |
91424 KB |
Output is correct |
12 |
Correct |
4 ms |
91424 KB |
Output is correct |
13 |
Correct |
3 ms |
91424 KB |
Output is correct |
14 |
Correct |
25 ms |
91424 KB |
Output is correct |
15 |
Correct |
25 ms |
91424 KB |
Output is correct |
16 |
Correct |
5 ms |
91424 KB |
Output is correct |
17 |
Correct |
237 ms |
91424 KB |
Output is correct |
18 |
Correct |
22 ms |
91424 KB |
Output is correct |
19 |
Correct |
16 ms |
91424 KB |
Output is correct |
20 |
Correct |
6 ms |
91424 KB |
Output is correct |
21 |
Correct |
5 ms |
91424 KB |
Output is correct |
22 |
Correct |
18 ms |
91424 KB |
Output is correct |
23 |
Correct |
217 ms |
91424 KB |
Output is correct |
24 |
Correct |
578 ms |
91808 KB |
Output is correct |
25 |
Correct |
416 ms |
91808 KB |
Output is correct |
26 |
Correct |
87 ms |
91808 KB |
Output is correct |
27 |
Correct |
131 ms |
91808 KB |
Output is correct |
28 |
Correct |
441 ms |
91808 KB |
Output is correct |
29 |
Correct |
382 ms |
91808 KB |
Output is correct |
30 |
Correct |
75 ms |
91808 KB |
Output is correct |
31 |
Correct |
226 ms |
91808 KB |
Output is correct |
32 |
Correct |
161 ms |
91808 KB |
Output is correct |
33 |
Execution timed out |
1028 ms |
91808 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |