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. Excel和Word 简易工具类,JEasyPoi 2.1.7 版本发布

    JEasyPOI 简介 EasyPOI 功能如同名字easy,追求的就是简易,让一个没接触过poi的人员,可以傻瓜化的快速实现Excel导入导出.Word模板导出,可以仅仅5行代码就可以完成Excel ...

  2. Jsoup解析网页源码时常用的Element(s)类

    Jsoup解析网页源码时常用的Element(s)类 一.简介 该类是Node的直接子类,同样实现了可克隆接口.类声明:public class Element extends Node 它表示由一个 ...

  3. leetcode ex3 找出穿过最多点的直线 Max Points on a Line

    题目 https://oj.leetcode.com/problems/max-points-on-a-line/ 答案与分析 http://www.aiweibang.com/yuedu/18326 ...

  4. 利用nginx搭建RTMP视频点播、直播、HLS服务器(转)

    开发环境 Ubuntu 14.04 server nginx-1.8.1 nginx-rtmp-module nginx的服务器的搭建 安装nginx的依赖库 sudo apt-get update ...

  5. 尚硅谷springboot学习22-Thymeleaf入门

    Thymeleaf是一种模板引擎,类似于JSP.Velocity.Freemarker

  6. Java中Asm包有什么用?

    ASM能做什么 我们都知道,一般情况下,Class文件是通过javac编译器产生的,然后通过类加载器加载到虚拟机内,再通过执行引擎去执行. 现在我们可以通过ASM的API直接生成符合Java虚拟机规范 ...

  7. 6.5 Shell 算术计算

    6.5 Shell Arithmetic shell允许在其内计算表达式,可以通过以下方式使用:((中,let和带-i选项的declare命令中. 只能计算固定长度的整数,而且不会检查溢出,除0可以捕 ...

  8. ETC2 区别于ETC的重要点

    ETC2 主要是对于NPOT却是4的倍数的贴图有较大压缩,比如一个1920X1080RGB的Loading图,ETC4压缩下不管用,大小5.9M,ETC2下压缩为1M

  9. linux 一个跟踪文件删除的小技巧

    最近有同事问我说他有个现场环境,经常会丢失业务文件,每天都出现,几百个里面丢失1到两个. 为了解决这个问题,我让他布置audit,具体可以man一下auditctl. 过了一天,他说audit.log ...

  10. python学习笔记之函数的参数

    函数的参数有位置参数和关键字参数,位置参数一定要在关键字参数的前面,位置参数的优先级是高于关键字参数的,否则会报错 def my_abs(a,b): print(a) print(b) my_abs( ...