poj2970 The lazy programmer 【优先队列】
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 ai, bi, di (1 ≤ ai, bi ≤ 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 【优先队列】的更多相关文章
- POJ 2970 The lazy programmer(优先队列+贪心)
Language: Default The lazy programmer Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1 ...
- POJ 2970 The lazy programmer
The lazy programmer Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2785 Accepted: 70 ...
- I - The lazy programmer 贪心+优先队列
来源poj2970 A new web-design studio, called SMART (Simply Masters of ART), employs two people. The fir ...
- POJ 2970 The lazy programmer(贪心+单调优先队列)
A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...
- Keep It Simple
The KISS principle, or Keep It Simple, Stupid, spans many trades, industries, and professions. The m ...
- “盛大游戏杯”第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 描述 在一个矩形的灰度图像上,每个 ...
- 程序员能力矩阵 Programmer Competency Matrix
[译文]程序员能力矩阵 Programmer Competency Matrix [译文]程序员能力矩阵 Programmer Competency Matrix 注意:每个层次的知识都是渐增的,位于 ...
- 最小生成树-普利姆算法lazy实现
算法描述 lazy普利姆算法的步骤: 1.从源点s出发,遍历它的邻接表s.Adj,将所有邻接的边(crossing edges)加入优先队列Q: 2.从Q出队最轻边,将此边加入MST. 3.考察此边的 ...
- 索引式优先队列(indexed priority queue)
为了达到O(ElogV)的效率,需要对普利姆算法进行eager实现. 如果我们用java来做,jdk当中的priorityQueue并不能满足我们的要求. 因为我们需要进行一个对索引元素降key的操作 ...
随机推荐
- 白鹭引擎 - 遮罩( Rectangle )
1: 矩形遮罩 class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用 ...
- dubbo超时优先级设置
调用超时配置的优先级 可以在多个配置项设置超时,由上至下覆盖(即上面的优先),示例如下: # 其它的参数(retries.loadbalance.actives等)的覆盖策略也一样. 提供者端特定方法 ...
- USB之HID类Set_Report Request[调试手记1]
请翻开<Device Class Definition for Human Interface Devices (HID) Version 1.11 >7.2.2 Set_Report R ...
- day40-socket编程
一.socket介绍 看socket之前,先来回顾一下五层通讯流程: 但实际上从传输层开始以及以下,都是操作系统帮咱们完成的 Socket又称为套接字,它是应用层与TCP/IP协议族通信的中间软件抽象 ...
- python-玉米(小米)商城作业
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- YCSB性能测试工具使用(转)
在网上查In-Memory NoSQL性能测试的资料时,偶然间发现了这个性能测试工具YCSB,全称为“Yahoo! Cloud Serving Benchmark”.它内置了对常见NoSQL数据库和数 ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- ZEOSDBO-7安装
zeosdbo是一套免费开源的Delphi数据库连接组件,可连接mssql.mysql.sybase.oracle.firebird.sqlite.postgresql等多种数据库.调用方法简单. 连 ...
- RTX二次开发SDK需要注意的地方
1.如果是ASP.net二次开发调用的,线程池必须开发 Enable 32-bit Applications设置为true. 因为RTX的调用接口很多是32位的.否则会报一下错误 RootObj = ...
- C#关键字as出现的错误
ObjectCache cache = MemoryCache.Default; string cacheData1 = cache["key1"] as string;//得不到 ...