题目传送门https://vjudge.net/problem/CSES-1619
解题思路
考虑一个优先队列,每次将先离开的顾客排在前面,对于当前顾客,若队头顾客已经离开,那么直接出队。取队列长度最大值即为答案。
代码
#include<bits/stdc++.h>
using namespace std;
int n;
struct man{
int l,r;
// friend bool operator < (man x,man y)
// {
// return x.r>y.r;
// }
}a[200001];
bool cmp(man x,man y)
{
// if(x.l==y.l)return x.r<y.r;
return x.l<y.l;
}
struct node{
int id,d;
friend bool operator < (node x,node y)
{
return x.d>y.d;
}
};
priority_queue<node> q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].l>>a[i].r;
}
sort(a+1,a+n+1,cmp);
int ans=0,cnt=0;
for(int i=1;i<=n;i++)
{
while(!q.empty()&&a[q.top().id].r<a[i].l)
{
//cout<<a[q.top().id].r<<endl;
q.pop();
cnt--;
}
node tail;
tail.id=i;
tail.d=a[i].r;
q.push(tail);
cnt++;
ans=max(ans,cnt);
}
cout<<ans;
return 0;
}