[JSOI2007]建筑抢修

Description

修理建筑需要T1秒,如果在T2秒之内还没有修理完成,这个建筑就报废了。给出若干条信息,求最多能修理多少建筑.

Sample Input

4
100 200
200 1300
1000 1250
2000 3200

Sample Output

3

贪心
先按结束时间排序,按次序处理。
如果当前的时间加上需要的时间没有超过结束时间,直接进堆,更新答案;(使最终结果变大)
而如果超过了,把堆顶取出预制比较,若堆顶所花时间较多,取出堆顶,将当前建筑进堆。(在最终结果不变的情况下,减少总时间)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
struct node
{
    int t,ed;
}a[210000];
int cmp(const void *xx,const void *yy)
{
    node n1=*(node *)xx;
    node n2=*(node *)yy;
    return n1.ed-n2.ed;
}
multiset<int> s;
multiset<int>::iterator it;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d%d",&a[i].t,&a[i].ed);
    qsort(a+1,n,sizeof(node),cmp);
    int head=1,tail=0,k=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i].t+k<=a[i].ed)
        {
            k+=a[i].t;
            ans++;
            s.insert(a[i].t);
        }
        else
        {
            it=s.end();it--;
            int q=*it;
            if(a[i].t<q)
            {
                int ss=s.count(q);
                s.erase(q);
                for(int j=1;j<ss;j++)s.insert(q);
                s.insert(a[i].t);
                k+=a[i].t-q;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值