poj2970The lazy programmer 优先队列+贪心

12 篇文章 0 订阅
The lazy programmer
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 2421 Accepted: 605

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

题意:给出每个任务的deadline和所需时间,以及每个时间段单位money可以买的时间。求需要花的最少money。

思路:依照时间以此解决每个任务,时间不够时从前面的任务中缩减(购买)时间。

用变量sum维护完成当前任务所需的时间。
如果sum超过deadline,从之前的任务中取出一个买时间,直到sum<=deadline.
买到的时间=a*x,a越大越划算。因此用优先队列维护可以买时间的任务。

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
int n;
struct node{
	int a,b,d;
	bool operator < (const node&x)const{//优先队列排序
		return a < x.a;
	}
}con[100001];

bool cmp(const node &a,const node &b)//数组排序
{
	return a.d<b.d;
}
priority_queue<node> qu;

int main()
{
	scanf("%d",&n);
	for (int i = 0; i < n; ++i) 
		scanf("%d%d%d",&con[i].a,&con[i].b,&con[i].d);
	sort(con,con+n,cmp);
	int sum = 0;	//维护完成当前任务所需时间
	double ans = 0.0;//完成所有任务所需要的最小费用
	for (int i = 0; i < n; ++i){
		sum +=  con[i].b;
		qu.push(con[i]);//当前任务也可以购买时间
		while(sum>con[i].d) {
		    node x = qu.top();qu.pop();
		    if(x.b>(sum-con[i].d)){
		    	x.b -= sum-con[i].d;
		    	ans += (sum-con[i].d)*1.0/x.a;
		    	sum = con[i].d;
		    	qu.push(x);
		    }else{
		    	ans += x.b*1.0/x.a;
		    	sum -= x.b;
		    }
		}
	}
	printf("%.2f\n",ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值