SPOJ 417 The lazy programmer(贪心)

417. The lazy programmer

Problem code: LAZYPROG

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 influence other contracts. This
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

First line of the input contains an integer t (1 ≤ t ≤ 45), equal to the number of testcases. Then descriptions of t testcases follow.

First line of description contains the number of contracts N (1 ≤ N ≤ 100000, integer). Each of the next Nlines describes one contract and contains integer numbers aibidi (1 ≤ aibi ≤ 100001 ≤ di ≤1000000000)
separated by spaces.

At least 90% of testcases will have 1 ≤ N ≤ 10000.

Output

For each testcase in the input your program should output one line with a single real number S. Here 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.

Example

Input:
1
2
20 50 100
10 100 50

Output:
5.00


题意:有n个任务,每一个任务都有deadline,以及完成所需要的时间b,还有一个参数a,如果花x,那么他所需要的时间就变成b-x*a。任务需要一个接一个完成。问保证每个任务在deadline之前完成所需要花费的钱数

思路:首先确定的是deadline小的先处理。当发现curT+b[i] > deadline[i]的时候,只要从前面取出a[j]最大的,然后把curT往前挪。a[j]就用优先队列维护。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long LL;
#define REP(_,a,b) for(int _ = (a); _ <= (b); _++)
const int maxn = 100000+10;
const double eps = 1e-8;
int dcmp(double x) {
    if(fabs(x) < eps) return 0;
    else if(x > 0) return 1;
    else return -1;
}
struct Programer{
    double last,deadline,a,cost;
    Programer(double last=0,double deadline=0,double a=0):last(last),deadline(deadline),a(a){}
    friend bool operator < (Programer a,Programer b) {
        return a.a < b.a;
    }
}P[maxn];
bool cmp(Programer a,Programer b) {
     if(a.deadline != b.deadline) return a.deadline < b.deadline;
     else return a.last < b.last;
}
int n;
priority_queue<Programer> pqP;
void init() {
    while(!pqP.empty()) pqP.pop();
}
void input() {
    REP(i,1,n) {
        scanf("%lf%lf%lf",&P[i].a,&P[i].last,&P[i].deadline);
        P[i].cost = 0.0;
    }
    sort(P+1,P+1+n,cmp);
}
void solve() {
    double curT = 0,ret = 0;
    REP(i,1,n) {
        pqP.push(P[i]);
        if(dcmp(curT+P[i].last-P[i].deadline)>0) {
             while(dcmp(curT+P[i].last-P[i].deadline)>0) {
                Programer tmp = pqP.top();
                pqP.pop();
                double extra = (tmp.last/tmp.a-tmp.cost)*tmp.a;
                double need = curT+P[i].last-P[i].deadline;
                if(dcmp(need-extra) >= 0) {
                    ret += tmp.last/tmp.a-tmp.cost;
                    curT -= extra;
                }else {
                    curT -= need;
                    tmp.cost += need/tmp.a;
                    ret += need/tmp.a;
                    pqP.push(tmp);
                }
            }
            curT = P[i].deadline;
        }else {
            curT += P[i].last;
        }
    }
    printf("%.2f\n",ret);
}
int main(){
    int ncase;
    cin >> ncase;
    while(ncase--) {
        scanf("%d",&n);
        init();
        input();
        solve();
    }
    return 0;
}

时间: 2024-10-03 09:04:53

SPOJ 417 The lazy programmer(贪心)的相关文章

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

Language: Default The lazy programmer Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1566   Accepted: 386 Description A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is a web-designer and

【POJ 2970】The lazy programmer(优先队列+贪心)

这题范围不会超long long全用int存就行了 贪心的话,每次把一个任务加入到队列,如果不能在指定时间完成就到前面找a最小的一个任务补偿时间,当一个任务完成时间等于0的时候这个任务就不再放回队列 #include<cstdio> #include<queue> #include<algorithm> #include<cstring> using namespace std; //typedef long long LL; const int maxn

Codeforces Round #335 (Div. 2) D. Lazy Student 贪心

D. Lazy Student Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph — something that will definitely never be useful in real life. He asked a girl sitting next to hi

poj 2970 The lazy programmer 优先队列

题目链接:http://poj.org/problem?id=2970 先按deadline升序排 然后一件事一件事处理 这里需要[维护]已经过去的事情和当前这件事情中a值最大的项 这里就用到优先队列 时间不够的 就通过增大前面及当前的某件事的x值来补 这题卡精度 原来那种姿势不知道怎么就wa了 辛苦改成这种姿势以后 一开始在sum那里是整型过不了 然后改成double就过了 究其原因 应该也是[类型转换没有四舍五入] 导致精度损失 以后在除的地方 除了记得强制类型转换 还得考虑精度的问题 #i

SPOJ MSTICK. Wooden Sticks 贪心 结构体排序

MSTICK - Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine

SPOJ - LOCKER 数论 贪心

题意:求出\(n\)拆分成若干个数使其连乘最大的值 本题是之江学院网络赛的原题,计算规模大一点,看到EMAXX推荐就做了 忘了大一那会是怎么用均值不等式推出结果的(还给老师系列) 结论倒还记得:贪心分解3,不够就用2凑 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #

SPOJ:Decreasing Number of Visible Box(不错的,背包?贪心?)

Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman wants to hide boxes as many as possible. A box can be kept hidden inside of another box if and only if the box in which it will be held is empty and

SPOJ:PATHETIC STRINGS(分配问题&amp;贪心)

Problem statement: A string is said to be “PATHETIC” if all the characters in it are repeated the same number of times. You are given a string of length n, what is the minimum number of changes required to make a string “PATHETIC”. The string has onl

转摘&lt;&lt;On becoming an expert C programmer&gt;&gt;

The following is from an EMail message that I sent to to an individual on 12-Apr-2001. You may find this EMail message useful. The Writer asked: ``I just recently visited your website ... Just wanted to say greetings and ask a simple question: Do you