spoj 417 The lazy programmer(贪心&优先队列)

  
  
  
  

SPOJ Problem Set (classical)

417. The lazy programmer

Problem code: LAZYPROG


spoj 417 The lazy programmer(贪心&优先队列)_第1张图片

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 influence other contracts. This 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

First line of the input contains an integer t (1 ≤ t ≤ 45), equal to the number of testcases. Then descriptions of t testcases follow.

First line of description contains the number of contracts N (1 ≤ N ≤ 100000, integer). Each of the next Nlines describes one contract and contains integer numbers aibidi (1 ≤ aibi ≤ 100001 ≤ di ≤1000000000) separated by spaces.

At least 90% of testcases will have 1 ≤ N ≤ 10000.

Output

For each testcase in the input your program should output one line with a single real number S. Here 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.

Example

Input:
1
2
20 50 100
10 100 50

Output:
5.00
题意:
一共有n个任务,每个任务有一个截止时间di,有一个耗费时间bi ,如果花费x元钱,可以将耗费时间减少为bi-ai*x。现在要我们找出一个方案,用最少的钱,在截止日期前完成所有任务。
思路:
先对截止时间排序。然后先按正常工作时间做。如果超过截止时间就在优先队列里找出a最大的工作然后付钱减时。这样一定最优。
详细见代码:
#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
typedef __int64 ll;
class yb
{
public:
    int a,b,d;
    double c;//剩余可减少时间
    yb(){ c=0;}
    bool operator<(const yb& t)const//优先队列的优先级
    {
        return a<t.a;
    }
} con[maxn],tp;
int cmpd(double a,double b)//比较两个浮点数
{
    if(fabs(a-b)<eps)
        return 0;
    else if(a>b)
        return 1;
    else
        return -1;
}
bool cmp(yb a,yb b)//截止时间排序
{
    return a.d<b.d;
}
int main()
{
    int n,i;
    double T,ans,x;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)
            scanf("%d%d%d",&con[i].a,&con[i].b,&con[i].d);
        sort(con,con+n,cmp);
        priority_queue<yb> q;
        T=ans=0;
        for(i=0;i<n;i++)
        {
            T+=con[i].b;
            q.push(con[i]);
            while(cmpd(T,con[i].d)>0)
            {
                tp=q.top();
                q.pop();
                x=(T-con[i].d)/tp.a;
                if(cmpd((double)tp.b/tp.a,x+tp.c)>0)
                {
                    tp.c+=x;
                    ans+=x;
                    T=con[i].d;
                    q.push(tp);
                    break;
                }
                else
                {
                    x=(double)tp.b/tp.a-tp.c;
                    ans+=x;
                    T-=x*tp.a;
                }
            }
        }
        printf("%.2f\n",ans);
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)