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\n",ts++,count);
    }
    system("pause");
    return 0;
}
时间: 2024-07-30 07:42:06

POJ1323-Game Prediction的相关文章

POJ1323 Game Prediction(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 10475          Accepted: 5046 Description Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of

【POJ1323】Game Prediction 博弈,或者说贪心(本博客用栈处理)

题意: 输入m,n,表示有若干张卡牌,权值两两不同,且最大为m*n,交给至多m个人. 每回合所有人各出一张牌,权值最大者胜! 现在主角手里有n张牌,问他至少能赢多少局. 题解: 贪心.每次出最大牌看是否可以有人压制你. 或者换一种说法,就是所有人一起坑你,而且提前知道了你出什么牌,总之就是各种坑你!所以我们不考虑能赢多少把,我们考虑有多少把会输!再转换一下就是看多少张牌必赢,怎么针对都没用! 实现:不存东西的栈.没看懂就看代码...水水的~ #include <cstdio> #include

学习文章题目-Transfer learning for cross-company software defect prediction

所选主题:缺陷预测 论文题目: 1. Using class imbalance learning for software defect prediction 或 2.Transfer learning for cross-company software defect prediction 作者: 1. Wang Shuo, Yao Xin 2. Ying Ma, Guangchun Luo, Xue Zeng, Aiguo Chen 期刊: 1. IEEE transactions on

Intra Luma Prediction

Intra Luma Prediction 在宏块的帧内预测过程中,有四种宏块类型:I_4x4,I_8x8,I16x16,I_PCM.他们都需要在相邻块做去块滤波之前进行帧内预测. 下面为亮度帧内预测的总体流程 1-4获取当前block的帧内预测模式的预测,5-7获得最佳预测模式并对应预测模式的预测做后续处理 首先需要获得当前4x4(8x8)预测块有左.上的4x4(8x8)相邻块A.B,假设其所在宏块为mbAddrA.mbAddrB. 如果mbAddrA或mbAddrB中任意一个宏块不可用于帧内

MATLAB时间序列预测Prediction of time series with NAR neural network

具体请参考:http://lab.fs.uni-lj.si/lasin/wp/IMIT_files/neural/nn05_narnet/ format compact % data settings N = 249; % number of samples Nu = 224; % number of learning samples y = Data;% Input your data % prepare training data yt = con2seq(y(1:Nu)'); % prep

Your Prediction Gets As Good As Your Data

Your Prediction Gets As Good As Your Data May 5, 2015 by Kazem In the past, we have seen software engineers and data scientists assume that they can keep increasing their prediction accuracy by improving their machine learning algorithm. Here, we wan

HDU 1338 Game Prediction【贪心】

解题思路: 给出 n  m 牌的号码是从1到n*m 你手里的牌的号码是1到n*m之间的任意n个数,每张牌都只有一张,问你至少赢多少次 可以转化为你最多输max次,那么至少赢n-max次 而最多输max次,则是对方最多赢max次,则用对方的最小的牌去依次比较你手中的牌(按照升序排),如果找到有比它小的,则对方赢一次 依次循环直到遍历完对方的牌. Game Prediction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536

Intra Chroma Prediction

帧内预测依赖于当前宏块的相邻宏块,如果任何一个相邻宏块不可用,那么会直接影响到当前宏块的预测方式. 那么宏块怎么才谓之可用? 满足以下几个条件的相邻宏块为不可用: 相邻宏块超出边界,即(x<0 || x>PicWidthInMbs),(y<0 || y>PicHeightInMbs) 相邻宏块与当前处理的宏块不在同一slice 如果强制要求当前宏块的相邻宏块为intra(constrained_intra_pred_flag = 1),但实际上相邻宏块的编码方式为inter,该相邻

Adaptively handling remote atomic execution based upon contention prediction

In one embodiment, a method includes receiving an instruction for decoding in a processor core and dynamically handling the instruction with one of multiple behaviors based on whether contention is predicted. If no contention is predicted, the instru

Learn Branch Prediction From SimpleScalar Source (1)

作为一名CSer,最好的学习方式之一无疑是tracing code,看源代码--不知你此时是否与我一样想起了Linus那句名言「talk is cheap, show me the fucking code!」? 可是对计算机体系结构来说,很多技术直接是由硬件实现的,因而也被蒙上一层神秘的面纱. 好在还有一些模拟器(simulator)软件,例如SimpleScalar(http://www.simplescalar.com/)就是这样一套模拟处理器性能的工具集. 之前我有幸通过做一些proje