PAT A1056:Mice and Rice之队列模拟

题目描述

1056 Mice and Rice (25分)
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

求解思路

题意:给定NP和NG,NP表示老鼠的数目,NG表示每组的最大数目。而后给定NP只老鼠的重量,以及相应出场次序的老鼠索引。每次按出场顺序将NG只老鼠作为一组,每组选择重量最大的一只老鼠晋级,其他老鼠排名相同且被淘汰。最终需要输出每只老鼠的相应排名。

  • 使用一个队列用于按照顺序保存每一轮的老鼠索引
代码实现(AC)
#include iostream
#include queue
#include algorithm
using namespace std;
struct Mouse{
    int weight;
    int rank;
} ;
Mouse mouse[1001]; 
int NP, NG;
queueint q;
void init()
{
	scanf("%d %d", NP, NG);
    for(int i = 0; i  NP; i++){
        scanf("%d", mouse[i].weight);
    }
    for(int i = 0, t; i  NP; i++){
        scanf("%d", t);
        q.push(t);
    }
} 
void solve()
{
	init();
	// cnt为该轮老鼠数量,group为组数,maxWeight和maxIndex用于暂存某组中最重的老鼠的重量和下标
    int cntGroup, cnt, maxWeight, maxIndex;
    while ((cnt = (int)q.size())  1) {
        cntGroup = cnt / NG + (cnt % NG == 0 ? 0 : 1);
        for(int i = 0; i  cntGroup; i++){
            maxWeight = -1;
            maxIndex = -1;
            for(int j = 0; j  NG  i * NG + j  cnt; j++){
                mouse[q.front()].rank = cntGroup + 1;
                if(mouse[q.front()].weight  maxWeight){
                    maxIndex = q.front();
                    maxWeight = mouse[q.front()].weight;
                }
                q.pop();
            }
            q.push(maxIndex);
        }
    }
    /*获胜鼠*/
    mouse[q.front()].rank = 1;
    
    // 输出结果
    for(int i = 0; i  NP; i++){
        printf("%d", mouse[i].rank);
        if(i!=NP-1)	printf(" ");
    }
}
int main() 
{    
    solve();
    return 0;
}
最新回复(0)
/jishuV3J6NlQZupaEUaJtbQLcj9VSgVsi8StYh3ipCf1yVMM_3D4794636
8 简首页