標籤:
描述:
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.
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 105int cmp( const void *a,const void *b ){ return *(int *)b-*(int *)a;}int main(){ int m,n,a[N],count,ts=1,left,max_left; while( scanf("%d%d",&m,&n)!=EOF ){ if( m==0 && n==0 ) break; for( int i=0;i<n;i++ ) cin>>a[i]; qsort(a,n,sizeof(int),cmp);//遞減 left=0;max_left=m*n;count=0; for( int i=0;i<n;i++ ){ if( left==0 ){//場上沒有剩下的牌 if( max_left==a[i] ){ max_left--; count++;//必勝 } else{ left+=(max_left-a[i]-1);//這次剩餘多少沒出 max_left=a[i]-1;//記錄 } } else{ left--;//對方用掉一張贏 left+=(max_left-a[i]);//這次剩餘多少沒出 max_left=a[i]-1;//記錄 } } printf("Case %d: %d\n",ts++,count); } system("pause"); return 0;}
POJ1323-Game Prediction