bzoj1029: [JSOI2007]建筑抢修

Posted AKCqhzdy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj1029: [JSOI2007]建筑抢修相关的知识,希望对你有一定的参考价值。

今天是真的萎,切不动题,瞎写陈年老题找自信。。。

强行一波贪心猛如虎

先按毁坏时间排序,枚举,能修的就修,修不了就把前面耗时最长的拿出来和当前比较,假如现在需要时间更短就换

总之就是维护修了ans个的最快时间

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;

struct node
{
    int t,ed;
}a[210000];
bool cmp(node n1,node n2){return n1.ed<n2.ed;}
multiset<int> s;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d%d",&a[i].t,&a[i].ed);
    sort(a+1,a+n+1,cmp);
    
    int now=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i].t+now<=a[i].ed)
        {
            now+=a[i].t;
            ans++;
            s.insert(a[i].t);
        }
        else
        {
            int q=*--s.end();
            if(a[i].t<q)
            {
                int sum=s.count(q);
                s.erase(q);
                for(int j=1;j<sum;j++)s.insert(q);
                s.insert(a[i].t);
                now+=a[i].t-q;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

 

以上是关于bzoj1029: [JSOI2007]建筑抢修的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ-1029: [JSOI2007]建筑抢修(贪心+堆优化)

BZOJ1029: [JSOI2007]建筑抢修(贪心)

BZOJ1029: [JSOI2007]建筑抢修[模拟 贪心 优先队列]

BZOJ 1029: [JSOI2007]建筑抢修 堆+贪心

bzoj 1029 [JSOI2007]建筑抢修 - 贪心 + 大根堆

BZOJ 1029: [JSOI2007]建筑抢修优先队列+贪心策略