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. PHP + Nginx 在 Linux(centos7)系统下的环境搭建

    ( 选用的操作系统为 centos7 ) 01,安装 nginx => 请移步 https://www.cnblogs.com/lovling/p/9197572.html 02,下载 php  ...

  2. java reflect反射调用方法invoke

    类定义 package Reflect; public class MyTest { public int a; public static int b; public static final in ...

  3. 深度学习原理与框架-卷积神经网络基本原理 1.卷积层的前向传播 2.卷积参数共享 3. 卷积后的维度计算 4. max池化操作 5.卷积流程图 6.卷积层的反向传播 7.池化层的反向传播

    卷积神经网络的应用:卷积神经网络使用卷积提取图像的特征来进行图像的分类和识别       分类                        相似图像搜索                        ...

  4. 机器学习入门-数值特征-连续数据离散化(进行分段标记处理) 1.hist(Dataframe格式直接画直方图)

    函数说明: 1. .hist 对于Dataframe格式的数据,我们可以使用.hist直接画出直方图 对于一些像年龄和工资一样的连续数据,我们可以对其进行分段标记处理,使得这些连续的数据变成离散化 就 ...

  5. Java快速开发平台——JEECG 3.7.8 版本发布!我们的目标是有鱼丸也有粗面

    JEECG 3.7.8 版本发布,多样化主题UI满足你不同的需求 导读                    ⊙平台性能优化,速度闪电般提升           ⊙提供5套新的主流UI代码生成器模板( ...

  6. jquery实现增删改(伪)-老男孩作业day13

    使用jquery进行,文件的编写,实现自增id,删除,添加,编辑模式. jquery放在本地,src="jquery_js.js" 可以改成其他,或者在线的路径 readme &l ...

  7. ubuntu 安装oracle客户端

    from: http://webikon.com/cases/installing-oracle-sql-plus-client-on-ubuntu Installing Oracle SQL*Plu ...

  8. centos下同时启动多个tomcat

    1.解压apache-tomcat-7.0.69.tar.gz到/usr/local目录 .tar.gz -C /usr/local 2.新建目录tomcat7_1和tomcat7_2 tomcat7 ...

  9. RH_KABI_RESERVE的使用

    struct mm_struct { .......... #if defined(__GENKSYMS__) || !defined(CONFIG_SPAPR_TCE_IOMMU) /* We're ...

  10. springmvc简单教程

    IDEA建立Spring MVC Hello World 详细入门教程(转自)   引子,其实从.NET转Java已经有几个月时间了,项目也做了不少,但是很多配置都是根据公司模板或者网上教程比忽略画瓢 ...