首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

PAT甲级-1056 Mice and Rice (25分)

  • 24-03-03 00:44
  • 2241
  • 7325
blog.csdn.net

点击链接PAT甲级-AC全解汇总

题目:
Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse.

First the playing order is randomly decided for N​P​​ programmers. Then every N​G​​ programmers are grouped in a match. The fattest mouse in a group wins and enters the next turn. All the losers in this turn are ranked the same. Every N​G​​ winners are then grouped in the next match until a final winner is determined.

For the sake of simplicity, assume that the weight of each mouse is fixed once the programmer submits his/her code. Given the weights of all the mice and the initial playing order, you are supposed to output the ranks for the programmers.

Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: N​P​​ and N​G​​ (≤1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than N​G​​ mice at the end of the player’s list, then all the mice left will be put into the last group. The second line contains N​P​​ distinct non-negative numbers W​i​​ (i=0,⋯,N​P​​ −1) where each W​i​​ is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,⋯,N​P​​ −1 (assume that the programmers are numbered from 0 to N​P​​ −1). All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the final ranks in a line. The i-th number is the rank of the i-th programmer, and all the numbers must be separated by a space, with no extra space at the end of the line.

Sample Input:

11 3
25 18 0 46 37 3 19 22 57 56 10
6 0 8 7 10 5 9 1 4 2 3
  • 1
  • 2
  • 3

Sample Output:

5 5 5 2 5 5 5 3 1 3 5
  • 1

题意:
例子中输入的意思是:
一共有11个数,每3个一组比大小,每组胜出的进入下一轮,不足3人的单独一组
第二行输入是编号0 ~ 10这11个数的值
第三行输入是比较的排序,先6号,再0号。。以此类推
所以:
初始号 : 6 0 8 , 7 10 5 , 9 1 4 ,2 3
初始数 :19 25 57 , 22 10 3 ,56 18 37 ,0 46
第一轮 : 57 , 22 ,56 , 46
第二轮 : 57 , 46
第三轮 : 57

结果是 57排名1,46排名2,22和56排名并列3,其他都是并列第5
输出0 ~ 10这11个数各自的排名

我的思路:
num[]:存放0 ~ Np 这几个数
rank[]:存放0 ~ Np 这几个数的排名

每一轮过滤掉没有晋级下一轮的,他们的排名都是并列(晋级的人数+1)名
当前一共n人的时候,晋级人数为( 1 + 向 上 取 整 [ n − 1 N g ] 1+向上取整[\frac{n-1}{N_g}] 1+向上取整[Ng​n−1​]);
每组晋级的保存到next,一轮结束后把next给now;

注意:每一轮排名都先设置为( 2 + 向 上 取 整 [ n − 1 N g ] 2+向上取整[\frac{n-1}{N_g}] 2+向上取整[Ng​n−1​]),晋级的人的排名会再各自淘汰的时候更新,最后胜出的最后更新

我的代码:

#include
using namespace std;

int main()
{
    int Np,Ng;
    cin>>Np>>Ng;
    int num[Np]={0},rank[Np]={0};
    queue<int>now;
    for(int i=0;i<Np;i++)cin>>num[i];
    for(int i=0;i<Np;i++)
    {
        int t;
        cin>>t;
        now.push(t);
    }

    while(now.size()>1)
    {
        int rank_for_loss=2+ceil((now.size()-1)/Ng);//本轮输者排名
        int win_id=now.front(),cnt=0;
        queue<int>next;  //胜者放入next

        //找出本轮的胜者放到next
        while(now.size())
        {
            int id=now.front();
            now.pop();
            cnt++;
            rank[id]=rank_for_loss;//胜者等输了再更新
            if(num[win_id]<num[id])win_id=id;
            if(cnt>=Ng||(!now.size()))
            {
                next.push(win_id);
                if(now.size())win_id=now.front();
                cnt=0;
            }
        }
        now=next;//更新now
    }
    rank[now.front()]=1;//最终的胜者排名为1

    cout<<rank[0];
    for(int i=1;i<Np;i++)cout<<" "<<rank[i];
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
注:本文转载自blog.csdn.net的邂逅模拟卷的文章"https://blog.csdn.net/qq_34451909/article/details/105755348"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top