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个合同,截止日期分别是di,有个程序员,完成每个合同的时间是bi。对于合同i,给他x元钱,相应完成时间变为bi-ai*x。求需要最少的钱数,保证该程序员按时完成所有合同。

算法:所有合同,按照截止日期排序。维护一个优先队列,存储历史上完成的合同及相应使用时间。每下一个合同时间不够用,就在历史上选择ai最大的合同,将其时间用钱来买,从而增加当前合同可以使用的时间。统计付钱总额。

代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm> #define llint long long
#define LEN 100000 double ans;
int n;
int d[LEN], a[LEN], b[LEN], t[LEN]; struct pair2{
int a,len;
bool operator < (const pair2 &tmp) const{
return a<tmp.a;
}
}pairs[LEN];
std::priority_queue<pair2*> pq; void input();
void work();
void output(); int main(){
input();
work();
output(); return ;
} void input(){
scanf("%d", &n);
for(int i=;i<n;i++){
scanf("%d %d %d", &a[i], &b[i], &d[i]);
t[i]=i;
}
}
bool compareSort(const int& i, const int& j){
return d[i]<d[j];
}
void work(){
int pair_i=, cur=;
std::sort(t, t+n, compareSort);
for(int i=;i<n;i++){
int &index = t[i];
int remain=d[index]-cur;
if(remain>=b[index]){
pairs[pair_i].a = a[index];
pairs[pair_i].len = b[index];
pq.push(&pairs[pair_i++]);
cur += b[index];
}else{
pairs[pair_i].a = a[index];
pairs[pair_i].len = remain; remain = b[index]-remain;
while(!pq.empty()){
pair2* p = pq.top();
if (remain <= p->len){
p->len -= remain;
ans += double(remain)/a[index];
if (!p->len) pq.pop();
remain = ;
break;
}else{
ans += double(p->len)/a[index];
pq.pop();
remain -= p->len;
}
}
if (remain){
ans += double(remain)/a[index];
} pq.push(&pairs[pair_i++]);
cur = d[index];
}
}
}
void output(){
printf("%.2f\n", ans);
}

poj2970 The lazy programmer 【优先队列】的更多相关文章

  1. POJ 2970 The lazy programmer(优先队列+贪心)

    Language: Default The lazy programmer Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1 ...

  2. POJ 2970 The lazy programmer

    The lazy programmer Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2785   Accepted: 70 ...

  3. I - The lazy programmer 贪心+优先队列

    来源poj2970 A new web-design studio, called SMART (Simply Masters of ART), employs two people. The fir ...

  4. POJ 2970 The lazy programmer(贪心+单调优先队列)

    A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...

  5. Keep It Simple

    The KISS principle, or Keep It Simple, Stupid, spans many trades, industries, and professions. The m ...

  6. “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】

    黑白图像直方图 发布时间: 2017年7月9日 18:30   最后更新: 2017年7月10日 21:08   时间限制: 1000ms   内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...

  7. 程序员能力矩阵 Programmer Competency Matrix

    [译文]程序员能力矩阵 Programmer Competency Matrix [译文]程序员能力矩阵 Programmer Competency Matrix 注意:每个层次的知识都是渐增的,位于 ...

  8. 最小生成树-普利姆算法lazy实现

    算法描述 lazy普利姆算法的步骤: 1.从源点s出发,遍历它的邻接表s.Adj,将所有邻接的边(crossing edges)加入优先队列Q: 2.从Q出队最轻边,将此边加入MST. 3.考察此边的 ...

  9. 索引式优先队列(indexed priority queue)

    为了达到O(ElogV)的效率,需要对普利姆算法进行eager实现. 如果我们用java来做,jdk当中的priorityQueue并不能满足我们的要求. 因为我们需要进行一个对索引元素降key的操作 ...

随机推荐

  1. leetcode1022

    class Solution(object): def __init__(self): self.li = list() self.sums = 0 def Trace(self,root): if ...

  2. Mysql 获取表属性

    获取表字段信息: select column_name from information_schema.COLUMNS where table_name='表名' nformation_schema. ...

  3. 使用STM32CubeMX生成USB_HOST_HID工程[添加对CAPS_LOCK指示灯的控制][SetReport]

    在之前(使用STM32CubeMX生成USB_HOST_HID工程)的基础上进行修改 在结合之前在pc上的测试 USB之HID类Set_Report Request[调试手记1] 测试代码如下: /* ...

  4. git push error HTTP code = 413

    error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large 将 ...

  5. python中Strip()函数的用法

    Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符. str.strip([chars]) ...

  6. table布局与div布局

      DIV与TABLE本身并不存在什么优缺点,所谓web标准只是推荐的是正确的使用标签,好比说:DIV用于布局,而TABLE则本来就是转二维数据的.让TABLE做该做的事,并不是说页面里不出现TABL ...

  7. jquery接触初级----- 一种新奇的选择器用法

    今天看到一个新奇的jquery 选择器的用法,因为以前没有见过,所以记录下来 1.jquery 选择器: 给body添加一个元素,添加元素的时候,同时把属性和点击事件都一起进行添加 <!DOCT ...

  8. ACM__最小生成树之prime

    今天做了一道题,根本没想到最小生成树,稀里糊涂的浪费了很多时间,复习一下 转载自https://www.cnblogs.com/zhangming-blog/p/5414514.html Prim算法 ...

  9. MySQL5.7 并行复制配置

    转自:https://www.cnblogs.com/langdashu/p/6125621.html [MySQL] 号称永久解决了复制延迟问题的并行复制,MySQL5.7 一.缘由: 某天看到主从 ...

  10. Constructor构造方法

    我们写一个car类,并写一个无参构造方法. public class Car { int speed; //构造方法名字和 类一致 区分大小写 不需要写返回值 和参数列表 public Car(){ ...