Atcoder Regular Contest 076 F - Exhausted?题解

题目链接:F - Exhausted?
题目大意:有m个椅子在数轴上排列,第i张椅子(1≤i≤m)的坐标为i。 高桥君和他的朋友一共有n个人。高桥君他们因为玩了太久的游戏,大家的腰和背都很痛,所以他们很有必要坐在椅子上休息一下。高桥君他们每个人坐的椅子的坐标都很讲究,第i个人想坐在坐标在li以下(包括li)的椅子上,或者坐在坐标在ri以上(包括ri)的椅子上。当然,一个的椅子只能坐一个人。 可这样计算下去,可能会让他们不能都坐在椅子上休息。青木君关心高桥君他们的健康,尽可能多地增加椅子,让高桥君他们都能够坐在椅子上休息。 椅子可以添加到任意的实数坐标上,请求出需要添加椅子数量的最小值。
题解:这一道题一开始就会想到贪心,先考虑弱化版:只有\(l_i\),那么很简单,再加上\(r_i\)的话,就是当\(l_i\)不行时,把坐在\(l_i\)之前的\(r_i\)最小值给拿出来,放到后面,这样肯定是最有的。
代码:

#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
#define Maxn 200000
int tmp[Maxn+5];
struct Person{
    int l,r;
    friend bool operator <(Person p,Person q){
        if(p.l==q.l){
            return p.r<q.r;
        }
        return p.l<q.l;
    }
}a[Maxn+5];
priority_queue<int,vector<int>,greater<int> > q;
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i].l,&a[i].r);
    }
    sort(a+1,a+1+n);
    int h=1,len=0;
    for(int i=1;i<=n;i++){
        q.push(a[i].r);
        if(h<=a[i].l){
            h++;
        }
        else{
            tmp[++len]=q.top();
            q.pop();
        }
    }
    sort(tmp+1,tmp+1+len);
    int ans=0,t=m;
    for(int i=len;i>0;i--){
        if(t>=h&&t>=tmp[i]){
            t--;
        }
        else{
            ans++;
        }
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/withhope/p/11293698.html