题解 AT2645 【Exhausted?】
我的blog
题解
第一眼贪心,结果没过样例,就暴力打了个网络流。(结果我那个WA的贪心竟然比网络流高?!)
其实是我少考虑了。首先是如果是只有
但是,有了两个限制?我之前是按
我们再考虑一下,如果左边塞不下了,那么无论如何,都要有一个人坐到右边,那么我们可以吧左边有位置的一个人踢出来,让他坐在右边。那我们肯定是吧
那就好办了,我们往左边塞的时候,坐不下就把
CODE:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,ans=0,tmp[200005];
struct Student{
int l,r;
bool operator<(const Student &b)const{
return l!=b.l?l<b.l:r>b.r;
}
}s[200005];
priority_queue<int,vector<int>,greater<int> > q;
int main(){
// freopen("data.in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d%d",&s[i].l,&s[i].r);
sort(s+1,s+n+1);
int h=1,t=m;
for(int i=1;i<=n;i++){
q.push(s[i].r);
if(h<=t&&h<=s[i].l)h++;
else{
tmp[++tmp[0]]=q.top();
q.pop();
}
}
sort(tmp+1,tmp+tmp[0]+1);
for(int i=tmp[0];i>=1;i--){
if(h<=t&&tmp[i]<=t)t--;
else ans++;
}
printf("%d",ans);
}