SPOJ 417 The lazy programmer(贪心)

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个任务,每一个任务都有deadline,以及完成所需要的时间b,还有一个参数a,如果花x,那么他所需要的时间就变成b-x*a。任务需要一个接一个完成。问保证每个任务在deadline之前完成所需要花费的钱数
 
思路:首先确定的是deadline小的先处理。当发现curT+b[i] > deadline[i]的时候,只要从前面取出a[j]最大的,然后把curT往前挪。a[j]就用优先队列维护。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long LL;
#define REP(_,a,b) for(int _ = (a); _ <= (b); _++)
const int maxn = 100000+10;
const double eps = 1e-8;
int dcmp(double x) {
    if(fabs(x) < eps) return 0;
    else if(x > 0) return 1;
    else return -1;
}
struct Programer{
    double last,deadline,a,cost;
    Programer(double last=0,double deadline=0,double a=0):last(last),deadline(deadline),a(a){}
    friend bool operator < (Programer a,Programer b) {
        return a.a < b.a;
    }
}P[maxn];
bool cmp(Programer a,Programer b) {
     if(a.deadline != b.deadline) return a.deadline < b.deadline;
     else return a.last < b.last;
}
int n;
priority_queue<Programer> pqP;
void init() {
    while(!pqP.empty()) pqP.pop();
}
void input() {
    REP(i,1,n) {
        scanf("%lf%lf%lf",&P[i].a,&P[i].last,&P[i].deadline);
        P[i].cost = 0.0;
    }
    sort(P+1,P+1+n,cmp);
}
void solve() {
    double curT = 0,ret = 0;
    REP(i,1,n) {
        pqP.push(P[i]);
        if(dcmp(curT+P[i].last-P[i].deadline)>0) {
             while(dcmp(curT+P[i].last-P[i].deadline)>0) {
                Programer tmp = pqP.top();
                pqP.pop();
                double extra = (tmp.last/tmp.a-tmp.cost)*tmp.a;
                double need = curT+P[i].last-P[i].deadline;
                if(dcmp(need-extra) >= 0) {
                    ret += tmp.last/tmp.a-tmp.cost;
                    curT -= extra;
                }else {
                    curT -= need;
                    tmp.cost += need/tmp.a;
                    ret += need/tmp.a;
                    pqP.push(tmp);
                }
            }
            curT = P[i].deadline;
        }else {
            curT += P[i].last;
        }
    }
    printf("%.2f\n",ret);
}
int main(){
    int ncase;
    cin >> ncase;
    while(ncase--) {
        scanf("%d",&n);
        init();
        input();
        solve();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值