Game Prediction

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?20) and n (1?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个人均分n*m张牌,每个人拥有m张牌,玩家选择一个人并拿出自己最大的牌与之相比较,如果比对方大,就赢,反之就输了并且输了的这张牌不能再用了。求玩家最少可能赢几次。

思路:将玩家的牌存在数组a中,然后从剩余的牌中选择m张较大的,注意一定要取大不取小·,将这m张牌存在一个数组b中并且分给一个人,在游戏中直接选择这个人与他进行游戏,这样才会使你赢得的次数最少。将这两个数组从大到小排,选择a的首元素与b的首元素比较,如果大于b,玩家赢,次数加一,直到小于b的首元素,此时a,b中元素下标均加一,以此类推,继续比较。代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int i,n,m,s,sum,l,k=1;
    int a[100],b[100],book[10010];
    while(~scanf("%d %d",&n,&m)&&(n||m))
    {
        sum=0;
        l=0;
        memset(book,0,sizeof(book));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(i=0; i<m; i++)
        {
            scanf("%d",&a[i]);
            book[a[i]]=1;
        }
        for(i=n*m; i>=1; i--)
        {
            if(l==m)
                break;    //如果找够m个结束
            if(book[i]==0)
                b[l++]=i; //找尽量大的并且存在b中
        }                 //求a最少赢几次,就让其中一个人的牌尽量大,a与之对抗,这样a赢得几率就是最小
        sort(a,a+m,cmp);
        sort(b,b+m,cmp);  //从大到小排
        s=0;
        for(i=0; i<m; i++)
        {
            if(a[i]>b[s])
                sum++;  //sum表示a赢了
            else
            {
                 s++;
            }
        }
        printf("Case %d: %d\n",k++,sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/angeliaaa/article/details/81103614