码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-02 13:29:11      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

按照截止日期排序,之后一个一个遍历,记录当前时间,如果当前时间大于截止时间,那么从选过的任务里删除一个花费最大的任务

优先队列维护

14038525 201301052100 2786 Accepted 11168K 1016MS C++ 905B 2015-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;
}

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

标签:

原文地址:http://blog.csdn.net/u013451221/article/details/44830467

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