codeforces 1203F2 Complete the Projects (hard version)

传送门:http://codeforces.com/problemset/problem/1203/F2

由简单的我们知道要在b<0的部分,我们需要选择a+b尽可能大的

所以这题要求最多能选多少个,b>=0的情况我们尽量拿就行了

b<0的情况,就尽量先拿a+b大的,当某个时刻某个物品不能拿的时候,就从之前的拿的物品中b最小的(减得最多的)把他(可能是当前这个物品本身)丢出去,就是一种时光倒流法的贪心。

 

#include<bits/stdc++.h>
#define maxl 1010
using namespace std;

int n,r,ans;
struct node
{
	int a,b;
	bool operator < (const node &y)const
	{
		return a<y.a;
	}
	bool operator > (const node &y)const
	{
		return a>y.a;
	}
};
vector <node> pos,neg;
priority_queue<int,vector<int>,greater<int> >q;

inline void prework()
{
	scanf("%d%d",&n,&r);
	node d;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&d.a,&d.b);
		if(d.b<0)
			neg.push_back(d);
		else
			pos.push_back(d);
	}	
	sort(pos.begin(),pos.end());
	int l=neg.size();
	for(int i=0;i<l;i++)
	{
		neg[i].a+=neg[i].b;
		neg[i].a=max(neg[i].a,0); 
	}
	sort(neg.begin(),neg.end());
	reverse(neg.begin(),neg.end());
}

inline void mainwork()
{
	ans=0;
	int l=pos.size();
	for(int i=0;i<l;i++)
	if(r>=pos[i].a)
		r+=pos[i].b,ans++;
	l=neg.size();
	for(int i=0;i<l;i++)
	{
		r+=neg[i].b;
		q.push(neg[i].b);
		if(r<neg[i].a)
		{
			r-=q.top();
			q.pop();	
		}
	}
	ans+=q.size();
}

inline void print()
{
	printf("%d\n",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值