您的位置:首页 > 其它

Poj 2970 The lazy programmer(贪心)

2017-10-08 17:15 477 查看
题目地址:http://poj.org/problem?id=2970

思路:

1.按照d值从小到大排序,依次完成任务。

2.将所有已完成任务加入优先队列(a值越大越优先),若当前任务无法完成,则从队列中不断找到a值最大的任务,花费一定钱数减少完成时间,直到当前时间等于d。

3.若某任务花费值为b/a,则以后花费钱任务时间也不会减少,从队列中删除。

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100000+50;

struct Node
{
int a,b,d;
double s;
bool operator < (const Node& rhs) const
{
return a<rhs.a;
}
};

int n;
Node a[maxn];
priority_queue<Node> q;

int cmp(Node a,Node b)
{
return a.d<b.d;
}

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

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

sort(a,a+n,cmp);

double now=0.0;
double ans=0.0;

for(int i=0; i<n; i++)
{
now+=a[i].b;
q.push(a[i]);
while(now>a[i].d)
{
Node del=q.top();
q.pop();

double t=now-a[i].d;
if(del.s+1.0*t/del.a<(double)del.b/del.a)
{
now-=t;
ans+=1.0*t/del.a;
del.s+=1.0*t/del.a;
q.push(del);
}
else
{
double t=del.b-del.s*del.a;
now-=t;
ans+=t/del.a;
}
}
}
printf("%.2f\n",ans);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: