#include<bits/stdc++.h>
#define forin(i, a, b) for(int i = a; i <= b; ++i)
#define ii pair<int, int>
#define iii pair<int, ii>
using namespace std;
const int N = 3e4 + 10, T = 180;
int n, m, inf;
int b[N], p[N], d[T + 10][N];
vector<int> walk[N];
vector<ii> tele[N];
priority_queue<iii, vector<iii>, greater<iii> > pq;
int main () {
cin.tie(0)->sync_with_stdio(0);
if(fopen("Task.inp","r")) {
freopen("Task.inp","r",stdin);
freopen("WA.out","w",stdout);
}
cin>>n>>m;
forin(i, 1, m) {
cin>>b[i]>>p[i];
if(p[i] > T) {
int pos = b[i], d = 0;
do {
if(d) tele[b[i]].push_back({pos, d});
pos += p[i];
d++;
} while(pos < n);
pos = b[i]; d = 0;
do {
if(d) tele[b[i]].push_back({pos, d});
pos -= p[i];
d++;
} while(pos >= 0);
}
else walk[b[i]].push_back(p[i]);
}
memset(d, 60, sizeof(d));
inf = d[0][0];
d[0][b[1]] = 0;
pq.push({0, {b[1], 0}});
while(!pq.empty()) {
int kc = pq.top().first;
int u, l; tie(u, l) = pq.top().second;
pq.pop();
if(d[l][u] != kc) continue;
if(l) walk[u].push_back(l);
for(auto e : tele[u]) {
int v, w; tie(v, w) = e;
if(d[0][v] <= kc + w) continue;
d[0][v] = kc + w;
pq.push({d[0][v], {v, 0}});
}
for(auto w : walk[u]) {
if(u - w >= 0 && d[w][u - w] > kc + 1) {
d[w][u - w] = kc + 1;
pq.push({d[w][u - w], {u - w, w}});
}
if(u + w < n && d[w][u + w] > kc + 1) {
d[w][u + w] = kc + 1;
pq.push({d[w][u + w], {u + w, w}});
}
}
if(l) walk[u].pop_back();
}
int ans = inf;
forin(i, 0, T) ans = min(ans, d[i][b[2]]);
if(ans == inf) cout<<-1;
else cout<<ans;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:15:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen("Task.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:16:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("WA.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
24020 KB |
Output is correct |
2 |
Correct |
9 ms |
24020 KB |
Output is correct |
3 |
Correct |
9 ms |
24020 KB |
Output is correct |
4 |
Correct |
10 ms |
24020 KB |
Output is correct |
5 |
Correct |
9 ms |
23964 KB |
Output is correct |
6 |
Correct |
9 ms |
24020 KB |
Output is correct |
7 |
Correct |
9 ms |
24020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
24020 KB |
Output is correct |
2 |
Correct |
11 ms |
24020 KB |
Output is correct |
3 |
Correct |
10 ms |
24020 KB |
Output is correct |
4 |
Correct |
9 ms |
23984 KB |
Output is correct |
5 |
Correct |
10 ms |
24060 KB |
Output is correct |
6 |
Correct |
9 ms |
24020 KB |
Output is correct |
7 |
Correct |
10 ms |
24020 KB |
Output is correct |
8 |
Correct |
10 ms |
24020 KB |
Output is correct |
9 |
Correct |
11 ms |
24112 KB |
Output is correct |
10 |
Correct |
10 ms |
24020 KB |
Output is correct |
11 |
Correct |
10 ms |
24020 KB |
Output is correct |
12 |
Correct |
10 ms |
24028 KB |
Output is correct |
13 |
Correct |
10 ms |
24032 KB |
Output is correct |
14 |
Correct |
10 ms |
24148 KB |
Output is correct |
15 |
Correct |
10 ms |
24148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
24016 KB |
Output is correct |
2 |
Correct |
9 ms |
24032 KB |
Output is correct |
3 |
Correct |
10 ms |
23960 KB |
Output is correct |
4 |
Correct |
9 ms |
24020 KB |
Output is correct |
5 |
Correct |
9 ms |
24020 KB |
Output is correct |
6 |
Correct |
9 ms |
23944 KB |
Output is correct |
7 |
Correct |
10 ms |
23984 KB |
Output is correct |
8 |
Correct |
9 ms |
24020 KB |
Output is correct |
9 |
Correct |
9 ms |
24044 KB |
Output is correct |
10 |
Correct |
11 ms |
24016 KB |
Output is correct |
11 |
Correct |
13 ms |
24020 KB |
Output is correct |
12 |
Correct |
10 ms |
24052 KB |
Output is correct |
13 |
Correct |
10 ms |
24020 KB |
Output is correct |
14 |
Correct |
10 ms |
24148 KB |
Output is correct |
15 |
Correct |
11 ms |
24104 KB |
Output is correct |
16 |
Correct |
10 ms |
24020 KB |
Output is correct |
17 |
Correct |
11 ms |
24148 KB |
Output is correct |
18 |
Correct |
11 ms |
23972 KB |
Output is correct |
19 |
Correct |
10 ms |
24020 KB |
Output is correct |
20 |
Correct |
10 ms |
24020 KB |
Output is correct |
21 |
Correct |
9 ms |
23976 KB |
Output is correct |
22 |
Correct |
12 ms |
24024 KB |
Output is correct |
23 |
Correct |
10 ms |
24044 KB |
Output is correct |
24 |
Correct |
11 ms |
24148 KB |
Output is correct |
25 |
Correct |
11 ms |
24152 KB |
Output is correct |
26 |
Correct |
10 ms |
24060 KB |
Output is correct |
27 |
Correct |
9 ms |
24148 KB |
Output is correct |
28 |
Correct |
11 ms |
24104 KB |
Output is correct |
29 |
Correct |
15 ms |
24148 KB |
Output is correct |
30 |
Correct |
11 ms |
24060 KB |
Output is correct |
31 |
Correct |
12 ms |
24120 KB |
Output is correct |
32 |
Correct |
11 ms |
24084 KB |
Output is correct |
33 |
Correct |
21 ms |
24204 KB |
Output is correct |
34 |
Correct |
20 ms |
24148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
24020 KB |
Output is correct |
2 |
Correct |
11 ms |
24020 KB |
Output is correct |
3 |
Correct |
9 ms |
24020 KB |
Output is correct |
4 |
Correct |
9 ms |
24020 KB |
Output is correct |
5 |
Correct |
9 ms |
24040 KB |
Output is correct |
6 |
Correct |
9 ms |
24044 KB |
Output is correct |
7 |
Correct |
9 ms |
23968 KB |
Output is correct |
8 |
Correct |
9 ms |
24052 KB |
Output is correct |
9 |
Correct |
9 ms |
24036 KB |
Output is correct |
10 |
Correct |
11 ms |
24076 KB |
Output is correct |
11 |
Correct |
10 ms |
24148 KB |
Output is correct |
12 |
Correct |
9 ms |
24020 KB |
Output is correct |
13 |
Correct |
10 ms |
24028 KB |
Output is correct |
14 |
Correct |
10 ms |
24080 KB |
Output is correct |
15 |
Correct |
11 ms |
24052 KB |
Output is correct |
16 |
Correct |
11 ms |
24020 KB |
Output is correct |
17 |
Correct |
11 ms |
24140 KB |
Output is correct |
18 |
Correct |
11 ms |
24020 KB |
Output is correct |
19 |
Correct |
9 ms |
24020 KB |
Output is correct |
20 |
Correct |
9 ms |
24020 KB |
Output is correct |
21 |
Correct |
9 ms |
24020 KB |
Output is correct |
22 |
Correct |
10 ms |
23964 KB |
Output is correct |
23 |
Correct |
10 ms |
24020 KB |
Output is correct |
24 |
Correct |
11 ms |
24192 KB |
Output is correct |
25 |
Correct |
10 ms |
24112 KB |
Output is correct |
26 |
Correct |
10 ms |
24148 KB |
Output is correct |
27 |
Correct |
10 ms |
24136 KB |
Output is correct |
28 |
Correct |
10 ms |
24148 KB |
Output is correct |
29 |
Correct |
16 ms |
24100 KB |
Output is correct |
30 |
Correct |
11 ms |
24020 KB |
Output is correct |
31 |
Correct |
12 ms |
24024 KB |
Output is correct |
32 |
Correct |
15 ms |
24020 KB |
Output is correct |
33 |
Correct |
21 ms |
24072 KB |
Output is correct |
34 |
Correct |
21 ms |
24148 KB |
Output is correct |
35 |
Correct |
22 ms |
24728 KB |
Output is correct |
36 |
Correct |
11 ms |
24156 KB |
Output is correct |
37 |
Correct |
33 ms |
25172 KB |
Output is correct |
38 |
Correct |
29 ms |
25044 KB |
Output is correct |
39 |
Correct |
29 ms |
25116 KB |
Output is correct |
40 |
Correct |
29 ms |
25024 KB |
Output is correct |
41 |
Correct |
30 ms |
25044 KB |
Output is correct |
42 |
Correct |
13 ms |
24532 KB |
Output is correct |
43 |
Correct |
13 ms |
24532 KB |
Output is correct |
44 |
Correct |
12 ms |
24404 KB |
Output is correct |
45 |
Correct |
88 ms |
27040 KB |
Output is correct |
46 |
Correct |
76 ms |
27120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
24020 KB |
Output is correct |
2 |
Correct |
9 ms |
24020 KB |
Output is correct |
3 |
Correct |
10 ms |
23912 KB |
Output is correct |
4 |
Correct |
9 ms |
23992 KB |
Output is correct |
5 |
Correct |
11 ms |
24004 KB |
Output is correct |
6 |
Correct |
10 ms |
24020 KB |
Output is correct |
7 |
Correct |
10 ms |
24044 KB |
Output is correct |
8 |
Correct |
10 ms |
24020 KB |
Output is correct |
9 |
Correct |
9 ms |
24020 KB |
Output is correct |
10 |
Correct |
10 ms |
24024 KB |
Output is correct |
11 |
Correct |
10 ms |
24020 KB |
Output is correct |
12 |
Correct |
10 ms |
24008 KB |
Output is correct |
13 |
Correct |
9 ms |
24004 KB |
Output is correct |
14 |
Correct |
10 ms |
24148 KB |
Output is correct |
15 |
Correct |
10 ms |
24148 KB |
Output is correct |
16 |
Correct |
9 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
24104 KB |
Output is correct |
18 |
Correct |
9 ms |
24020 KB |
Output is correct |
19 |
Correct |
9 ms |
24020 KB |
Output is correct |
20 |
Correct |
11 ms |
24020 KB |
Output is correct |
21 |
Correct |
9 ms |
24020 KB |
Output is correct |
22 |
Correct |
9 ms |
24036 KB |
Output is correct |
23 |
Correct |
10 ms |
24116 KB |
Output is correct |
24 |
Correct |
10 ms |
24148 KB |
Output is correct |
25 |
Correct |
10 ms |
24164 KB |
Output is correct |
26 |
Correct |
11 ms |
24156 KB |
Output is correct |
27 |
Correct |
10 ms |
24160 KB |
Output is correct |
28 |
Correct |
10 ms |
24148 KB |
Output is correct |
29 |
Correct |
15 ms |
24120 KB |
Output is correct |
30 |
Correct |
11 ms |
24020 KB |
Output is correct |
31 |
Correct |
12 ms |
24148 KB |
Output is correct |
32 |
Correct |
11 ms |
24096 KB |
Output is correct |
33 |
Correct |
22 ms |
24092 KB |
Output is correct |
34 |
Correct |
21 ms |
24188 KB |
Output is correct |
35 |
Correct |
21 ms |
24672 KB |
Output is correct |
36 |
Correct |
12 ms |
24076 KB |
Output is correct |
37 |
Correct |
31 ms |
25144 KB |
Output is correct |
38 |
Correct |
29 ms |
25044 KB |
Output is correct |
39 |
Correct |
29 ms |
25052 KB |
Output is correct |
40 |
Correct |
29 ms |
25060 KB |
Output is correct |
41 |
Correct |
30 ms |
25044 KB |
Output is correct |
42 |
Correct |
12 ms |
24488 KB |
Output is correct |
43 |
Correct |
13 ms |
24488 KB |
Output is correct |
44 |
Correct |
12 ms |
24404 KB |
Output is correct |
45 |
Correct |
75 ms |
27136 KB |
Output is correct |
46 |
Correct |
75 ms |
27160 KB |
Output is correct |
47 |
Correct |
93 ms |
28360 KB |
Output is correct |
48 |
Correct |
14 ms |
25280 KB |
Output is correct |
49 |
Correct |
14 ms |
25132 KB |
Output is correct |
50 |
Correct |
13 ms |
24916 KB |
Output is correct |
51 |
Correct |
47 ms |
27064 KB |
Output is correct |
52 |
Correct |
52 ms |
27072 KB |
Output is correct |
53 |
Correct |
20 ms |
25300 KB |
Output is correct |
54 |
Correct |
13 ms |
24488 KB |
Output is correct |
55 |
Correct |
11 ms |
24568 KB |
Output is correct |
56 |
Correct |
16 ms |
25172 KB |
Output is correct |
57 |
Correct |
57 ms |
24996 KB |
Output is correct |
58 |
Correct |
16 ms |
25240 KB |
Output is correct |
59 |
Correct |
17 ms |
25324 KB |
Output is correct |
60 |
Correct |
23 ms |
25592 KB |
Output is correct |
61 |
Correct |
20 ms |
25440 KB |
Output is correct |
62 |
Correct |
30 ms |
27476 KB |
Output is correct |
63 |
Correct |
64 ms |
49284 KB |
Output is correct |
64 |
Correct |
69 ms |
57348 KB |
Output is correct |
65 |
Correct |
328 ms |
55752 KB |
Output is correct |
66 |
Correct |
951 ms |
50572 KB |
Output is correct |
67 |
Correct |
963 ms |
50944 KB |
Output is correct |