poj 2970 The lazy programmer 优先队列

题目链接:http://poj.org/problem?id=2970

先按deadline升序排

然后一件事一件事处理

这里需要【维护】已经过去的事情和当前这件事情中a值最大的项 这里就用到优先队列

时间不够的 就通过增大前面及当前的某件事的x值来补

这题卡精度

原来那种姿势不知道怎么就wa了

辛苦改成这种姿势以后

一开始在sum那里是整型过不了

然后改成double就过了

究其原因 应该也是【类型转换没有四舍五入】 导致精度损失

以后在除的地方 除了记得强制类型转换 还得考虑精度的问题

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 110000;
const double eps = 1e-7;

struct Node
{
    int a, b, d;
    double x;
    bool operator < (const Node& x) const
    {
        return a < x.a;
    }
}e[maxn];

bool cmp(const Node x, const Node y)
{
    return x.d < y.d;
}

priority_queue<Node> que;

int main()
{
    //freopen("in.txt", "r", stdin);

    int n;
    while(scanf("%d", &n) == 1)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].d);
            e[i].x = 0;
        }
        e[0].d = 0;

        sort(e + 1, e + n + 1, cmp);

        double ans = 0;
        double sum = 0;
        for(int i = 1; i <= n; i++)
        {
            que.push(e[i]);
            sum += e[i].b;
            if(sum > e[i].d)
            {
                //int tmp = sum - e[i].d;//需要减掉的时间
                while(sum > e[i].d)
                {
                    Node cur = que.top();
                    que.pop();
                    double z = (double)(sum - e[i].d) / cur.a;
                    if((double)cur.b/cur.a - cur.x > z)
                    {
                        cur.x += z;
                        ans += z;
                        sum -= z*cur.a;
                        que.push(cur);
                    }
                    else
                    {
                        z = (double)cur.b/cur.a - cur.x;
                        sum -= z*cur.a;
                        ans += z;
                        cur.x += z;
                    }
                }
            }
        }

        while(!que.empty())
            que.pop();

        printf("%.2f\n", ans);
    }

    return 0;
}
时间: 2024-11-06 22:06:57

poj 2970 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 优先队列

先按di排序,(从小到大).然后依次完成合同,若发现第i个合同无法在截止日期前完成,便从之前已经完成的任务中选一个aj最大的合同,付钱来使得这个合同尽快完成. #include<cstring> #include<cstdio> #include<iostream> #include<queue> #include<algorithm> using namespace std; struct node { int q; int w; bool o

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 directo

POJ 3253 Fence Repair (优先队列)

POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He the

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

poj 3253 Fence Repair(优先队列+哈夫曼树)

题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每一个父节点都是两个子节点的和.这个题就是可以从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个元素为止. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <cty

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv

POJ 1451 T9 字典树+优先队列

题目来源:POJ 1451 T9 题意:给你一些单词 和优先值 然后当你按下数字的时候首先会出现哪个单词 就是我们平时手机的按键 思路:建一颗字典树 因为按一个数字对应多个字母 那么就有多种情况 我们要输出权值最大的一个 我用了优先队列 这里每个前缀的优先值是所有单词优先值的和 例如abc 5 abd 6 acd 7 那么a这个前缀的优先值是18 ab的优先值是11 #include <cstdio> #include <cstring> #include <queue>

POJ 3253 Fence Repair(优先队列,哈夫曼树)

题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用于取前2个小的元素 #include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; _