Link Title: F. - Exhausted?
Title effect: m chairs there are arranged on the number line, the coordinates of the i-th chair (1≤i≤m) is i. Jun Takahashi and his friends a total of n people. Jun Takahashi because they play a long game, we have the waist and back pain, so it is necessary that they sit in a chair to rest. Jun Takahashi coordinates chair of each of them sitting very particular person i want to sit on a chair in li coordinate the following (including li), or sitting on a chair in the coordinates ri above (including ri) of. Of course, a chair can only take one person. It can be calculated it, may not make them all sit in a chair to rest. Jun Takahashi Jun Aoki care about their health, as much as possible to increase the chair, so that they are able Jun Takahashi sitting in a chair to rest. The chair can be added to the coordinates of an arbitrary real number, a request to add the minimum number of chairs.
Solution: The questions start think of greed, consider a weaker version: Only \ (L_i \) , then it is simple, plus \ (r_i \) , then, is when \ (\ l_i) when not, to sit in \ (l_i \) before \ (r_i \) minimum to come out into the back, so there is certainly the most.
Code:
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
#define Maxn 200000
int tmp[Maxn+5];
struct Person{
int l,r;
friend bool operator <(Person p,Person q){
if(p.l==q.l){
return p.r<q.r;
}
return p.l<q.l;
}
}a[Maxn+5];
priority_queue<int,vector<int>,greater<int> > q;
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].l,&a[i].r);
}
sort(a+1,a+1+n);
int h=1,len=0;
for(int i=1;i<=n;i++){
q.push(a[i].r);
if(h<=a[i].l){
h++;
}
else{
tmp[++len]=q.top();
q.pop();
}
}
sort(tmp+1,tmp+1+len);
int ans=0,t=m;
for(int i=len;i>0;i--){
if(t>=h&&t>=tmp[i]){
t--;
}
else{
ans++;
}
}
printf("%d\n",ans);
return 0;
}