CSES-1619 Restaurant Customers

题目传送门icon-default.png?t=O83Ahttps://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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值