【每日一题】建筑抢修

[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;
} 
全部评论

相关推荐

06-27 12:30
延安大学 C++
实习+外包,这两个公司底层融为一体了,如何评价呢?
一表renzha:之前面了一家外包的大模型,基本上都能答出来,那面试官感觉还没我懂,然后把我挂了,我都还没嫌弃他是外包,他把我挂了……
点赞 评论 收藏
分享
鼠鼠第一次实习,啥也不懂一直是自己一个人吃的饭,不会做工作老是被嫌弃,大人的世界是这样的吗?
我是星星我会发亮:好的mt有两种,一种愿意教你的,一种几乎什么活都不给你派让你很闲允许你做自己事情的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务