#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
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 target){
int cost = 0;
for(int i = MAX_S-1; i>=0; i--){
if(bl[i][pos]<target){
pos = bl[i][pos];
cost += (1LL<<i);
}
}
if(bl[0][pos]>=target){
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, inter.second+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);
if(inter.first<M){
times.push_back(inter.first+M);
times.push_back(inter.second+M);
}
}
sort(times.begin(), times.end());
for(pii& e: inters){
if(e.first<e.second){
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;
}
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 =1e6;
for(int i = 0; i<times.size(); i++){
res = min(res, get_cost(bl, i, lower_bound(times.begin(), times.end(), times[i]+M)-times.begin()));
}
if(res==1e6){
cout<<-1<<endl;
}
else{
cout<<res<<endl;
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:80:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int i = 0; i<inters.size(); i++){
| ~^~~~~~~~~~~~~~
Main.cpp:88:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int i = 0; i<times.size(); i++){
| ~^~~~~~~~~~~~~
Main.cpp:111:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for(int i = 0; i<times.size(); i++){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
604 KB |
Output is correct |
8 |
Incorrect |
0 ms |
436 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
604 KB |
Output is correct |
8 |
Incorrect |
0 ms |
436 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
604 KB |
Output is correct |
8 |
Incorrect |
0 ms |
436 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
384 KB |
Output is correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
604 KB |
Output is correct |
8 |
Incorrect |
0 ms |
436 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |