【Bzoj1029】建筑抢修

题意

给出n个需要抢修的建筑,每个建筑抢修需要t1时间,并且要在t2之前完成抢修,问在时间S内能抢修的建筑最多有多少个。


解析

贪心,首先按t2排序,能修则修。然后不能的话每当有建筑时判断,如果这个建筑需要用时比之前的短,就替换。


#include <queue>
#include <cstdio>
#include <algorithm>

#define Rep( i , _begin , _end ) for(int i=(_begin);i<=(_end);i++)
#define For( i , _begin , _end ) for(int i=(_begin);i!=(_end);i++)

#define x first
#define y second

using std :: max;
using std :: min;
using std :: sort;
using std :: pair;
using std :: priority_queue;

typedef pair<int,int> aii;

const int maxx = 200000 + 25;

priority_queue < aii > quq;

aii a[maxx],b[maxx];
int n,ans,tmp;

int main(){
    scanf("%d",&n);
    Rep( i , 1 , n )
        scanf("%d%d",&a[i].y,&a[i].x);
    sort(a+1,a+n+1);
    Rep( i , 1 , n )
        b[i].y = a[i].x,b[i].x = a[i].y;

    Rep( i , 1 , n ){
        if(tmp + b[i].x <= b[i].y){
            quq.push(b[i]);
            ans ++;
            tmp += b[i].x;
        }
        else{
            if(quq.empty()) continue;
            if(b[i].x < quq.top().x){
                tmp -= (quq.top().x - b[i].x);
                quq.pop();
                quq.push(b[i]);
            }
        }
    }

    printf("%d",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值