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. ssm学习的第一个demo---crm(1)

    这是一个普通的CRM项目 (第一步规划好项目设计路线:导入jar包→配置sqlMapConfig.xml(空文件)→配置applicationContext.xml →配置springMVC.xml→ ...

  2. JAVA SpringBoot2 整合 JSP视图模板 整合 Ueditor富文本编辑器

    一般涉及到后台管理系统,就少不了富文本编辑器,这个可以图,文,视频混排的高级工具,笔者通过对比,发现目前市场上最好的三方库还当属百度的 ueditor 近年来 SpringBoot 框架可谓越来越火, ...

  3. word中怎样设置页码包含总页数

    一个同事做毕业论文,论文是Word格式,1-2页是封面和目录,不需要页码,第3-10页是论文内容,需要从第1页开始显示,并显示论文内容的总页数8 页.具体为页脚处显示“第*页共*页”.他让我帮忙设置一 ...

  4. Installing PHP5 on Ubuntu Server

    When installing PHP 5 from source I ran into the following problems and solutions: Problem:configure ...

  5. MiniDao_1.6.4 版本发布,轻量级Java持久化框架,Hibernate项目辅助利器

    MiniDao 简介及特征 MiniDao 是一款超轻量的JAVA持久层框架,具备Mybatis一样的SQL能力: 支持SQL分离.支持标签.支持注解.MiniDao产生的初衷是为了解决Hiberna ...

  6. beyondCompare工具使用

    1.下载beyondcompare  (从官网下载) 2.载入.class文件比对 参见: beyond compare 对class文件反编译及比较  (https://blog.csdn.net/ ...

  7. 开发webapp手机返回键 退出问题 摘录

    mui进行手机物理键的监听 确保引入mui 调用以下函数 // android 返回按键处理 androidBack(store, data) { try { mui.init({ keyEventB ...

  8. CSS GRID ESSENTIALS

    CSS GRID ESSENTIALS Review At this point, we've covered a great deal of different ways to manipulate ...

  9. Servlet基本_画面遷移

    画面遷移方法は.下記ようがある.・リクエストのディスパッチ・リダイレクト(画面から) 1.ディスパッチ1)概念サーブレットから他のリソース(サーブレット.JSP.Htmlなど)にリクエストを転送するこ ...

  10. jeecg使用小结

    1.上传word模板时报“java.lang.UnsatisfiedLinkError: no jacob-1.17-M2-x64 in java.library.path”异常 原因:jdk下没有找 ...