题目:
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.
2 1 0 2 0
2
3 1 0 2 0 0 2
3
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.
题意分析:
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node
{
int a;
int b;
}a[1005];
int cmp(Node x,Node y)
{
if(x.b==y.b)
return x.a>y.a;
else
return x.b>y.b;
}
int main()
{
int n,temp,ans;
while(scanf("%d",&n)!=EOF)
{
temp=1;
ans=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].a,&a[i].b);
}
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
{
temp--;
ans+=a[i].a;
temp+=a[i].b;
if(temp==0)
break;
}
printf("%d\n",ans);
}
}
本文介绍了一种卡牌游戏的策略玩法,玩家需选择最佳卡牌以获得最高得分。通过贪心算法对卡牌底部数值进行降序排列,并考虑额外可打牌数,最终确定最优策略。
1696

被折叠的 条评论
为什么被折叠?



