CF155B B - Combination

Ilya plays a card game by the following rules.

A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded.

More formally: let’s say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.

Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has.

Each of the next n lines contains two non-negative space-separated integers — ai and bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly.

Print the single number — the maximum number of points you can score in one round by the described rules.

Input
2
1 0
2 0
Output
2

Input
3
1 0
2 0
0 2
Output
3

Note
In the first sample none of two cards brings extra moves, so you should play the one that will bring more points.

In the second sample you should first play the third card that doesn’t bring any points but lets you play both remaining cards.

这题,感觉不算太难,再cmp的设计上感觉很好玩。
看完题后的感觉是类似于贪心,也就是先把所给数据按某种顺序进行排序

#include<cstdio>
#include<algorithm>
using namespace std;
struct emm     /*设置所输入数据的结构体*/
{
int ai;
int bi;
}card[1005];
bool cmp(emm a,emm b) /*对cmp函数进行设计*/
{
if(a.bi!=b.bi)    /*不相等时把较大bi放前面,便于计数器加上较大的值,使其多进行几次*/
return a.bi>b.bi;
else
return a.ai>b.ai; /*如果bi相等,如bi=0,则把ai中较大的值放前面,便于取到最大值*/
}
int main()
{
int n;
int num=1;//num为计数器
int ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
	scanf("%d %d",&card[i].ai,&card[i].bi);
	if(card[i].bi>0)
	{
		num=num+card[i].bi-1;//bi>0,则计数器-1+bi的值不会为0,所以把ai的值加进去。
		ans+=card[i].ai;
	}
}
sort(card,card+n,cmp);//排序
for(int i=0;i<n&&num;i++)//注意&&num
{
	if(card[i].bi==0)
	{
		ans+=card[i].ai;//更新bi=0时的总值
		num--;//计数器的值-1
	}
}
printf("%d",ans);
return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值