其他分享
首页 > 其他分享> > PAT甲级1056Mice and Rice

PAT甲级1056Mice and Rice

作者:互联网

目录

题目介绍

题解

解题思路

代码

// Problem: PAT Advanced 1056
// URL: https://pintia.cn/problem-sets/994805342720868352/problems/994805419468242944
// Tags: 

#include <iostream>
#include <queue>
using namespace std;

int playerNum, playerMaxNumInAGroup; // 玩家数量,1组最多多少玩家
queue<int> nowPlayers, nextPlayers;     // 本轮玩家的索引和下轮玩家的索引
int weight[1002]; // 老鼠的重量
int rrank[1002]; // 玩家的排名
int groupNum; // 本轮比赛有多少组


/*一组玩家进行比赛*/
int findFatMouse(){
    int maxWeight = weight[nowPlayers.front()], fatMouse = nowPlayers.front();
    rrank[nowPlayers.front()] = groupNum + 1;
    nowPlayers.pop();

    for (int i=1; i < playerMaxNumInAGroup && !nowPlayers.empty(); i++){
        if (weight[nowPlayers.front()] > maxWeight){
            maxWeight = weight[nowPlayers.front()];
            fatMouse = nowPlayers.front();
        }
        rrank[nowPlayers.front()] = groupNum + 1;
        nowPlayers.pop();
    }
    nextPlayers.push(fatMouse);
}

/*计算本轮比赛有几组*/
int calcGroupNum(){
    int nowPlayerNum = nowPlayers.size();
    if (nowPlayerNum % playerMaxNumInAGroup == 0)
        return nowPlayerNum / playerMaxNumInAGroup;
    else
        return nowPlayerNum / playerMaxNumInAGroup + 1;
}

int main()
{
    // 读取变量
    scanf("%d%d", &playerNum, &playerMaxNumInAGroup);
    for(int i=0; i < playerNum; i++)
        scanf("%d", weight+i);
    for(int i=0, player; i < playerNum; i++){
        scanf("%d", &player);
        nowPlayers.push(player);
    }

    // 进行比赛
    while(nowPlayers.size() > 1){
        nextPlayers = queue<int>(); // 清空下一轮比赛的玩家队列
        groupNum = calcGroupNum();  // 计算本轮比赛有几组,本轮失败的玩家的rank即为groupNum + 1
        for (int i = 0; i < groupNum; i++) // 进行本轮的groupNum组比赛
            findFatMouse(); // 一组玩家进行比赛
        nowPlayers = nextPlayers; // 进行下一轮比赛,更新玩家队列
    }
    rrank[nowPlayers.front()] = 1; // 剩下的1个人就是最终的第1名

    // 输出排名
    printf("%d", rrank[0]);
    for (int i = 1; i < playerNum; i++)
        printf(" %d", rrank[i]);
    return 0;
}

参考链接


Github(github.com):@chouxianyu

Github Pages(github.io):@臭咸鱼

知乎(zhihu.com):@臭咸鱼

博客园(cnblogs.com):@臭咸鱼

B站(bilibili.com):@绝版臭咸鱼

微信公众号:@臭咸鱼的快乐生活

转载请注明出处,欢迎讨论和交流!


标签:PAT,比赛,int,rank,玩家,nowPlayers,front,Rice,1056Mice
来源: https://www.cnblogs.com/chouxianyu/p/14350629.html