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;
    }
  • 相关阅读:
    剑指offer之 二维数组的查找
    常用的基础算法总结之 希尔排序
    让shell脚本中的echo输出带颜色
    nginx利用lua实现nginx反向代理proxy_store缓存文件自删除
    LNMP平滑升级nginx并安装ngx_lua模块教程
    nginx的luajit安装luarocks并安装luafilesystem
    PHP图片识别成文字
    使用tesseract-ocr破解网站验证码
    利用开源程序(ImageMagick+tesseract-ocr)实现图像验证码识别
    http://ocr.wdku.net/
  • 原文地址:https://www.cnblogs.com/lucio_yz/p/4771308.html
Copyright © 2011-2022 走看看