zoukankan      html  css  js  c++  java
  • POJ1323-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. 

      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=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
    ",ts++,count);
        }
        system("pause");
        return 0;
    }
  • 相关阅读:
    监控mysql的存储引擎
    基于复制的高可用
    No orientation specified, and the default is
    iOS 图标、图形尺寸? iPhone、iPad、 iPod touch
    cocos2d-x3.9利用cocos引擎一键打包Android平台APK(C++小白教程)
    loaded some nib but the view outlet was not set
    IOS开发:UIAlertView使用
    UIAlertView笔记
    Xcode之外的文档浏览工具--Dash (在iOS代码库中浏览本帖)
    iOS 开发者能用上的 10 个 Xcode 插件
  • 原文地址:https://www.cnblogs.com/lucio_yz/p/4771308.html
Copyright © 2011-2022 走看看