#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define vl vector<ll>
#define fi firp
#define se second
#define in insert
#define mpr make_pair
#define lg(x) __lg(x)
#define bpc(x) __builtin_popcount(x)
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int sz = 3e4+5;
const ll inf = 100000000000000000;
const ll LG = 26;
bool used[sz];
vl pos[sz];
void solve(){
ll n, m, i, j, c;
cin >> n >> m;
map<pair<ll, ll>, ll> mp;
vl b(m + 1), p(m + 1);
for(i = 1; i <= m; i++){
cin >> b[i] >> p[i];
b[i]++;
if(mp[mpr(b[i], p[i])]){continue;}
mp[mpr(b[i], p[i])]++;
pos[b[i]].pb(p[i]);
}
priority_queue<array<ll, 2>, vector<array<ll, 2>>, greater<array<ll, 2>>> pq;
pq.push({0, b[1]});
vl dis(n + 1, inf);
dis[b[1]] = 0;
while(!pq.empty()){
ll v = pq.top()[1];
ll dtov = pq.top()[0];
pq.pop();
if(v == b[2]){
cout << dtov << endl;
return ;
}
if(used[v]){continue;}
used[v] = 1;
for(auto P: pos[v]){
c = 1;
for(i = v + P; i <= n; i += P, c++){
if(dis[i] > dis[v] + c){
dis[i] = dis[v] + c;
pq.push({dis[i], i});
}
}
c = 1;
for(i = v - P; i > 0; i -= P, c++){
if(dis[i] > dis[v] + c){
dis[i] = dis[v] + c;
pq.push({dis[i], i});
}
}
}
}
cout << -1 << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t = 1;
///cin >> t;
while(t--){
solve();
}
}
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:21:17: warning: unused variable 'j' [-Wunused-variable]
21 | ll n, m, i, j, c;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1112 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1176 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1112 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1200 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
5 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1316 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1656 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1628 KB |
Output is correct |
33 |
Correct |
5 ms |
1624 KB |
Output is correct |
34 |
Correct |
3 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1168 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
5 ms |
1372 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1544 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1416 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1628 KB |
Output is correct |
34 |
Correct |
3 ms |
1628 KB |
Output is correct |
35 |
Correct |
13 ms |
3720 KB |
Output is correct |
36 |
Correct |
3 ms |
1628 KB |
Output is correct |
37 |
Correct |
11 ms |
3164 KB |
Output is correct |
38 |
Correct |
20 ms |
4448 KB |
Output is correct |
39 |
Correct |
16 ms |
4188 KB |
Output is correct |
40 |
Correct |
15 ms |
4188 KB |
Output is correct |
41 |
Correct |
16 ms |
4476 KB |
Output is correct |
42 |
Correct |
4 ms |
1884 KB |
Output is correct |
43 |
Correct |
4 ms |
1884 KB |
Output is correct |
44 |
Correct |
7 ms |
1884 KB |
Output is correct |
45 |
Correct |
16 ms |
4444 KB |
Output is correct |
46 |
Correct |
16 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1120 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1168 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1120 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1376 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1300 KB |
Output is correct |
19 |
Correct |
1 ms |
1172 KB |
Output is correct |
20 |
Correct |
5 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1172 KB |
Output is correct |
22 |
Correct |
2 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1176 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1176 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1628 KB |
Output is correct |
30 |
Correct |
4 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1768 KB |
Output is correct |
34 |
Correct |
3 ms |
1628 KB |
Output is correct |
35 |
Correct |
13 ms |
3900 KB |
Output is correct |
36 |
Correct |
3 ms |
1624 KB |
Output is correct |
37 |
Correct |
11 ms |
3164 KB |
Output is correct |
38 |
Correct |
20 ms |
4420 KB |
Output is correct |
39 |
Correct |
15 ms |
4184 KB |
Output is correct |
40 |
Correct |
15 ms |
4184 KB |
Output is correct |
41 |
Correct |
15 ms |
4444 KB |
Output is correct |
42 |
Correct |
4 ms |
1884 KB |
Output is correct |
43 |
Correct |
4 ms |
1880 KB |
Output is correct |
44 |
Correct |
7 ms |
1880 KB |
Output is correct |
45 |
Correct |
18 ms |
4444 KB |
Output is correct |
46 |
Correct |
19 ms |
4744 KB |
Output is correct |
47 |
Correct |
16 ms |
4816 KB |
Output is correct |
48 |
Correct |
12 ms |
3840 KB |
Output is correct |
49 |
Correct |
11 ms |
3736 KB |
Output is correct |
50 |
Correct |
7 ms |
2908 KB |
Output is correct |
51 |
Correct |
23 ms |
6908 KB |
Output is correct |
52 |
Correct |
22 ms |
5844 KB |
Output is correct |
53 |
Correct |
17 ms |
5088 KB |
Output is correct |
54 |
Correct |
2 ms |
1760 KB |
Output is correct |
55 |
Correct |
2 ms |
1760 KB |
Output is correct |
56 |
Correct |
671 ms |
5580 KB |
Output is correct |
57 |
Correct |
4 ms |
2780 KB |
Output is correct |
58 |
Correct |
6 ms |
2272 KB |
Output is correct |
59 |
Correct |
7 ms |
2396 KB |
Output is correct |
60 |
Correct |
6 ms |
2432 KB |
Output is correct |
61 |
Correct |
7 ms |
2760 KB |
Output is correct |
62 |
Correct |
26 ms |
4840 KB |
Output is correct |
63 |
Correct |
48 ms |
6584 KB |
Output is correct |
64 |
Correct |
40 ms |
9168 KB |
Output is correct |
65 |
Correct |
53 ms |
9588 KB |
Output is correct |
66 |
Correct |
59 ms |
9928 KB |
Output is correct |
67 |
Correct |
48 ms |
8912 KB |
Output is correct |