#include <bits/stdc++.h>
using namespace std;
struct shift{
int s, e;
const bool operator<(const shift &t) const{if(s==t.s){return e<t.e;}return s<t.s;}
};
int main(){
int n, m, li, lt=0;
cin >> n >> m;
vector<shift> v(n);
for(int i=0; i<n; i++){
cin >> v[i].s >> v[i].e;
}
sort(v.begin(),v.end());
for(int i=0; i<n; i++){
if((v[i].e-v[i].s+m)%m>lt){
lt=(v[i].e-v[i].s+m)%m;
li=i;
}
}
priority_queue<int> pq;
if(v[li].e<v[li].s){
v[li].e+=m;
}
int pf=v[li].e, p=(li+1)%n, ans=1;
while(pf<v[li].s+m){
while(v[p].s<=pf&&p<li){
if((v[p].s<v[li].s&&v[p].s+m<=pf)||v[p].s>v[li].s){
if(v[p].e<v[li].s||v[p].s<v[li].s){
pq.push(v[p].e+m);
}
else{
pq.push(v[p].e);
}
//cout << p << " " << v[p].e << " " << pf << endl;
p++;
p%=n;
}
else{
break;
}
}
if(pq.empty()||pq.top()<=pf){
cout << -1 << endl;
return 0;
}
else{
ans++;
pf=pq.top();
pq.pop();
}
}
cout << ans << endl;
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:29:19: warning: 'li' may be used uninitialized in this function [-Wmaybe-uninitialized]
29 | while(v[p].s<=pf&&p<li){
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
516 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
516 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
516 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
516 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |