This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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){
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |