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;
}
思路:
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;
}
相关文章推荐
- 【POJ 2970】The lazy programmer(优先队列+贪心)
- POJ 2970 The lazy programmer(优先队列+贪心)
- POJ 2970 The lazy programmer(优先队列+贪心)
- POJ 2970 The lazy programmer(贪心+单调优先队列)
- POJ-2970-The lazy programmer
- POJ 2970 The lazy programmer 优先队列
- POJ 2970 The lazy programmer 优先队列
- poj 2970 The lazy programmer 优先队列
- POJ2970 The Lazy Programmer
- SPOJ 417 The lazy programmer(贪心)
- POJ - 3646 The Dragon of Loowater(贪心)
- POJ Face The Right Way(尺取法+贪心)
- poj 3262 Protecting the Flowers(贪心)
- POJ-2287 Tian Ji -- The Horse Racing 贪心规则在动态规划中的应用 Or 纯贪心
- (贪心5.1.2)POJ 2287 Tian Ji -- The Horse Racing
- POJ1989 The Cow Lineup——贪心+扫描技巧
- poj3262 Protecting the Flowers(贪心)
- (贪心5.1.2)POJ 2287 Tian Ji -- The Horse Racing
- 【POJ】2786-Keep the Customer Satisfied(贪心 + 优先队列,姿势不对就要跪)
- POJ 3262 Protecting the Flowers 贪心