#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define int long long
const int MAX_S = 20;
int N, M;
vector<int> times;
vector<vector<int>> calc_bl(vector<int> optimal_next){
int n = optimal_next.size();
vector<vector<int>> bl(MAX_S, vector<int>(n));
for(int i = 0; i<n; i++){
bl[0][i] = optimal_next[i];
}
for(int step =1; step<MAX_S; step++){
for(int i = 0; i<n; i++){
bl[step][i] = bl[step-1][bl[step-1][i]];
}
}
return bl;
}
int get_cost(vector<vector<int>>& bl, int pos, int begin){
int cost = 0;
for(int i = MAX_S-1; i>=0; i--){
if(times[bl[i][pos]]<times[begin]+M){
pos = bl[i][pos];
cost += (1LL<<i);
}
}
if(times[bl[0][pos]]>=times[begin]+M){
return cost+1;
}
return 1e9;
}
signed main(){
cin>>N>>M;
vector<pii> inters;
for(int i = 0; i<N; i++){
pii inter;
cin>>inter.first>>inter.second;
if(inter.first>inter.second){
inters.push_back({inter.first-M, inter.second});
inters.push_back({inter.first, inter.second+M});
inters.push_back({inter.first+M, inter.second+2*M});
}
else{
inters.push_back({inter.first, inter.second});
inters.push_back({inter.first+M, inter.second+M});
}
}
for(auto inter: inters){
times.push_back(inter.first);
times.push_back(inter.second);
}
sort(times.begin(), times.end());
for(pii& e: inters){
e.first = lower_bound(times.begin(), times.end(), e.first) - times.begin();
e.second = lower_bound(times.begin(), times.end(), e.second)- times.begin();
}
sort(inters.begin(), inters.end());
vector<pii> ev;
set<pii> cur_open;
for(int i = 0; i<inters.size(); i++){
auto e = inters[i];
ev.push_back({e.first, i});
}
vector<int> opt(times.size());
sort(ev.begin(), ev.end());
reverse(ev.begin(), ev.end());
for(int i = 0; i<times.size(); i++){
while(ev.size()>0 && ev.back().first==i){
auto cur_ev = ev.back();
ev.pop_back();
cur_open.insert(pii({inters[cur_ev.second].second, cur_ev.second}));
}
if(cur_open.size()>0){
opt[i] = (--cur_open.end())->first;
}
else{
opt[i] = i;
}
while(cur_open.size()>0 && cur_open.begin()->first == i){
cur_open.erase(cur_open.begin());
}
}
/*for(int i = 0; i<times.size(); i++){
cout<<times[i]<<" "<<times[opt[i]]<<endl;
}*/
auto bl = calc_bl(opt);
int res =1e9;
for(int i = 0; i<times.size(); i++){
res = min(res, get_cost(bl, i, i));
}
if(res>=1e6){
cout<<-1<<endl;
}
else{
cout<<res<<endl;
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:78:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i<inters.size(); i++){
| ~^~~~~~~~~~~~~~
Main.cpp:86: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]
86 | for(int i = 0; i<times.size(); i++){
| ~^~~~~~~~~~~~~
Main.cpp:108: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]
108 | for(int i = 0; i<times.size(); i++){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
388 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
388 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
500 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
704 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
388 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
500 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
704 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
10 ms |
4828 KB |
Output is correct |
40 |
Correct |
10 ms |
4828 KB |
Output is correct |
41 |
Correct |
10 ms |
4832 KB |
Output is correct |
42 |
Correct |
10 ms |
4556 KB |
Output is correct |
43 |
Correct |
12 ms |
5088 KB |
Output is correct |
44 |
Correct |
10 ms |
4832 KB |
Output is correct |
45 |
Correct |
11 ms |
4832 KB |
Output is correct |
46 |
Correct |
10 ms |
4832 KB |
Output is correct |
47 |
Correct |
10 ms |
4832 KB |
Output is correct |
48 |
Correct |
9 ms |
4676 KB |
Output is correct |
49 |
Correct |
9 ms |
4572 KB |
Output is correct |
50 |
Correct |
9 ms |
4576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
16 ms |
4672 KB |
Output is correct |
16 |
Correct |
10 ms |
4576 KB |
Output is correct |
17 |
Correct |
17 ms |
5088 KB |
Output is correct |
18 |
Correct |
11 ms |
4832 KB |
Output is correct |
19 |
Correct |
9 ms |
4832 KB |
Output is correct |
20 |
Correct |
341 ms |
217140 KB |
Output is correct |
21 |
Correct |
462 ms |
169240 KB |
Output is correct |
22 |
Correct |
454 ms |
170504 KB |
Output is correct |
23 |
Correct |
394 ms |
168608 KB |
Output is correct |
24 |
Correct |
427 ms |
168588 KB |
Output is correct |
25 |
Correct |
513 ms |
187580 KB |
Output is correct |
26 |
Correct |
408 ms |
169116 KB |
Output is correct |
27 |
Correct |
429 ms |
168260 KB |
Output is correct |
28 |
Correct |
413 ms |
169384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
692 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
12 ms |
4832 KB |
Output is correct |
13 |
Correct |
9 ms |
4672 KB |
Output is correct |
14 |
Correct |
9 ms |
4576 KB |
Output is correct |
15 |
Correct |
378 ms |
217144 KB |
Output is correct |
16 |
Correct |
411 ms |
170612 KB |
Output is correct |
17 |
Correct |
484 ms |
172704 KB |
Output is correct |
18 |
Correct |
501 ms |
179632 KB |
Output is correct |
19 |
Correct |
448 ms |
168352 KB |
Output is correct |
20 |
Correct |
404 ms |
166308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
388 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
500 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
704 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
10 ms |
4828 KB |
Output is correct |
40 |
Correct |
10 ms |
4828 KB |
Output is correct |
41 |
Correct |
10 ms |
4832 KB |
Output is correct |
42 |
Correct |
10 ms |
4556 KB |
Output is correct |
43 |
Correct |
12 ms |
5088 KB |
Output is correct |
44 |
Correct |
10 ms |
4832 KB |
Output is correct |
45 |
Correct |
11 ms |
4832 KB |
Output is correct |
46 |
Correct |
10 ms |
4832 KB |
Output is correct |
47 |
Correct |
10 ms |
4832 KB |
Output is correct |
48 |
Correct |
9 ms |
4676 KB |
Output is correct |
49 |
Correct |
9 ms |
4572 KB |
Output is correct |
50 |
Correct |
9 ms |
4576 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
344 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
432 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
1 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
604 KB |
Output is correct |
64 |
Correct |
1 ms |
604 KB |
Output is correct |
65 |
Correct |
16 ms |
4672 KB |
Output is correct |
66 |
Correct |
10 ms |
4576 KB |
Output is correct |
67 |
Correct |
17 ms |
5088 KB |
Output is correct |
68 |
Correct |
11 ms |
4832 KB |
Output is correct |
69 |
Correct |
9 ms |
4832 KB |
Output is correct |
70 |
Correct |
341 ms |
217140 KB |
Output is correct |
71 |
Correct |
462 ms |
169240 KB |
Output is correct |
72 |
Correct |
454 ms |
170504 KB |
Output is correct |
73 |
Correct |
394 ms |
168608 KB |
Output is correct |
74 |
Correct |
427 ms |
168588 KB |
Output is correct |
75 |
Correct |
513 ms |
187580 KB |
Output is correct |
76 |
Correct |
408 ms |
169116 KB |
Output is correct |
77 |
Correct |
429 ms |
168260 KB |
Output is correct |
78 |
Correct |
413 ms |
169384 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
1 ms |
348 KB |
Output is correct |
86 |
Correct |
1 ms |
692 KB |
Output is correct |
87 |
Correct |
1 ms |
604 KB |
Output is correct |
88 |
Correct |
1 ms |
604 KB |
Output is correct |
89 |
Correct |
1 ms |
604 KB |
Output is correct |
90 |
Correct |
12 ms |
4832 KB |
Output is correct |
91 |
Correct |
9 ms |
4672 KB |
Output is correct |
92 |
Correct |
9 ms |
4576 KB |
Output is correct |
93 |
Correct |
378 ms |
217144 KB |
Output is correct |
94 |
Correct |
411 ms |
170612 KB |
Output is correct |
95 |
Correct |
484 ms |
172704 KB |
Output is correct |
96 |
Correct |
501 ms |
179632 KB |
Output is correct |
97 |
Correct |
448 ms |
168352 KB |
Output is correct |
98 |
Correct |
404 ms |
166308 KB |
Output is correct |
99 |
Correct |
454 ms |
171216 KB |
Output is correct |
100 |
Correct |
459 ms |
169452 KB |
Output is correct |
101 |
Correct |
422 ms |
170912 KB |
Output is correct |
102 |
Correct |
434 ms |
167324 KB |
Output is correct |
103 |
Correct |
423 ms |
169628 KB |
Output is correct |
104 |
Correct |
464 ms |
170912 KB |
Output is correct |
105 |
Correct |
308 ms |
171052 KB |
Output is correct |