zoukankan      html  css  js  c++  java
  • bzoj 1029

    经典题型,贪心算法。我们用大根堆来维护前k个元素中选最多个数,所需时间的最小值。

    先按照结束时间排序,然后从第一个开始枚举。

    如果现在的所需时间的总和小于等于这个建筑的最晚开始加工时间,那么就维修,把所需时间放入大根堆中。

    否则就判断这个建筑的所需时间是否小于大根堆的堆顶元素,如果是,就弹出堆顶元素,并把这个建筑的所需时间加入大根堆中。

    #include<cstdio>
    #include<cctype>
    #include<queue>
    #include<algorithm>
    using namespace std;
    int read(){
        char c; while(!isdigit(c=getchar())); int x=c-'0';
        while(isdigit(c=getchar())) x=x*10+c-'0'; return x;
    }
    struct building{
        int need,time;
    }a[150000];
    priority_queue<int> q;
    bool comp(building x,building y){
        return x.time<y.time;
    }
    int main(){
        int n=read();
        for(int i=0;i<n;i+=1)
            a[i].need=read(),a[i].time=read();
        sort(a+0,a+n,comp);
        int now=0,cnt=0;
        for(int i=0;i<n;i+=1)
            if(now<=a[i].time-a[i].need) now+=a[i].need,cnt++,q.push(a[i].need);
            else if(a[i].need<q.top()) now-=q.top()-a[i].need,q.pop(),q.push(a[i].need);
        printf("%d",cnt);
        return 0;
    }
  • 相关阅读:
    array方法(一)
    浅谈开发中所注意的问题
    缩略图初版
    Finite State Machine
    mMathf -》 Unity3d通用脚本
    Follow Path -》 Unity3d通用脚本
    Google 网站打不开
    MonoBehaviour.StopCoroutine
    Tutorial Unity 4 apk splitting into OBB for google play
    NGUI List<EventDelegate> 小坑
  • 原文地址:https://www.cnblogs.com/AmnesiacVisitor/p/7591314.html
Copyright © 2011-2022 走看看