// In the name of God
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 3e4 + 5;
int n, m;
int dis[N];
unordered_set<int> S[N];
bool vis[N];
void solve() {
cin >> n >> m;
int s = -1, t = -1;
memset(dis, 63, sizeof dis);
for (int i = 0; i < m; i++) {
int b, p; cin >> b >> p;
if (s != -1 && t == -1)
t = b;
if (s == -1)
s = b;
S[b].insert(p);
}
priority_queue<pair<int, int> > pq;
pq.push(mp(0, s));
dis[s] = 0;
while (!pq.empty()) {
int i = pq.top().se;
pq.pop();
if (vis[i])
continue;
vis[i] = true;
int prv = -1;
for (auto p : S[i]) {
for (int j = i + p; j < n; j += p) {
//cout << "check " << j << ' ' << dis[j] << '-' << dis[i] + (j - i) / p << endl;
if (dis[j] > dis[i] + (j - i) / p) {
dis[j] = dis[i] + (j - i) / p;
pq.push(mp(-dis[j], j));
}
if (S[j].count(p))
break;
}
for (int j = i - p; j >= 0; j -= p) {
//cout << "check " << j << ' ' << dis[j] << '-' << dis[i] + (i - j) / p << endl;
if (dis[j] > dis[i] + (i - j) / p) {
dis[j] = dis[i] + (i - j) / p;
pq.push(mp(-dis[j], j));
}
if (S[j].count(p))
break;
}
}
}
cout << (dis[t] < 1e9 ? dis[t] : -1) << '\n';
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:36:13: warning: unused variable 'prv' [-Wunused-variable]
36 | int prv = -1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
1 ms |
2004 KB |
Output is correct |
4 |
Correct |
1 ms |
2004 KB |
Output is correct |
5 |
Correct |
1 ms |
2004 KB |
Output is correct |
6 |
Correct |
1 ms |
2004 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1976 KB |
Output is correct |
2 |
Correct |
1 ms |
2004 KB |
Output is correct |
3 |
Correct |
1 ms |
1972 KB |
Output is correct |
4 |
Correct |
1 ms |
2004 KB |
Output is correct |
5 |
Correct |
1 ms |
1980 KB |
Output is correct |
6 |
Correct |
1 ms |
1976 KB |
Output is correct |
7 |
Correct |
1 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
1 ms |
2004 KB |
Output is correct |
10 |
Correct |
1 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2176 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
2 ms |
2004 KB |
Output is correct |
14 |
Correct |
2 ms |
2132 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
2 ms |
2004 KB |
Output is correct |
4 |
Correct |
1 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
1960 KB |
Output is correct |
6 |
Correct |
1 ms |
1968 KB |
Output is correct |
7 |
Correct |
1 ms |
2004 KB |
Output is correct |
8 |
Correct |
1 ms |
1976 KB |
Output is correct |
9 |
Correct |
2 ms |
1972 KB |
Output is correct |
10 |
Correct |
2 ms |
2104 KB |
Output is correct |
11 |
Correct |
2 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
2 ms |
2108 KB |
Output is correct |
14 |
Correct |
2 ms |
2132 KB |
Output is correct |
15 |
Correct |
2 ms |
2108 KB |
Output is correct |
16 |
Correct |
1 ms |
2108 KB |
Output is correct |
17 |
Correct |
2 ms |
2260 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
2 ms |
2368 KB |
Output is correct |
21 |
Correct |
2 ms |
2104 KB |
Output is correct |
22 |
Correct |
2 ms |
2104 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
2 ms |
2260 KB |
Output is correct |
25 |
Correct |
3 ms |
2260 KB |
Output is correct |
26 |
Correct |
2 ms |
2004 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
2 ms |
2372 KB |
Output is correct |
29 |
Correct |
3 ms |
2132 KB |
Output is correct |
30 |
Correct |
2 ms |
2100 KB |
Output is correct |
31 |
Correct |
2 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2132 KB |
Output is correct |
33 |
Correct |
4 ms |
2232 KB |
Output is correct |
34 |
Correct |
4 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
1 ms |
2004 KB |
Output is correct |
3 |
Correct |
1 ms |
2004 KB |
Output is correct |
4 |
Correct |
2 ms |
2004 KB |
Output is correct |
5 |
Correct |
1 ms |
2004 KB |
Output is correct |
6 |
Correct |
1 ms |
1972 KB |
Output is correct |
7 |
Correct |
1 ms |
2004 KB |
Output is correct |
8 |
Correct |
1 ms |
2004 KB |
Output is correct |
9 |
Correct |
1 ms |
2004 KB |
Output is correct |
10 |
Correct |
2 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
2 ms |
2104 KB |
Output is correct |
14 |
Correct |
2 ms |
2132 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
1 ms |
2132 KB |
Output is correct |
17 |
Correct |
2 ms |
2260 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
2 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
1 ms |
2132 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
4 ms |
2260 KB |
Output is correct |
25 |
Correct |
4 ms |
2260 KB |
Output is correct |
26 |
Correct |
2 ms |
2132 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
4 ms |
2368 KB |
Output is correct |
29 |
Correct |
3 ms |
2132 KB |
Output is correct |
30 |
Correct |
2 ms |
2132 KB |
Output is correct |
31 |
Correct |
2 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2132 KB |
Output is correct |
33 |
Correct |
3 ms |
2260 KB |
Output is correct |
34 |
Correct |
4 ms |
2260 KB |
Output is correct |
35 |
Correct |
11 ms |
3392 KB |
Output is correct |
36 |
Correct |
3 ms |
2356 KB |
Output is correct |
37 |
Correct |
12 ms |
3156 KB |
Output is correct |
38 |
Correct |
15 ms |
3716 KB |
Output is correct |
39 |
Correct |
14 ms |
3796 KB |
Output is correct |
40 |
Correct |
15 ms |
3796 KB |
Output is correct |
41 |
Correct |
15 ms |
3720 KB |
Output is correct |
42 |
Correct |
5 ms |
2260 KB |
Output is correct |
43 |
Correct |
5 ms |
2240 KB |
Output is correct |
44 |
Correct |
5 ms |
2516 KB |
Output is correct |
45 |
Correct |
14 ms |
3668 KB |
Output is correct |
46 |
Correct |
14 ms |
3696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
1 ms |
2004 KB |
Output is correct |
3 |
Correct |
1 ms |
2004 KB |
Output is correct |
4 |
Correct |
1 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
2088 KB |
Output is correct |
6 |
Correct |
2 ms |
2004 KB |
Output is correct |
7 |
Correct |
1 ms |
1964 KB |
Output is correct |
8 |
Correct |
2 ms |
1976 KB |
Output is correct |
9 |
Correct |
1 ms |
2004 KB |
Output is correct |
10 |
Correct |
1 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
1984 KB |
Output is correct |
13 |
Correct |
2 ms |
2004 KB |
Output is correct |
14 |
Correct |
2 ms |
2132 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
2 ms |
2260 KB |
Output is correct |
18 |
Correct |
2 ms |
2152 KB |
Output is correct |
19 |
Correct |
2 ms |
2104 KB |
Output is correct |
20 |
Correct |
2 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2108 KB |
Output is correct |
22 |
Correct |
2 ms |
2104 KB |
Output is correct |
23 |
Correct |
3 ms |
2108 KB |
Output is correct |
24 |
Correct |
3 ms |
2260 KB |
Output is correct |
25 |
Correct |
3 ms |
2336 KB |
Output is correct |
26 |
Correct |
2 ms |
2112 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
2 ms |
2368 KB |
Output is correct |
29 |
Correct |
3 ms |
2228 KB |
Output is correct |
30 |
Correct |
2 ms |
2132 KB |
Output is correct |
31 |
Correct |
3 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2100 KB |
Output is correct |
33 |
Correct |
4 ms |
2260 KB |
Output is correct |
34 |
Correct |
4 ms |
2260 KB |
Output is correct |
35 |
Correct |
12 ms |
3452 KB |
Output is correct |
36 |
Correct |
3 ms |
2368 KB |
Output is correct |
37 |
Correct |
12 ms |
3144 KB |
Output is correct |
38 |
Correct |
16 ms |
3768 KB |
Output is correct |
39 |
Correct |
16 ms |
3760 KB |
Output is correct |
40 |
Correct |
17 ms |
3776 KB |
Output is correct |
41 |
Correct |
15 ms |
3668 KB |
Output is correct |
42 |
Correct |
5 ms |
2260 KB |
Output is correct |
43 |
Correct |
5 ms |
2316 KB |
Output is correct |
44 |
Correct |
5 ms |
2516 KB |
Output is correct |
45 |
Correct |
14 ms |
3668 KB |
Output is correct |
46 |
Correct |
14 ms |
3668 KB |
Output is correct |
47 |
Correct |
27 ms |
4824 KB |
Output is correct |
48 |
Correct |
11 ms |
4564 KB |
Output is correct |
49 |
Correct |
9 ms |
4564 KB |
Output is correct |
50 |
Correct |
8 ms |
3924 KB |
Output is correct |
51 |
Correct |
32 ms |
6076 KB |
Output is correct |
52 |
Correct |
37 ms |
6088 KB |
Output is correct |
53 |
Correct |
22 ms |
6016 KB |
Output is correct |
54 |
Correct |
2 ms |
2260 KB |
Output is correct |
55 |
Correct |
3 ms |
2356 KB |
Output is correct |
56 |
Correct |
12 ms |
6484 KB |
Output is correct |
57 |
Correct |
27 ms |
3024 KB |
Output is correct |
58 |
Correct |
7 ms |
2388 KB |
Output is correct |
59 |
Correct |
8 ms |
2516 KB |
Output is correct |
60 |
Correct |
10 ms |
2516 KB |
Output is correct |
61 |
Correct |
9 ms |
2772 KB |
Output is correct |
62 |
Correct |
21 ms |
6548 KB |
Output is correct |
63 |
Correct |
47 ms |
4816 KB |
Output is correct |
64 |
Correct |
51 ms |
4748 KB |
Output is correct |
65 |
Correct |
68 ms |
4796 KB |
Output is correct |
66 |
Correct |
107 ms |
4724 KB |
Output is correct |
67 |
Correct |
108 ms |
4684 KB |
Output is correct |