The lazy programmer POJ - 2970

原题链接
考察:贪心
错误思路:
  对于每个任务,按d,a顺序排序,如果不能按时完成就付钱使得按时完成.
思路:
  不一定要压当前任务的时间,我们可以压花费更小的任务时间,使得超时任务按时完成.

Code

#include <iostream> 
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int N = 100010;
typedef long long LL;
struct Node{
	int a,b,d;
}node[N];
struct cmp2{
	bool operator()(Node n,Node m){
		if(m.d==n.d) return m.a>n.a;
		return n.d<m.d;
	}
};
struct cmp{
	bool operator()(Node n,Node m){
		return n.a<m.a;
	}
};
int n;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int a,b,d;
		scanf("%d%d%d",&a,&b,&d);
		node[i].a = a,node[i].b = b,node[i].d = d;
	}
	double ans = 0;
	sort(node+1,node+n+1,cmp2());
	priority_queue<Node,vector<Node>,cmp> q;
	LL now = 0;
	for(int i=1;i<=n;i++)
	{
		now+=(LL)node[i].b;
		q.push(node[i]);
		while(q.size()&&now>node[i].d)
		{
			Node it = q.top();
			q.pop();
			if(now-node[i].d>=it.b)
			{
				ans+=1.0*it.b/it.a;
				now-=it.b;
			}else{
				ans+=(now-node[i].d)*1.0/it.a;
				it.b-=now-node[i].d;
				now-=now-node[i].d;
				q.push(it);
			}
		}
	}
	printf("%.2lf\n",ans);
	return 0;
}

posted @ 2021-06-18 22:18  acmloser  阅读(26)  评论(0编辑  收藏  举报