AtCoder Regular Contest 076 F - Exhausted? (贪心)

Topic

Insert picture description here

answer

If only llThe limit of l , then it must be directly in accordance withlll Sort from small to large, and then fill in from the chair on the left one by one. It is a greedy board question.

But this question also limits lll andrrWhat about r , it has directly evolved into a black question.

We might as well follow lll Greed, then giverrr An opportunity to repent.

A bit abstract, the specific approach is:

We still follow lll Sort (from small to large, as mentioned above), each time the smallest one is not occupied, if there is no such position, then consider "replace" the previous person, and therrof the person who needs to be replacedr is higher than the currentrrr is small (so that it can be replaced with more options), if there are more than one that meets the requirements,rrshould be replaced obviouslyThe smallest r comes out.
After the operation, there are still a few people left (unlucky to be "replaced" or "replaced" failed), and the occupied position must be a prefix at this time, so we press (the remaining people)rrr Sort from the largest to the smallest and then be greedy (start from the chair on the right). At this time, people who can’t sit down have no choice.

Seeing the "obvious" above, you know that it is definitely not what the author said. The above method is from the comment of the wzy god, and it feels very understandable (I only explained four places).

So only need to do llUse a small root heap to maintainrrwhen lr smallest people, and secondly to pay attention to detail, behind greedyrrCan't sit in front of greedyllwhen rSeated position at l .
Insert picture description here

CODE

#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 200005
#define LL long long
#define DB double
#define ENDL putchar('\n')
LL read() {
    
    
	LL f=1,x=0;char s = getchar();
	while(s < '0' || s > '9') {
    
    if(s=='-')f = -f;s = getchar();}
	while(s >= '0' && s <= '9') {
    
    x=x*10+(s-'0');s = getchar();}
	return f * x;
}
int n,m,i,j,s,o,k;
struct it{
    
    
	int l,r;  it(){
    
    l=r=0;}
	it(int L,int R){
    
    l=L;r=R;}
}a[MAXN];
bool cmp(it a,it b) {
    
    return a.l < b.l;}
bool operator < (it a,it b) {
    
    return a.r < b.r;}
bool operator > (it a,it b) {
    
    return b < a;}
priority_queue<it,vector<it>,greater<it> > b1;
priority_queue<it> b2;
int main() {
    
    
	n = read();m = read();
	for(int i = 1;i <= n;i ++) {
    
    
		a[i].l = read();a[i].r = read();
	}
	sort(a + 1,a + 1 + n,cmp);
	int le = 0;
	b1.push(it(0,m+1));
	for(int i = 1;i <= n;i ++) {
    
    
		if(le < a[i].l) {
    
    
			le ++;
			b1.push(a[i]);
		}
		else {
    
    
			if(a[i].r > b1.top().r) {
    
    
				b2.push(b1.top());
				b1.pop(); b1.push(a[i]);
			}
			else b2.push(a[i]);
		}
	}
	int ans = 0,le2 = m+1;
	while(!b2.empty()) {
    
    
		it t = b2.top(); b2.pop();
		if(le2 > max(t.r,le+1)) le2 --; // Detail!! : Don't disturb the prefix !!
		else ans ++;
	}
	printf("%d\n",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43960414/article/details/113836036