#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e15;
void Solve() {
int n, m;
cin >> n >> m;
vector<pair<int, int> > doge(m + 1);
vector<int> positions[n + 1];
for(int i = 1; i <= m; i++) {
cin >> doge[i].first >> doge[i].second; //position, jump;
doge[i].first++;
positions[doge[i].first].push_back(i);
}
if(doge[1].first == doge[2].first) {
cout << "0\n";
return;
}
vector<bool> vis(m + 1);
vector<int> dist(m + 1);
for(int i = 1; i <= m; i++) {
vis[i] = false;
dist[i] = INF;
}
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
for(int i = 0; i < positions[doge[1].first].size(); i++) {
dist[positions[doge[1].first][i]] = 0;
pq.push({dist[positions[doge[1].first][i]], positions[doge[1].first][i]});
}
while(!pq.empty()) {
int cur_dist = pq.top().first, cur_doge = pq.top().second;
//cout << "cur_dist = " << cur_dist << ", cur_doge = " << cur_doge << "\n";
pq.pop();
if(vis[cur_doge]) {
continue;
}
if(cur_doge == 2) {
break;
}
vis[cur_doge] = true;
int cur_skyscraper = doge[cur_doge].first, cur_jump = doge[cur_doge].second;
//cout << "cur_skyscraper = " << cur_skyscraper << ", cur_jump = " << cur_jump << "\n";
int new_dist = 1;
for(int i = cur_skyscraper + cur_jump; i <= n; i += cur_jump) {
//cout << "i = " << i << ", new_dist = " << new_dist << "\n";
for(int j = 0; j < positions[i].size(); j++) {
//cout << "positions[i][j] = " << positions[i][j] << "\n";
if(vis[positions[i][j]]) {
continue;
}
if(dist[positions[i][j]] > cur_dist + new_dist) {
//cout << "hellO\n";
dist[positions[i][j]] = cur_dist + new_dist;
pq.push({cur_dist + new_dist, positions[i][j]});
}
}
new_dist++;
}
new_dist = 1;
for(int i = cur_skyscraper - cur_jump; i > 0; i -= cur_jump) {
//cout << "i = " << i << ", new_dist = " << new_dist << "\n";
for(int j = 0; j < positions[i].size(); j++) {
//cout << "positions[i][j] = " << positions[i][j] << "\n";
if(vis[positions[i][j]]) {
continue;
}
if(dist[positions[i][j]] > cur_dist + new_dist) {
//cout << "hello\n";
dist[positions[i][j]] = cur_dist + new_dist;
pq.push({cur_dist + new_dist, positions[i][j]});
}
}
new_dist++;
}
}
while(!pq.empty()) {
pq.pop();
}
if(dist[2] == INF) {
cout << "-1\n";
return;
}
cout << dist[2] << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message
skyscraper.cpp: In function 'void Solve()':
skyscraper.cpp:30:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = 0; i < positions[doge[1].first].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:50:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int j = 0; j < positions[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:66:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int j = 0; j < positions[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
2 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
2 ms |
464 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
15 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
592 KB |
Output is correct |
34 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
584 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
336 KB |
Output is correct |
20 |
Correct |
11 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
316 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
592 KB |
Output is correct |
34 |
Correct |
2 ms |
592 KB |
Output is correct |
35 |
Correct |
21 ms |
2640 KB |
Output is correct |
36 |
Correct |
2 ms |
592 KB |
Output is correct |
37 |
Correct |
14 ms |
2380 KB |
Output is correct |
38 |
Correct |
24 ms |
3908 KB |
Output is correct |
39 |
Correct |
6 ms |
1744 KB |
Output is correct |
40 |
Correct |
11 ms |
2380 KB |
Output is correct |
41 |
Correct |
21 ms |
3908 KB |
Output is correct |
42 |
Correct |
4 ms |
1608 KB |
Output is correct |
43 |
Correct |
7 ms |
2128 KB |
Output is correct |
44 |
Correct |
249 ms |
2168 KB |
Output is correct |
45 |
Correct |
23 ms |
3784 KB |
Output is correct |
46 |
Correct |
24 ms |
3788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
584 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
11 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
2 ms |
584 KB |
Output is correct |
35 |
Correct |
23 ms |
2640 KB |
Output is correct |
36 |
Correct |
3 ms |
592 KB |
Output is correct |
37 |
Correct |
15 ms |
2380 KB |
Output is correct |
38 |
Correct |
28 ms |
3908 KB |
Output is correct |
39 |
Correct |
8 ms |
1744 KB |
Output is correct |
40 |
Correct |
10 ms |
2372 KB |
Output is correct |
41 |
Correct |
20 ms |
3908 KB |
Output is correct |
42 |
Correct |
7 ms |
1584 KB |
Output is correct |
43 |
Correct |
7 ms |
2128 KB |
Output is correct |
44 |
Correct |
221 ms |
2168 KB |
Output is correct |
45 |
Correct |
24 ms |
3784 KB |
Output is correct |
46 |
Correct |
23 ms |
3800 KB |
Output is correct |
47 |
Correct |
23 ms |
4024 KB |
Output is correct |
48 |
Correct |
6 ms |
1996 KB |
Output is correct |
49 |
Correct |
6 ms |
1996 KB |
Output is correct |
50 |
Correct |
5 ms |
1740 KB |
Output is correct |
51 |
Correct |
27 ms |
4848 KB |
Output is correct |
52 |
Correct |
27 ms |
3784 KB |
Output is correct |
53 |
Correct |
10 ms |
3024 KB |
Output is correct |
54 |
Correct |
1 ms |
972 KB |
Output is correct |
55 |
Correct |
1 ms |
972 KB |
Output is correct |
56 |
Execution timed out |
1089 ms |
3536 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |