BZOJ1029_建筑抢修_KEY

题目传送门

这是一道贪心的问题。

总体做法是这样的:先按照报废的快慢从小到大SORT一遍,优先修报废快的。同时开一个大根堆(C++的朋友可以用priority_queue),用来记录已经修了的建筑的耗时大小。

在优先修建筑的时候,如果在当前所用的时间加上修的时间超过了报废时间了的话,就找出大根堆的TOP,与所要修的建筑的时间比较,如果TOP比它大,那么就放弃修之前那个修TOP时用的时间,改而以那个时间来修当前这个建筑。再将TOP替换为当前的修建筑的耗时。/※

还有就是要注意[放弃改修]时的总耗时操作的细节。

code

/**************************************************************
    Problem: 1029
    User: yekehe
    Language: C++
    Result: Accepted
    Time:444 ms
    Memory:2384 kb
****************************************************************/

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

struct FFF{int y,k;};
int n,m;
int i,j;
int tot,ans;
priority_queue<int>y;
FFF a[150005];

int max(int x,int y){return x>y?x:y;}
int cop(FFF x,FFF y){return x.k<y.k;}

int main()
{
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    scanf("%d %d",&a[i].y,&a[i].k);
    sort(a+1,a+n+1,cop);
    for (i=1;i<=n;i++)
    {
        if (tot+a[i].y<=a[i].k)
        {
            tot+=a[i].y;
            ans++;
            y.push(a[i].y);
        }
        else
        {
            if (a[i].y<y.top()&&y.top()!=0)
            {
                tot-=y.top()-a[i].y;
                y.pop();
                y.push(a[i].y);
            }
        }
    }
    printf("%d",ans);
}

O(NlogN)

有疑留评。

posted @ 2017-09-25 19:16  Cptraser  阅读(98)  评论(0编辑  收藏  举报