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

 
 

SPOJ Problem Set (classical)

417. The lazy programmer

Problem code: LAZYPROG


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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到&ldquo;题库&rdquo;选项,将鼠标悬停在上面,然后选择&ldquo;SPOJ&rdquo;。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击&ldquo;绑定&rdquo;按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt&quot;}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt&quot;}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值