码迷,mamicode.com
首页 > Web开发 > 详细

[JSOI2007]建筑抢修 优先队列 贪心

时间:2018-11-06 19:38:27      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:bsp   pac   priority   cstring   printf   can   ++   ota   贪心   

Code:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 200000+4
#define ll long long
struct WORK{  int cost,ed ; }work[maxn];
bool cmp(WORK a,WORK b){ return a.ed<b.ed; }
priority_queue<ll>Q;
int main(){
    //freopen("input.in","r",stdin);
    int n,ans=0;
    ll total=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%d%d",&work[i].cost,&work[i].ed);
    sort(work+1,work+1+n,cmp);
    for(int i=1;i<=n;++i){
        if(total+work[i].cost<=work[i].ed) ++ans, Q.push(work[i].cost), total+=work[i].cost;
        else if(work[i].cost<Q.top()) total-=Q.top(),total+=work[i].cost,Q.pop(),Q.push(work[i].cost);
    }
    printf("%d",ans);
    return 0;
}

 

[JSOI2007]建筑抢修 优先队列 贪心

标签:bsp   pac   priority   cstring   printf   can   ++   ota   贪心   

原文地址:https://www.cnblogs.com/guangheli/p/9916951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!