描述:
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.
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.
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.
题中说最少能赢的最大次数,意味着我们要求的是必胜的次数,可以脑补,当场上有人拿比你这次出的牌更大的牌的时候,你是必输的。
所以只需要知道场上有没有比你牌大的牌,就可以确定这次是不是必胜。可以用o(n2)的算法,设置一个数组,标记每张牌是否出过(没有一张牌点数相同),然后每出一个去找。
这里是o(n)的算法,记录了点数大于你这张牌的牌还没出的数目left,和上一次出牌较小的的点数-1(以便计算这次能有多少张牌没出)。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define N 105
int cmp( const void *a,const void *b ){
return *(int *)b-*(int *)a;
} int main(){
int m,n,a[N],count,ts=,left,max_left;
while( scanf("%d%d",&m,&n)!=EOF ){
if( m== && n== ) break;
for( int i=;i<n;i++ )
cin>>a[i];
qsort(a,n,sizeof(int),cmp);//递减 left=;max_left=m*n;count=;
for( int i=;i<n;i++ ){
if( left== ){//场上没有剩下的牌
if( max_left==a[i] ){
max_left--;
count++;//必胜
}
else{
left+=(max_left-a[i]-);//这次剩余多少没出
max_left=a[i]-;//记录
}
}
else{
left--;//对方用掉一张赢
left+=(max_left-a[i]);//这次剩余多少没出
max_left=a[i]-;//记录
}
}
printf("Case %d: %d\n",ts++,count);
}
system("pause");
return ;
}