poj 2970 优先队列

题目来源Northeastern Europe 2004 poj上的链接点这儿

下面放出题目描述

The lazy programmer
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 1118
Accepted: 273

Description

A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is a web-designer and an executive director at the same time. The second one is a programmer. The director is so a nimble guy that the studio has already got N contracts for web site development. Each contract has a deadline di.

It is known that the programmer is lazy. Usually he does not work as fast as he could. Therefore, under normal conditions the programmer needs bi of time to perform the contract number i. Fortunately, the guy is very greedy for money. If the director pays him xi dollars extra, he needs only (bi − ai xi) of time to do his job. But this extra payment does not influent other contract. It means that each contract should be paid separately to be done faster. The programmer is so greedy that he can do his job almost instantly if the extra payment is (bi ⁄ ai) dollars for the contract number i.

The director has a difficult problem to solve. He needs to organize programmer’s job and, may be, assign extra payments for some of the contracts so that all contracts are performed in time. Obviously he wishes to minimize the sum of extra payments. Help the director!

Input

The first line of the input contains the number of contracts N (1 ≤ N ≤ 100 000, integer). Each of the next N lines describes one contract and contains integer numbers aibidi (1 ≤ aibi ≤ 10 000; 1 ≤ di ≤ 1 000 000 000) separated by spaces.

Output

The output needs to contain a single real number S in the only line of file. S is the minimum sum of money which the director needs to pay extra so that the programmer could perform all contracts in time. The number must have two digits after the decimal point.

Sample Input

2
20 50 100
10 100 50

Sample Output

5.00



题意简单来说,就是有n件工作,每件工作有一个deadline d,一个预计完成需要的时间 b,还有一个a (如果你给这个人x元,那么他完成这件工作的时间就变为b - a * x)。题目所求是问你最少需要多少时间,便可以保证所有工作都在其deadline之前完成。


很明显,如果手头这个工作无法完成,那么我们就需要在已经完成的工作中(这里其实包括手头的工作)找到那个a最大的工作,然后给钱来减少那个工作所需的时间,以便以最小的花销来完成现在所有的工作。所以我们需要一个数据结构来维护当前拥有最大a的那个工作。我们可以直接调用stl里的priority_queue来完成这个工作,每次取出a最大的工作,如果这项工作已经不能再给钱了,那么就将其POP出队列,选取下一个,直到我们可以再deadline前完成手头的工作。


下面放出代码

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a))
#define rep(i,initial_n,end_n) for(int (i)=(initial_n);(i)<(end_n);i++)
#define repp(i,initial_n,end_n) for(int (i)=(initial_n);(i)<=(end_n);(i)++)
#define reep(i,initial_n,end_n) for((i)=(initial_n);(i)<(end_n);i++)
#define reepp(i,initial_n,end_n) for((i)=(initial_n);(i)<=(end_n);(i)++)
#define eps 1.0e-9
#define MAX_N 1010

using namespace std;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef __int64 ll;
typedef unsigned __int64 ull;

struct node {
    int a, b, d;
    double c;
        node() { c = 0; }
    friend bool operator < (node a, node b) {
        return a.d < b.d;
    }
};

struct cmp {
    bool operator() (node a, node b) {
        return a.a < b.a;
    }
};

node a[100010];
priority_queue<node, vector<node>, cmp> q;

int main() {
    int n;
    while(~scanf("%d", &n)) {
        rep(i, 0 , n) {
            scanf("%d%d%d", &a[i].a, &a[i].b, &a[i].d);
            a[i].c = 0;
        }
        sort(a, a + n);
        double day = 0, ans = 0;
        while(!q.empty()) q.pop();
        rep(i, 0, n) {
            q.push(a[i]);
            day +=a[i].b;
            while(day - a[i].d > eps) {
                node tmp = q.top(); q.pop();
                double x = (day - a[i].d) / tmp.a;
                if((double)tmp.b / tmp.a - tmp.c - x > eps) {
                   tmp.c += x;
                   ans += x, day -= x*tmp.a;
                   q.push(tmp);
                   break;
                }
                else {
                    x = (double)tmp.b / tmp.a - tmp.c;
                    tmp.c += x;
                    ans += x;
                    day -= x * tmp.a;
                }
            }
        }
        printf("%.2f\n", ans);
    }
    return 0;
}







你可能感兴趣的:(数据结构,ACM,优先队列,poj)