POJ-2970-The lazy programmer

思路:优先队列解决,先按结束日期排序,然后若当前截止日期无法完成当前截止日期之前的所有任务,那么从队伍里选出a值最小的进行“加班”

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1e5+1000;
struct Node
{
    int a;
    int b;
    int d;
    bool operator < (const Node &s)const
    {
        return a<s.a;
    }
}a[maxn];
int n;
priority_queue<Node> q;
int cmp(Node sa,Node sb)
{
    return sa.d<sb.d;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        while(!q.empty())
            q.pop();
        for(int i=0;i<n;i++)
            scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].d);
        sort(a,a+n,cmp);
        long long sum=0;
        double ans=0;
        for(int i=0;i<n;i++)
        {
            sum+=a[i].b;
            q.push(a[i]);
            while(!q.empty()&&sum>a[i].d)
            {
                Node top=q.top();
                q.pop();
                if(sum-a[i].d>top.b)
                {
                    ans+=top.b*1.0/top.a;
                    sum-=top.b;
                }
                else
                {
                    ans+=(sum-a[i].d)*1.0/top.a;
                    top.b-=sum-a[i].d;
                    sum=a[i].d;
                    q.push(top);
                }
            }
        }
        printf("%.2f\n",ans);
    }
    return 0;
}


你可能感兴趣的:(优先队列)