BZOJ 1029 [JSOI2007]建筑抢修

贪心,根据t2从小到大排序,用堆维护。

·剩余时间足够完成当前任务,则插入当前任务
·剩余时间不足完成当前任务,若当前任务的t1比堆中t1的最大值大,则不插入,否则插入

#include<cstdio>
#include<algorithm>
#define MAXN 150010
struct pro{int t1, t2;}p[MAXN];
using namespace std;
bool cmp(pro a, pro b){return a.t2<b.t2;}
int heap[MAXN],cnt=0;
void add(int x)
{
    heap[++cnt]=x;
    for(int i = cnt, j = cnt>>1; j; i=j, j>>=1)
    {
        if(heap[j]<heap[i])
            swap(heap[i],heap[j]);
        else break;
    }
}
void del()
{
    heap[1]=heap[cnt--];
    for(int i = 1, j = 1<<1; j <= cnt; i=j, j<<=1)
    {
        if(j<cnt && heap[j]<heap[j|1])
            j|=1;
        if(heap[i]<heap[j])
            swap(heap[i],heap[j]);
        else break;
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i =1; i <=n;i ++)
        scanf("%d%d",&p[i].t1,&p[i].t2);
    sort(p+1,p+n+1,cmp);
    int last=0; 
    for(int i =1; i <=n;i ++)
    {
        last+=p[i].t2-p[i-1].t2;
        if(p[i].t1<=last)
        {
            last-=p[i].t1;
            add(p[i].t1);
        }
        else if(heap[1]>p[i].t1)
        {
            last+=heap[1]-p[i].t1;
            del();
            add(p[i].t1);
        }
    } 
    printf("%d\n",cnt);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值