当前位置:网站首页>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=' ')
边栏推荐
猜你喜欢
随机推荐
loj 6038「雅礼集训 2017 Day5」远行
无头单向非循环链表(C语言实现)
H5不同屏幕大小显示不同的文字大小图片大小
兆骑科创创业赛事活动举办平台,投融资对接,线上直播路演
logistic regression model - based on R
shell------常用小工具,sort,uniq,tr,cut
变量和函数的声明提前
年初离职,学习半年源码,终于拿到了蚂蚁Offer,分享面试过程
keil5——安装教程附资源包
浏览器跨域方案,适用于本地调试接口(超简单)
在中国银河证券开户安全吗 齐齐哈尔股票开户
开源一夏 | 自己画一块ESP32-C3 的开发板(PCB到手)
俄驻美大使馆:扎波罗热核电站遭炮击威胁欧洲核安全
初窥门径代码起手,Go lang1.18入门精炼教程,由白丁入鸿儒,首次运行golang程序EP01
【小码匠自习室】重做ABC250-D, 我无力反抗
mysql 查询一个字段为特定值,并且另一个字段的值出现两次的记录?
[内部资源] 想拿年薪30W的软件测试人员,这份资料必须领取
现在网上开户安全么?接着证券开户选择哪个证券?
【小码匠自习室】ABC258-A 代码写的啰嗦了
星起航跨境—当前形势下,突破思维做精细化运营才能提高转化