当前位置:网站首页>PAT甲级:1056 Mice and Rice
PAT甲级:1056 Mice and Rice
2022-08-08 14:40:00 【正在黑化的KS】
题目描述:
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 NP programmers. Then every NG 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 NG 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: NP and NG (≤1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than NG 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 NP distinct non-negative numbers Wi (i=0,⋯,NP−1) where each Wi is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,⋯,NP−1 (assume that the programmers are numbered from 0 to NP−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
Sample Output:
5 5 5 2 5 5 5 3 1 3 5
代码长度限制
16 KB
时间限制
200 ms
内存限制
64 MB
题目大意:
老鼠和大米是一个编程竞赛的主题,程序员们需要编写代码控制老鼠在给定的地图上移动,每只老鼠的目标都是吃掉尽可能多的大米,从而变成肥老鼠。
共有 NP 个程序员参赛,入场顺序随机,每 NG 个程序员被分为一组。
组中最胖的老鼠获胜,并进入下一轮。
所有在本回合中失败的老鼠排名都相同。
获胜者继续每NG 个一组,进行比赛,直到决出唯一胜者为止。
为了简单起见,当程序员们提交了代码后,他们的老鼠的最终重量就已经确定了。
给定所有老鼠的重量和程序员们的参赛顺序,请你为程序员们排名。
输入格式
第一行包含两个整数 NP 和 NG,分别表示总参赛人数以及每组最多人数。
如果分组到最后,剩下不足 NG 个人,则剩下的所有人分为一组。
所有 NP 只老鼠的编号为 0∼NP−1。
第二行包含 NP 个不同的非负整数 Wi(i=0,1,…,NP−1),其中 Wi 表示编号为 i 的老鼠的重量。
第三行包含一个 0∼NP−1的排列,表示老鼠的具体参赛顺序,以样例为例,6 号老鼠排在第一个,0 号老鼠排在第二个,以此类推。
输出格式
输出一行 NP 个整数,其中第 i 个整数表示编号为 i 的老鼠的最终排名。
题目思路: 模拟整个过程即可 详见代码注释
Python3代码:
nP,nG = map(int,input().split())
Rank = [0] * nP
w = list(map(int,input().split()))
curr = list(map(int,input().split()))
idxRank = 0
record = [] # 淘汰者
while len(record) < nP -1 :
next = [] # 晋级者
restCnt = len(curr) # 剩余选手数量
for i in range(0,restCnt,nG) :
maxId,maxW = -1,-1
j = 0
while j < nG and i + j < restCnt : # 小组内进行比赛
idx = i + j
if w[curr[idx]] > maxW :
if maxW != -1 : record.append(maxId) # 记录淘汰者
maxW = w[curr[idx]]
maxId = curr[idx]
else : record.append(curr[idx])
j += 1
next.append(maxId) # 记录晋级者
while idxRank < len(record) : # 更新淘汰者排名,晋级人数+1
Rank[record[idxRank]] = len(next) + 1
idxRank += 1
curr = next # 更新下一轮比赛人员
Rank[curr[0]] = 1 # 仅剩冠军还在curr数组
for i in range(nP) : print(Rank[i],end=' ')
边栏推荐
- JS-BOM-通过或运算-可以实现缺省值
- shell三剑客-----sed命令
- JS-BOM-for,if(字符串转大小写)
- 变量和函数的声明提前
- [Small Coder Study Room] [NOI Online 2020-2 Beginner Group] Finished: Abominable precision will make you burnt
- Time to update your tech arsenal in 2020: Asgi vs Wsgi (FastAPI vs Flask)
- 2022-08-07 The fifth group Gu Xiangquan study notes day31-collection-Map collection
- 接口测试,
- JS-BOM-阶乘计算
- 良心到难以置信的网站推荐第7期丨全程干货
猜你喜欢
随机推荐
mysql 查询一个字段为特定值,并且另一个字段的值出现两次的记录?
什么是幂等性
Shell三剑客之sed命令详解
「复盘」面试BAMT回来整理398道高频面试题,助你拿高薪offer
并发请求如何优雅地处理重复请求
shell三剑客-----awk命令
JS Adder (DOM)
[Redis] Bitmap and usage scenarios of bitmap (statistics of online people and user online status)
egg.js框架的基本设置 及 使用
【小码匠自习室】让错误成为孩子进步的阶梯
【小码匠自习室】[NOI Online 2020-3 入门组] 最急救助:被“幸运女神”眷顾的人
星起航跨境—跨境电商进入3.0时代,卖家迎来全新机遇
线程的状态简介说明
bandanas Kerchief头巾是何含义?
2022-08-07 第五小组 顾祥全 学习笔记 day31-集合-Map集合
【电路基础2】电容
JS - BOM - - can be achieved through calculation or default values
JS-BOM-通过或运算-可以实现缺省值
【Kaggle】Save My Paper 基于自编码器的文本图像去噪
1052. 爱生气的书店老板