跳到正文

题解

题解 AT2645 【Exhausted?】

ezoiLZH约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
获赞
13

版本

  1. v1当前

我的blog

题解

第一眼贪心,结果没过样例,就暴力打了个网络流。(结果我那个WA的贪心竟然比网络流高?!)

其实是我少考虑了。首先是如果是只有 LL 限制或是 RR 限制,那么很明显,先排序一遍,从两边开始往中间扫,能塞就塞,塞不了就加点。

但是,有了两个限制?我之前是按 LL 作为第一关键字,RR 作为第二关键字排序,但显然 RR 不单调,直接贪心显然WA。

我们再考虑一下,如果左边塞不下了,那么无论如何,都要有一个人坐到右边,那么我们可以吧左边有位置的一个人踢出来,让他坐在右边。那我们肯定是吧 RR 小的踢出来(容错率高)。

那就好办了,我们往左边塞的时候,坐不下就把 RR 最小的踢掉,可以用小根堆维护。把左边塞完之后,再把没座位的,以同样的方法往右边塞,实在没办法就只能补了。

CODE:

C++
#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);
}
原文