codeforces 1203 F1 Complete the Projects (easy version) 贪心

https://codeforces.com/problemset/problem/1203/F1
题目大意:给出 n n n个任务,每个任务有 r a t i n g i rating_{i} ratingi限制和加分 s c o r e i score_{i} scorei( s c o r e i score_{i} scorei可能是个负数),给出你的初始 r a t i n g rating rating,当 r a t i n g > r a t i n g i rating>rating_{i} rating>ratingi时,才能完成第 i i i个任务,完成任务后: r a t i n g = r a t i n g + s c o r e i rating=rating+score_{i} rating=rating+scorei,你可以任意安排完成任务的顺序,且完成任意一个任务后必须满足 r a t i n g > = 0 rating>=0 rating>=0,若能实现输出 Y E S YES YES否则输出 N O NO NO

思路:贪心, s c o r e score score为正的,按照 r a t i n g rating rating从小到大排序, s c o r e score score为负的;在最坏情况下,每个任务完成后的最高分数为: r a t i n g = r a t i n g i + s c o r e i rating=rating_{i}+score_{i} rating=ratingi+scorei,按照这个值从大到小排序。

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define pr pair<int,int>
typedef long long ll;
using namespace std;

int n,tot;

struct node
{
	int a,b;
	node(){}
	node(int aa,int bb)
	{
		a=aa,b=bb;
	}
}a[105],b[105];

bool cmp1(node x,node y)
{
	return x.a<y.a;
}

bool cmp2(node x,node y)
{
	return (x.a+x.b)>(y.a+y.b);
}

int main()
{
	scanf("%d %d",&n,&tot);
	int u,v;
	int len1=0,len2=0;
	for(int i=0;i<n;i++)
	{
		scanf("%d %d",&u,&v);
		if(v>=0)
			a[len1++]=node(u,v);
		else
			b[len2++]=node(u,v);
	}
	bool flag=1;
	sort(a,a+len1,cmp1);
	sort(b,b+len2,cmp2);
	for(int i=0;i<len1;i++)
	{
		if(tot>=a[i].a)
			tot+=a[i].b;
		else
			flag=0;
	}
	for(int i=0;i<len2;i++)
	{
		if(tot>=b[i].a)
			tot+=b[i].b;
		else
			flag=0;
	}
	if(tot<0)
		flag=0;
	printf("%s\n",flag?"YES":"NO");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值