#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
const int INF=1e18;
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
int n,m,b,p,s,t,d[50001];
vector <int> ke[50001];
int32_t main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n >> m;
for (int i=0;i<m;i++){
cin >> b >> p;
ke[b].push_back(p);
if (!i)
s=b;
if (i==1)
t=b;
}
for (int i=0;i<n;i++){
d[i]=INF;
sort(ke[i].begin(),ke[i].end());
ke[i].resize(unique(ke[i].begin(),ke[i].end())-ke[i].begin());
}
q.push({0,s});
d[s]=0;
while (!q.empty()){
int u=q.top().second,du=q.top().first;
q.pop();
if (d[u]!=du)
continue;
for (int p:ke[u]){
for (int v=u+p;v<n;v+=p){
if (d[v]>d[u]+(v-u)/p){
d[v]=d[u]+(v-u)/p;
q.push({d[v],v});
}
int pos=lower_bound(ke[v].begin(),ke[v].end(),p)-ke[v].begin();
if (pos<ke[v].size()&&ke[v][pos]==p)
break;
}
for (int v=u-p;v>=0;v-=p){
if (d[v]>d[u]+(u-v)/p){
d[v]=d[u]+(u-v)/p;
q.push({d[v],v});
}
int pos=lower_bound(ke[v].begin(),ke[v].end(),p)-ke[v].begin();
if (pos<ke[v].size()&&ke[v][pos]==p)
break;
}
}
}
cout << (d[t]==INF?-1:d[t]);
}
Compilation message
skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:41:24: 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]
41 | if (pos<ke[v].size()&&ke[v][pos]==p)
| ~~~^~~~~~~~~~~~~
skyscraper.cpp:50:24: 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 | if (pos<ke[v].size()&&ke[v][pos]==p)
| ~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1504 KB |
Output is correct |
3 |
Correct |
2 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1500 KB |
Output is correct |
6 |
Correct |
1 ms |
1500 KB |
Output is correct |
7 |
Correct |
1 ms |
1504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1504 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1500 KB |
Output is correct |
7 |
Correct |
1 ms |
1504 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
2 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1508 KB |
Output is correct |
13 |
Correct |
2 ms |
1492 KB |
Output is correct |
14 |
Correct |
2 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1396 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1500 KB |
Output is correct |
9 |
Correct |
1 ms |
1496 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
2 ms |
1512 KB |
Output is correct |
12 |
Correct |
1 ms |
1508 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
1504 KB |
Output is correct |
17 |
Correct |
2 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1492 KB |
Output is correct |
19 |
Correct |
1 ms |
1500 KB |
Output is correct |
20 |
Correct |
1 ms |
1492 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
1492 KB |
Output is correct |
23 |
Correct |
2 ms |
1492 KB |
Output is correct |
24 |
Correct |
2 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
1644 KB |
Output is correct |
26 |
Correct |
2 ms |
1512 KB |
Output is correct |
27 |
Correct |
1 ms |
1508 KB |
Output is correct |
28 |
Correct |
1 ms |
1492 KB |
Output is correct |
29 |
Correct |
3 ms |
1876 KB |
Output is correct |
30 |
Correct |
2 ms |
1624 KB |
Output is correct |
31 |
Correct |
2 ms |
1748 KB |
Output is correct |
32 |
Correct |
2 ms |
1620 KB |
Output is correct |
33 |
Correct |
5 ms |
2136 KB |
Output is correct |
34 |
Correct |
5 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1496 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1496 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1616 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
2 ms |
1492 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
2 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
2 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1492 KB |
Output is correct |
19 |
Correct |
1 ms |
1492 KB |
Output is correct |
20 |
Correct |
1 ms |
1492 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
1496 KB |
Output is correct |
23 |
Correct |
1 ms |
1492 KB |
Output is correct |
24 |
Correct |
2 ms |
1640 KB |
Output is correct |
25 |
Correct |
2 ms |
1620 KB |
Output is correct |
26 |
Correct |
1 ms |
1508 KB |
Output is correct |
27 |
Correct |
1 ms |
1492 KB |
Output is correct |
28 |
Correct |
2 ms |
1492 KB |
Output is correct |
29 |
Correct |
3 ms |
1884 KB |
Output is correct |
30 |
Correct |
2 ms |
1620 KB |
Output is correct |
31 |
Correct |
2 ms |
1748 KB |
Output is correct |
32 |
Correct |
2 ms |
1620 KB |
Output is correct |
33 |
Correct |
5 ms |
2136 KB |
Output is correct |
34 |
Correct |
5 ms |
2132 KB |
Output is correct |
35 |
Correct |
8 ms |
2212 KB |
Output is correct |
36 |
Correct |
3 ms |
1620 KB |
Output is correct |
37 |
Correct |
9 ms |
2152 KB |
Output is correct |
38 |
Correct |
10 ms |
2388 KB |
Output is correct |
39 |
Correct |
10 ms |
2412 KB |
Output is correct |
40 |
Correct |
11 ms |
2412 KB |
Output is correct |
41 |
Correct |
11 ms |
2388 KB |
Output is correct |
42 |
Correct |
5 ms |
2028 KB |
Output is correct |
43 |
Correct |
5 ms |
2092 KB |
Output is correct |
44 |
Correct |
5 ms |
2064 KB |
Output is correct |
45 |
Correct |
10 ms |
2776 KB |
Output is correct |
46 |
Correct |
11 ms |
2776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1500 KB |
Output is correct |
2 |
Correct |
1 ms |
1504 KB |
Output is correct |
3 |
Correct |
1 ms |
1608 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1496 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1500 KB |
Output is correct |
8 |
Correct |
1 ms |
1504 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1508 KB |
Output is correct |
12 |
Correct |
1 ms |
1512 KB |
Output is correct |
13 |
Correct |
2 ms |
1492 KB |
Output is correct |
14 |
Correct |
2 ms |
1508 KB |
Output is correct |
15 |
Correct |
2 ms |
1492 KB |
Output is correct |
16 |
Correct |
2 ms |
1496 KB |
Output is correct |
17 |
Correct |
3 ms |
1496 KB |
Output is correct |
18 |
Correct |
1 ms |
1520 KB |
Output is correct |
19 |
Correct |
1 ms |
1496 KB |
Output is correct |
20 |
Correct |
1 ms |
1516 KB |
Output is correct |
21 |
Correct |
1 ms |
1504 KB |
Output is correct |
22 |
Correct |
1 ms |
1492 KB |
Output is correct |
23 |
Correct |
2 ms |
1492 KB |
Output is correct |
24 |
Correct |
2 ms |
1624 KB |
Output is correct |
25 |
Correct |
2 ms |
1628 KB |
Output is correct |
26 |
Correct |
2 ms |
1496 KB |
Output is correct |
27 |
Correct |
1 ms |
1500 KB |
Output is correct |
28 |
Correct |
2 ms |
1524 KB |
Output is correct |
29 |
Correct |
4 ms |
1884 KB |
Output is correct |
30 |
Correct |
2 ms |
1628 KB |
Output is correct |
31 |
Correct |
2 ms |
1752 KB |
Output is correct |
32 |
Correct |
2 ms |
1624 KB |
Output is correct |
33 |
Correct |
4 ms |
2152 KB |
Output is correct |
34 |
Correct |
5 ms |
2132 KB |
Output is correct |
35 |
Correct |
9 ms |
2128 KB |
Output is correct |
36 |
Correct |
3 ms |
1640 KB |
Output is correct |
37 |
Correct |
9 ms |
2136 KB |
Output is correct |
38 |
Correct |
11 ms |
2352 KB |
Output is correct |
39 |
Correct |
11 ms |
2404 KB |
Output is correct |
40 |
Correct |
11 ms |
2376 KB |
Output is correct |
41 |
Correct |
10 ms |
2408 KB |
Output is correct |
42 |
Correct |
5 ms |
2020 KB |
Output is correct |
43 |
Correct |
5 ms |
2012 KB |
Output is correct |
44 |
Correct |
5 ms |
2120 KB |
Output is correct |
45 |
Correct |
11 ms |
2696 KB |
Output is correct |
46 |
Correct |
11 ms |
2692 KB |
Output is correct |
47 |
Correct |
24 ms |
3440 KB |
Output is correct |
48 |
Correct |
7 ms |
2388 KB |
Output is correct |
49 |
Correct |
8 ms |
2408 KB |
Output is correct |
50 |
Correct |
5 ms |
2152 KB |
Output is correct |
51 |
Correct |
29 ms |
3744 KB |
Output is correct |
52 |
Correct |
28 ms |
3772 KB |
Output is correct |
53 |
Correct |
17 ms |
3856 KB |
Output is correct |
54 |
Correct |
3 ms |
2132 KB |
Output is correct |
55 |
Correct |
3 ms |
2132 KB |
Output is correct |
56 |
Correct |
9 ms |
2772 KB |
Output is correct |
57 |
Correct |
24 ms |
2952 KB |
Output is correct |
58 |
Correct |
7 ms |
2392 KB |
Output is correct |
59 |
Correct |
8 ms |
2532 KB |
Output is correct |
60 |
Correct |
8 ms |
2520 KB |
Output is correct |
61 |
Correct |
8 ms |
2536 KB |
Output is correct |
62 |
Correct |
14 ms |
2900 KB |
Output is correct |
63 |
Correct |
41 ms |
6564 KB |
Output is correct |
64 |
Correct |
46 ms |
6540 KB |
Output is correct |
65 |
Correct |
60 ms |
6632 KB |
Output is correct |
66 |
Correct |
123 ms |
10692 KB |
Output is correct |
67 |
Correct |
122 ms |
10684 KB |
Output is correct |