1. 程式人生 > >【POJ】2786-Keep the Customer Satisfied(貪心 + 優先佇列,姿勢不對就要跪)

【POJ】2786-Keep the Customer Satisfied(貪心 + 優先佇列,姿勢不對就要跪)

按照截止日期排序,之後一個一個遍歷,記錄當前時間,如果當前時間大於截止時間,那麼從選過的任務裡刪除一個花費最大的任務

優先佇列維護

14038525 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;
}