#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define upmax(a, b) a = max(a, b);
#define upmin(a, b) a = min(a, b);
#define pr(x) cout << x << endl;
#define wpr(x) cout << #x << ": " << x << endl;
#define rep(i, s, e) for(ll i = s; i < e; i++)
#define rep(i, e) for(ll i = 0; i < e; i++)
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
const ll inf = 1e18;
ll n, m;
vvll d, have; // [place][sky ind]
vll s, p;
void bfs()
{
deque<pll> q; // {place, sky ind}
q.push_front({ s[0], 0 });
d[s[0]][0] = 0;
while (!q.empty()) {
ll b = q.front().first;
ll ind = q.front().second;
//wpr(b);
//wpr(ind);
//cout << "dist: " << d[b][ind] << endl;
q.pop_front();
rep(i, have[b].size()) {
if (d[b][have[b][i]] != inf) continue;
d[b][have[b][i]] = d[b][ind];
q.push_front({ b, have[b][i] });
}
have[b].clear();
ll jump = p[ind];
if (b - jump >= 0 && d[b-jump][ind] == inf) {
d[b - jump][ind] = d[b][ind] + 1;
q.push_back({ b - jump, ind });
}
if (b + jump < n && d[b + jump][ind] == inf) {
d[b + jump][ind] = d[b][ind] + 1;
q.push_back({ b + jump, ind });
}
}
}
int main()
{
cin >> n >> m;
s.resize(m);
p.resize(m);
have.resize(n);
rep(i, m) {
cin >> s[i] >> p[i];
have[s[i]].push_back(i);
}
d.resize(n, vll(m, inf));
bfs();
ll ans = d[s[1]][1];
if (ans == inf) ans = -1;
pr(ans);
}
/*
*/
Compilation message
skyscraper.cpp:11: warning: "rep" redefined
11 | #define rep(i, e) for(ll i = 0; i < e; i++)
|
skyscraper.cpp:10: note: this is the location of the previous definition
10 | #define rep(i, s, e) for(ll i = s; i < e; i++)
|
skyscraper.cpp: In function 'void bfs()':
skyscraper.cpp:11:35: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | #define rep(i, e) for(ll i = 0; i < e; i++)
......
43 | rep(i, have[b].size()) {
| ~~~~~~~~~~~~~~~~~
skyscraper.cpp:43:3: note: in expansion of macro 'rep'
43 | rep(i, have[b].size()) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
288 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
7 ms |
1992 KB |
Output is correct |
13 |
Correct |
8 ms |
1868 KB |
Output is correct |
14 |
Correct |
3 ms |
1868 KB |
Output is correct |
15 |
Correct |
3 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
7 ms |
1868 KB |
Output is correct |
13 |
Correct |
7 ms |
1868 KB |
Output is correct |
14 |
Correct |
3 ms |
1868 KB |
Output is correct |
15 |
Correct |
3 ms |
1868 KB |
Output is correct |
16 |
Correct |
2 ms |
1612 KB |
Output is correct |
17 |
Correct |
9 ms |
10712 KB |
Output is correct |
18 |
Correct |
9 ms |
13872 KB |
Output is correct |
19 |
Correct |
5 ms |
9036 KB |
Output is correct |
20 |
Correct |
237 ms |
31840 KB |
Output is correct |
21 |
Correct |
4 ms |
3916 KB |
Output is correct |
22 |
Correct |
6 ms |
10444 KB |
Output is correct |
23 |
Correct |
7 ms |
12364 KB |
Output is correct |
24 |
Correct |
16 ms |
28572 KB |
Output is correct |
25 |
Correct |
16 ms |
31820 KB |
Output is correct |
26 |
Correct |
95 ms |
31840 KB |
Output is correct |
27 |
Correct |
85 ms |
31820 KB |
Output is correct |
28 |
Correct |
21 ms |
31820 KB |
Output is correct |
29 |
Correct |
10 ms |
12068 KB |
Output is correct |
30 |
Correct |
3 ms |
3276 KB |
Output is correct |
31 |
Correct |
7 ms |
9548 KB |
Output is correct |
32 |
Correct |
6 ms |
8012 KB |
Output is correct |
33 |
Correct |
25 ms |
31820 KB |
Output is correct |
34 |
Correct |
24 ms |
31836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
1968 KB |
Output is correct |
12 |
Correct |
6 ms |
1960 KB |
Output is correct |
13 |
Correct |
7 ms |
1868 KB |
Output is correct |
14 |
Correct |
3 ms |
1868 KB |
Output is correct |
15 |
Correct |
3 ms |
1868 KB |
Output is correct |
16 |
Correct |
2 ms |
1612 KB |
Output is correct |
17 |
Correct |
8 ms |
10700 KB |
Output is correct |
18 |
Correct |
8 ms |
13900 KB |
Output is correct |
19 |
Correct |
6 ms |
9048 KB |
Output is correct |
20 |
Correct |
245 ms |
31864 KB |
Output is correct |
21 |
Correct |
4 ms |
3916 KB |
Output is correct |
22 |
Correct |
6 ms |
10444 KB |
Output is correct |
23 |
Correct |
7 ms |
12364 KB |
Output is correct |
24 |
Correct |
16 ms |
28468 KB |
Output is correct |
25 |
Correct |
17 ms |
31828 KB |
Output is correct |
26 |
Correct |
94 ms |
31828 KB |
Output is correct |
27 |
Correct |
86 ms |
31820 KB |
Output is correct |
28 |
Correct |
19 ms |
31820 KB |
Output is correct |
29 |
Correct |
12 ms |
12108 KB |
Output is correct |
30 |
Correct |
3 ms |
3276 KB |
Output is correct |
31 |
Correct |
7 ms |
9548 KB |
Output is correct |
32 |
Correct |
6 ms |
8012 KB |
Output is correct |
33 |
Correct |
24 ms |
31716 KB |
Output is correct |
34 |
Correct |
25 ms |
31836 KB |
Output is correct |
35 |
Runtime error |
150 ms |
262148 KB |
Execution killed with signal 9 |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
4 ms |
1868 KB |
Output is correct |
12 |
Correct |
7 ms |
1868 KB |
Output is correct |
13 |
Correct |
7 ms |
1868 KB |
Output is correct |
14 |
Correct |
3 ms |
1868 KB |
Output is correct |
15 |
Correct |
3 ms |
1868 KB |
Output is correct |
16 |
Correct |
2 ms |
1612 KB |
Output is correct |
17 |
Correct |
8 ms |
10732 KB |
Output is correct |
18 |
Correct |
8 ms |
13900 KB |
Output is correct |
19 |
Correct |
5 ms |
9164 KB |
Output is correct |
20 |
Correct |
238 ms |
31844 KB |
Output is correct |
21 |
Correct |
4 ms |
3884 KB |
Output is correct |
22 |
Correct |
6 ms |
10444 KB |
Output is correct |
23 |
Correct |
7 ms |
12364 KB |
Output is correct |
24 |
Correct |
16 ms |
28508 KB |
Output is correct |
25 |
Correct |
17 ms |
31744 KB |
Output is correct |
26 |
Correct |
92 ms |
31832 KB |
Output is correct |
27 |
Correct |
87 ms |
31820 KB |
Output is correct |
28 |
Correct |
19 ms |
31820 KB |
Output is correct |
29 |
Correct |
10 ms |
12064 KB |
Output is correct |
30 |
Correct |
3 ms |
3276 KB |
Output is correct |
31 |
Correct |
8 ms |
9548 KB |
Output is correct |
32 |
Correct |
6 ms |
7976 KB |
Output is correct |
33 |
Correct |
26 ms |
31820 KB |
Output is correct |
34 |
Correct |
27 ms |
31832 KB |
Output is correct |
35 |
Runtime error |
132 ms |
262148 KB |
Execution killed with signal 9 |
36 |
Halted |
0 ms |
0 KB |
- |