[BZOJ1572][Usaco2009 Open]工作安排Job

1572: [Usaco2009 Open]工作安排Job

Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 1351  Solved: 632 [Submit][Status][Discuss]

Description

Farmer John 有太多的工作要做啊!!!!!!!!为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间。 他的工作日从0时刻开始,有1000000000个单位时间(!)。在任一时刻,他都可以选择编号1~N的N(1 <= N <= 100000)项工作中的任意一项工作来完成。 因为他在每个单位时间里只能做一个工作,而每项工作又有一个截止日期,所以他很难有时间完成所有N个工作,虽然还是有可能。 对于第i个工作,有一个截止时间D_i(1 <= D_i <= 1000000000),如果他可以完成这个工作,那么他可以获利P_i( 1<=P_i<=1000000000 ). 在给定的工作利润和截止时间下,FJ能够获得的利润最大为多少呢?答案可能会超过32位整型。

Input

第1行:一个整数N. 第2~N+1行:第i+1行有两个用空格分开的整数:D_i和P_i.

Output

输出一行,里面有一个整数,表示最大获利值。

Sample Input

3
2 10
1 5
1 7

Sample Output

17

HINT

第1个单位时间完成第3个工作(1,7),然后在第2个单位时间完成第1个工作(2,10)以达到最大利润

 
堆 + 贪心
和 [JSOI2007]建筑抢修 一样
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char buf[10000000], *ptr = buf - 1;
inline int readint(){
    int n = 0;
    char ch = *++ptr;
    while(ch < '0' || ch > '9') ch = *++ptr;
    while(ch <= '9' && ch >= '0'){
        n = (n << 1) + (n << 3) + ch - '0';
        ch = *++ptr;
    }
    return n;
}
const int maxn = 100000 + 10;
typedef long long ll;
struct Node{
    int d, p;
    Node(){}
    Node(int _d, int _p): d(_d), p(_p){}
    bool operator < (const Node &x) const{
        return d < x.d;
    }
}a[maxn], t;
class cmp{
    public:
        bool operator () (const Node &a, const Node &b){
            return a.p > b.p;
        }
};
priority_queue<Node, vector<Node>, cmp> q;
int main(){
    fread(buf, sizeof(char), sizeof(buf), stdin);
    int n = readint();
    for(int i = 1; i <= n; i++){
        a[i].d = readint();
        a[i].p = readint();
    }
    sort(a + 1, a + n + 1);
    int tot = 0;
    long long ans = 0;
    for(int i = 1; i <= n; i++){
        if(tot == a[i].d){
            t = q.top();
            if(a[i].p > t.p){
                q.pop();
                q.push(a[i]);
                ans += a[i].p - t.p;
            }
        }
        else{
            tot++;
            ans += a[i].p;
            q.push(a[i]);
        }
    }
    printf("%lld
", ans);
    return 0;
}
原文地址:https://www.cnblogs.com/ruoruoruo/p/7486650.html