PAT 甲级 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 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

Sample Output:

5 5 5 2 5 5 5 3 1 3 5

对我而言,这道题吃了三个亏。1.读题,第二行给出每个老鼠的重量w[i],第三行的playing order意思为初始分组。即19 25 57 22 10 3  56 18 37 10 46,而非其他顺序,一开始就想的很复杂,导致对应的类也设计错了。2.数据结构的选择,这题用队列最为合适,用数组反倒不知道在做什么,G个分为一组,每次先出队列,更新排名,而后把组内最大的重新入队列。直到栈为空循环结束。 3.写这题时的要点,关于G个分为一组,不用去想什么整体for循环,或者整除不整除,内层再嵌套一个关于G的循环,能力不够根本写不出。直接老老实实外层一个for循环访问队列所有元素,定义一个变量cnt,出队列cnt+1,cnt 到G或i 下标到了队列尾就cnt 赋值为0》。纠缠太久的主观原因还是没有彻底地读懂这一题,包括怎么尽可能简单地去写代码,怎么设计合理的数据结构。一碰到栈和队列就懵了。4.最后的输出,输出要求按照老鼠下标

代码如下,非我劳动成果,柳神的看懂了而已:

#include 
#define maxn 1010

using namespace std;

struct Mice
{
    int weight;
    int Rank;
    int index0;
    int index;
};

bool cmp(Mice m1,Mice m2)
{
    if(m1.index0!=m2.index0)
    {
        return m1.index0>N>>G;
    int w[maxn] = {0};

    vector v(N);
    queue q;

    for(int i = 0; i>w[i];
    }

    int num = 0;
    for(int i = 0; i>num;
        v[i].weight = w[num];
        v[i].index0 = num;
        v[i].index = i;
    }

    for(int i = 0; i maxnum)
            {
                maxnum = temp.weight;
                maxnod = temp;
            }
            if(cnt==G||i==Size-1)   //同上一条注释,把G个成员分在同一小组,未整除另成一组
            {
                q.push(maxnod);
                cnt = 0;
                maxnum = -1;
            }
        }
    }
    sort(v.begin(),v.end(),cmp);

   for(int i = 0;i

 

你可能感兴趣的:(#,队列,PAT)