【每日一题】建筑抢修

[JSOI2007]建筑抢修

http://www.nowcoder.com/questionTerminal/f72ab43d14d94755a0bd7f351dd61936

贪心












#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 150010;
struct Node{
    int cost,end;
    bool operator < (const Node & a) const{
        return cost < a.cost;
    }
}a[N];
bool cmp(Node a,Node b){
    return a.end < b.end;
}
int main(){
    int n;scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        scanf("%d%d",&a[i].cost,&a[i].end);
    }
    sort(a+1,a+1+n,cmp);
    int now = 0;
    priority_queue<int> q;
    for(int i = 1;i <= n;i++){
        if(now + a[i].cost <= a[i].end){
            now += a[i].cost;
            q.push(a[i].cost);
        }else if(q.empty() || q.top() <= a[i].cost){
            continue;
        }else{
            now -= q.top();q.pop();
            now += a[i].cost;
            q.push(a[i].cost);
        }
    }
    printf("%d\n",q.size());
    return 0;
} 
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务