POJ1323 Game prediction (greedy algorithm training)

Source: Internet
Author: User
Tags rounds

Time limit: 1000MS Memory Limit: 10000K Total submissions: 10475 accepted: 5046

Description

Suppose there is 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 are at most n*m. And there is no 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 had been chosen, the player who had won the most rounds was the winner O f the game.
Given Your cards received at the beginning, write a program to tell the maximal number of rounds so you could at least Win during the whole game.

Input

The input consists of several test cases. The first line of all case contains, 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 is a line with n positive integers, representing the pips of cards in the received. Then a blank line follows to separate the cases.
The input is terminated by a line with the zeros.

Output

For each test case, output a line consisting of the "test case" number followed by the number of rounds you'll at least WI n during the game.

Sample Input

2 51 7 2 10 96 1162 63 54 66 65 61 57 56 50 53 480 0

Sample Output

Case 1:2case 2:4

First, the main topic

There are M individuals, one person n cards, each round the face of the biggest person win (the card may be a number in the 1~m*n and not repeat), give a person's hand, ask him to win at least the number of innings.

Second, the idea of solving problems

The idea of this topic is relatively simple, because the number of people and the number of cards is less, so the direct violence solution, with the first sample analysis, with a type of bool array. Mark the cards in my hand as true, the table is as follows:

10 9 8 7 6 5 4 3 2 1
? ? ? ? ?

? My opponent

Obviously when I was 7 o'clock, the opponent may have the face card for 8 can win me, and so on, so you can use an int type to record the opponent currently has more than my biggest card is also larger than the number of cards, when encountered I have the big brand when the consumption of a big card, if at this time the opponent is not bigger than my card, Then this round I absolutely win.

Third, the specific code

1#include <cstdio>2#include <cstring>3 4 intMain () {5     intM, N, I, J, TMP, count=1;6     BOOLcards[1050];7      while(SCANF ("%d%d", &m, &n) && m &&N) {8Memset (Cards,false,sizeof(cards));9          for(i=0; i<n; i++){Tenscanf"%d", &j); OneCARDS[J] =true; A         } -         intlarge_cards=0, ans=0; -          for(I=m*n; i>0; i--){ the             if(!cards[i]) large_cards++; -             Else{ -                 if(Large_cards = =0) ans++; -                 Elselarge_cards--; +             } -         } +          Aprintf"Case %d:%d\n", count, ans); atcount++; -     } -      -     return 0; -}
View Code

POJ1323 Game prediction (greedy algorithm training)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.