您的位置:首页 > 其它

【POJ】2786-Keep the Customer Satisfied(贪心 + 优先队列,姿势不对就要跪)

2015-04-02 12:30 211 查看
按照截止日期排序,之后一个一个遍历,记录当前时间,如果当前时间大于截止时间,那么从选过的任务里删除一个花费最大的任务

优先队列维护

140385252013010521002786Accepted11168K1016MSC++905B2015-04-02 12:22:16
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
struct Node{
    int a,b;
    Node(int a,int b):a(a),b(b){};
    friend bool operator < (Node p,Node q){
         return p.a < q.a;
    }
};
bool cmp(Node p,Node q){
    return p.b < q.b;
}
int n;
int main(){
    while(scanf("%d",&n) != EOF){
        vector<Node>v;
        priority_queue<Node>q;
        for(int i = 0; i < n; i++){
            int a,b;
            scanf("%d%d",&a,&b);
            v.push_back(Node(a,b));
        }
        int now = 0;
        sort(v.begin(),v.end(),cmp);
        for(int i = 0; i < v.size(); i++){
            q.push(v[i]);
            now += v[i].a;
            if(now > v[i].b){
                now -= q.top().a;
                q.pop();
            }
        }
        printf("%d\n",q.size());
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: