USACO 2009 Open Gold 2.Work Scheduling

类似贪心,在前i 时间内那i 个价值最大的。但不能完全用贪心解,因为有时间限制。

比如:

4
4 19
1 5
1 1
4 3

如果按v 排序的话最大时间限制为4, 则4 个都能取,但实际上(1,1)不能取。

#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 100005
typedef long long ll;
ll n;
struct node
{
	ll t,v;
}edge[inf];
int cmp(node a,node b)
{
	return a.t<b.t;
}
priority_queue <node>Q;
bool operator< (node a,node b)
{
	return a.v>b.v;
}
int main()
{
	scanf("%lld",&n);
	for(ll i=1;i<=n;i++)
	{
		scanf("%lld %lld",&edge[i].t,&edge[i].v);
	}
	sort(edge+1,edge+1+n,cmp);
	ll maxn=0;
	ll ans=0;
	for(ll i=1;i<=n;i++)//始终保持前i时间时取i个v 最大的。
	{
		Q.push(edge[i]);
		maxn=edge[i].t;
		while(Q.size()>maxn)Q.pop();
	}
	while(Q.size())
	{
		ans+=Q.top().v;
		Q.pop();
	}
	printf("%lld",ans);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值