#include <bits/stdc++.h>
using namespace std;
#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long int lli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;
void usaco()
{
freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
// freopen("problem.out", "w", stdout);
}
int main() {
// usaco();
int n, m;
scd(n);
scd(m);
int sq = 250;
vector<vii> graph(n);
vector<vb> con(n, vb(sq));
vector<vi> consq(n+1);
int st;
int en;
frange(i, m) {
int b, p;
scd(b);
scd(p);
if(i == 0) st = b;
if(i == 1) en = b;
if(p >= sq) {
for(int i=1; b+i*p<n; i++) {
graph[b].pb(mp(b+i*p, i));
// printf("%d %d %d\n", b, b+i*p, i);
}
for(int i=1; b-i*p>=0; i++) {
graph[b].pb(mp(b-i*p, i));
// printf("%d %d %d\n", b, b-i*p, i);
}
}
else {
con[b][p] = true;
consq[b].pb(p);
}
}
// forr(p, 1, sq) {
// vb vis(n);
// queue<int> q;
// frange(i, n) {
// if(con[i][p]) q.push(i);
// }
// while(q.size()) {
// auto x = q.front();
// q.pop();
// if(vis[x]) continue;
// vis[x] = true;
// if(x + p < n) {
// graph[x].pb(mp(x+p, 1));
// q.push(x+p);
// printf("%d %d %d\n", x, x+p, 1);
// }
// if(x - p >= 0) {
// graph[x].pb(mp(x-p, 1));
// q.push(x-p);
// printf("%d %d %d\n", x, x-p, 1);
// }
// }
// }
vector<vb> vis(n, vb(sq));
vvi dist(n, vi(sq, 2e9));
dist[st][0] = 0;
forr(i, 1, sq) {
if(con[st][i]) dist[st][i] = 0;
}
priority_queue<pair<int, pii>> pq;
pq.push(mp(0, mp(st, 0)));
while(pq.size()) {
auto p = pq.top();
pq.pop();
if(vis[p.s.f][p.s.s]) continue;
vis[p.s.f][p.s.s] = true;
dist[p.s.f][0] = min(dist[p.s.f][0], -p.f);
for(auto e : graph[p.s.f]) {
if(-p.f + e.s < dist[e.f][0]) {
dist[e.f][0] = -p.f + e.s;
// printf("%d %d\n", e.f, 0);
pq.push(mp(-dist[e.f][0], mp(e.f, 0)));
}
}
if(p.s.s) {
if(p.s.f + p.s.s < n && -p.f + 1 < dist[p.s.f + p.s.s][p.s.s]) {
dist[p.s.f + p.s.s][p.s.s] = -p.f + 1;
pq.push(mp(-dist[p.s.f + p.s.s][p.s.s], mp(p.s.f + p.s.s, p.s.s)));
// printf("%d %d\n", p.s.f + p.s.s, p.s.s);
}
if(p.s.f - p.s.s >= 0 && -p.f + 1 < dist[p.s.f - p.s.s][p.s.s]) {
dist[p.s.f - p.s.s][p.s.s] = -p.f + 1;
pq.push(mp(-dist[p.s.f - p.s.s][p.s.s], mp(p.s.f - p.s.s, p.s.s)));
// printf("%d %d\n", p.s.f - p.s.s, p.s.s);
}
}
for(auto i : consq[p.s.f]) {
dist[p.s.f][i] = min(dist[p.s.f][i], dist[p.s.f][0]);
if(p.s.f + i < n && -p.f + 1 < dist[p.s.f + i][i]) {
dist[p.s.f + i][i] = -p.f + 1;
pq.push(mp(-dist[p.s.f + i][i], mp(p.s.f + i, i)));
// printf("%d %d\n", p.s.f+i, i);
}
if(p.s.f - i >= 0 && -p.f + 1 < dist[p.s.f - i][i]) {
dist[p.s.f - i][i] = -p.f + 1;
pq.push(mp(-dist[p.s.f - i][i], mp(p.s.f - i, i)));
// printf("%d %d\n", p.s.f-i, i);
}
}
}
int mi = 2e9;
frange(i, sq) mi = min(mi, dist[en][i]);
if(mi < 2e9) printf("%d", mi);
else printf("-1");
}
Compilation message
skyscraper.cpp: In function 'void usaco()':
skyscraper.cpp:30:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
skyscraper.cpp:37:5: note: in expansion of macro 'scd'
37 | scd(n);
| ^~~
skyscraper.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
skyscraper.cpp:38:5: note: in expansion of macro 'scd'
38 | scd(m);
| ^~~
skyscraper.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
skyscraper.cpp:51:9: note: in expansion of macro 'scd'
51 | scd(b);
| ^~~
skyscraper.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
skyscraper.cpp:52:9: note: in expansion of macro 'scd'
52 | scd(p);
| ^~~
skyscraper.cpp:102:28: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
102 | pq.push(mp(0, mp(st, 0)));
| ^
skyscraper.cpp:146:39: warning: 'en' may be used uninitialized in this function [-Wmaybe-uninitialized]
146 | frange(i, sq) mi = min(mi, dist[en][i]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2396 KB |
Output is correct |
24 |
Correct |
3 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
3 ms |
2908 KB |
Output is correct |
29 |
Correct |
8 ms |
3164 KB |
Output is correct |
30 |
Correct |
3 ms |
2652 KB |
Output is correct |
31 |
Correct |
4 ms |
2908 KB |
Output is correct |
32 |
Correct |
4 ms |
2652 KB |
Output is correct |
33 |
Correct |
13 ms |
2952 KB |
Output is correct |
34 |
Correct |
13 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
3 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2904 KB |
Output is correct |
20 |
Correct |
3 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2396 KB |
Output is correct |
24 |
Correct |
3 ms |
2828 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
3 ms |
2908 KB |
Output is correct |
29 |
Correct |
7 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2652 KB |
Output is correct |
31 |
Correct |
4 ms |
2908 KB |
Output is correct |
32 |
Correct |
5 ms |
2904 KB |
Output is correct |
33 |
Correct |
13 ms |
2904 KB |
Output is correct |
34 |
Correct |
13 ms |
2948 KB |
Output is correct |
35 |
Correct |
17 ms |
2396 KB |
Output is correct |
36 |
Correct |
3 ms |
1628 KB |
Output is correct |
37 |
Correct |
28 ms |
3416 KB |
Output is correct |
38 |
Correct |
24 ms |
3416 KB |
Output is correct |
39 |
Correct |
25 ms |
3420 KB |
Output is correct |
40 |
Correct |
24 ms |
3420 KB |
Output is correct |
41 |
Correct |
24 ms |
3420 KB |
Output is correct |
42 |
Correct |
5 ms |
2980 KB |
Output is correct |
43 |
Correct |
6 ms |
2908 KB |
Output is correct |
44 |
Correct |
5 ms |
2908 KB |
Output is correct |
45 |
Correct |
68 ms |
3416 KB |
Output is correct |
46 |
Correct |
68 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2396 KB |
Output is correct |
24 |
Correct |
3 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2708 KB |
Output is correct |
26 |
Correct |
2 ms |
2904 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
3 ms |
2936 KB |
Output is correct |
29 |
Correct |
7 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2652 KB |
Output is correct |
31 |
Correct |
4 ms |
2912 KB |
Output is correct |
32 |
Correct |
3 ms |
2908 KB |
Output is correct |
33 |
Correct |
13 ms |
2908 KB |
Output is correct |
34 |
Correct |
14 ms |
2948 KB |
Output is correct |
35 |
Correct |
16 ms |
2396 KB |
Output is correct |
36 |
Correct |
3 ms |
1640 KB |
Output is correct |
37 |
Correct |
27 ms |
3420 KB |
Output is correct |
38 |
Correct |
24 ms |
3420 KB |
Output is correct |
39 |
Correct |
25 ms |
3420 KB |
Output is correct |
40 |
Correct |
24 ms |
3420 KB |
Output is correct |
41 |
Correct |
24 ms |
3420 KB |
Output is correct |
42 |
Correct |
6 ms |
2908 KB |
Output is correct |
43 |
Correct |
5 ms |
2880 KB |
Output is correct |
44 |
Correct |
5 ms |
2904 KB |
Output is correct |
45 |
Correct |
67 ms |
3412 KB |
Output is correct |
46 |
Correct |
67 ms |
3416 KB |
Output is correct |
47 |
Correct |
157 ms |
19228 KB |
Output is correct |
48 |
Correct |
22 ms |
30292 KB |
Output is correct |
49 |
Correct |
24 ms |
32348 KB |
Output is correct |
50 |
Correct |
23 ms |
35408 KB |
Output is correct |
51 |
Correct |
73 ms |
39192 KB |
Output is correct |
52 |
Correct |
90 ms |
39172 KB |
Output is correct |
53 |
Correct |
29 ms |
37212 KB |
Output is correct |
54 |
Correct |
21 ms |
37056 KB |
Output is correct |
55 |
Correct |
21 ms |
37212 KB |
Output is correct |
56 |
Correct |
28 ms |
38112 KB |
Output is correct |
57 |
Correct |
80 ms |
37212 KB |
Output is correct |
58 |
Correct |
27 ms |
37212 KB |
Output is correct |
59 |
Correct |
30 ms |
37220 KB |
Output is correct |
60 |
Correct |
34 ms |
37468 KB |
Output is correct |
61 |
Correct |
31 ms |
37464 KB |
Output is correct |
62 |
Correct |
69 ms |
39256 KB |
Output is correct |
63 |
Correct |
186 ms |
57688 KB |
Output is correct |
64 |
Correct |
258 ms |
58976 KB |
Output is correct |
65 |
Correct |
765 ms |
54220 KB |
Output is correct |
66 |
Correct |
912 ms |
37908 KB |
Output is correct |
67 |
Correct |
908 ms |
38172 KB |
Output is correct |