Description
Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of a card is a positive integer which is at most N*M. And there are no two cards with the same pip. During a round, each player chooses one card to compare with others. The player whose card with the biggest pip wins the round, and then the next round begins. After N rounds, when all the cards of each player have been chosen, the player who has won the most rounds is the winner of the game.
Given your cards received at the beginning, write a program to tell the maximal number of rounds that you may at least win during the whole game.
input
The input consists of several test cases. The first line of each case contains two integers m (2 <= m <= 20) and n (1 <= n <= 50), representing the number of players and the number of cards each player receives at the beginning of the game, respectively. This followed by a line with n positive integers, representing the pips of cards you received at the beginning. Then a blank line follows to separate the cases.
The input is terminated by a line with two zeros.
output
For each test case, output a line consisting of the test case number followed by the number of rounds you will at least win during the game.
Sample Input
2 5
1 7 2 10 9
6 11
62 63 54 66 65 61 57 56 50 53 48
0 0
Sample Output
Case 1: 2
Case 2: 4
题意
n个人在玩牌,每个人有m张牌,于是就有n*m张牌(每张牌都有一个值,介于1到n*m之间,不重复),然后进行m轮游戏,每轮每个人都出一张牌,牌最大的那个人就赢了,然后给出n和m,以及你的m张牌,问你最多能赢几轮? 就是你在最坏的情况下能赢多少局,
思路
因为问的是起码能赢多少局,所以考虑最坏状况
先把自己的牌按从大到小进行排序
m轮,每次看自己当前最大的是不是全场最大的,
是的话答案加一,然后对自己的下一张牌进行操作
不是的话,对其他人的牌减1,记录输牌次数
代码如下:
#include<stdio.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
int a[500];
int sum=1;
int main()
{
while(sum)
{
int win=0,lose=0;
int m,n;
scanf("%d %d",&n,&m);
if(m==0&&n==0)
break;
int i;
for(i=1;i<=m;i++)
scanf("%d",&a[i]);
sort(a,a+1+m);//把自己的牌按从大到小进行排序
for(i=n*m;i>=1;i--)
{
if(a[m]!=i)
lose++;//如果自己的牌不是当前最大的就为输
else
{
if(lose)
lose--;
else
win++;//是最大的赢牌数+1
m--;
if(m<0)
break;
}
}
printf("Case %d: %d\n",sum,win);
sum++;
}
return 0;
}