PAT甲级:1056 Mice and Rice

题目描述:

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=' ')

你可能感兴趣的:(PAT,python,算法,pat考试)