P2649 游戏预言

题目描述

John和朋友们在玩纸牌游戏,他们一共有m个人(包括John)。他们的纸牌比较特殊,一共有n*m张牌,牌号分别为1,2,…,n*m,没有牌号相同的牌。每个人先拿到n张牌,然后,每一轮,每个人出一张牌,谁最大则谁赢得这一轮。现在已知John手中的n张牌,计算他最少能赢得多少轮。

输入格式

第一行为两个整数m和n,2≤m≤20,1≤n≤50;第二行有n个正整数,表示John手中的n张牌的数值。

输出格式

仅一个整数,表示John最少能赢的次数。

输入输出样例

输入 #1
2 5
1 7 2 10 9
输出 #1
2
输入 #2
6 11
62 63 54 66 65 61 57 56 50 53 48
输出 #2
4

贪心加桶排

#include<cstdio>

using namespace std;

int n,i,j,m,a[5005],b[5005],ans,cnt;

inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            w=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*w;
}

int main(){
    m=read();
    n=read();
    for(i=1;i<=n;i++){
        a[i]=read();
        b[a[i]]=1;
    }
    n*=m;
    for(i=n;i>=1;i--){
        if(!b[i]){    
            ++cnt;
        }
        else if(cnt>0){
            --cnt;
        }
        else{
            ++ans;
        }
    }
    printf("%d ",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/hrj1/p/11669073.html