PAT Advanced 1056 Mice and Rice (25) [queue Method of Use]

topic

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 lef 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

Topic analysis

Known number of entries N, the number M each, runners weight, the initial order entry. Each group of the most important wins, winning each round there are M individuals into the next group the next round
Note: Because each round, there are M home win, so the rest loser ranked M + 1, the winner ranked as M

Problem-solving ideas

  1. Defined queue queue, process simulation game, every game all participants into the team, and in the final set Sentinel (Sentinel indicate the end of this round of competition)
  2. And traversing the hierarchical tree structure differences: The title of the queue to exit the cycle is identified as the number of entries is 1, and the hierarchical tree traversal cycle exit queue node is not identified as a queue
  3. Each time a cycle to the next set of contestants, the winner of the previous group set a position added to the queue and finally, prepare for the next game

Code

#include <iostream>
#include <queue>
using namespace std;
struct M {
    int w=-1;//weight
    int i=-1;//id
    int r=-1;//rank
};
int main(int argc,char * argv[]) {
    int np,ng,t;
    scanf("%d%d",&np,&ng);
    M ms[np+1];
    for(int i=0; i<np; i++) {
        scanf("%d",&ms[i].w);
        ms[i].i=i;
    }
    M em;
    ms[np]=em;
    queue<int> q;
    for(int i=0; i<np; i++) {
        scanf("%d",&t);
        q.push(t);
    }
    int group = np%ng==0?np/ng:np/ng+1;
    int max=np,cs=np;
    q.push(-1);//第一轮最后加一个哨兵 
    for(int i=1; group>=1&&q.size()>=1; i++) {
        int cur = q.front();
        if(cur!=-1)ms[cur].r = group+1; 
        if(cur==-1) { // 若本轮比赛结束 
            q.pop(); //将哨兵弹出,方便计算下一场比赛人数 
            ms[max].r = group; //添加上一轮比赛最后一组的获胜者到队列,预备下一场比赛 
            if(cs==1)break; //如果当前参赛人员只有一人,并且已完成排名,退出 
            q.push(max); //将本组获胜者加入队列,预备下一轮比赛 
            cs=q.size(); //下一轮参赛人数 
            group=(cs%ng==0?cs/ng:cs/ng+1); //重新计算group 
            q.push(-1); //每一轮最后加一个哨兵 
            max=np; //max置为哨兵参照,weight=-1 
            i=0; //重置 指针 
            continue;
        } else if(i!=1&&i%ng==1) { //一组统计完成,当前为下一组第一个参赛者 
            ms[max].r = group;
            q.push(max);//将本组获胜者加入队列,预备下一轮比赛 
            max=cur; //cur为下一组的第一个参赛者 
        } else if(ms[max].w<ms[cur].w) { //更新本组获胜者 
            max=cur;
        }
        q.pop();
    }
    for(int i=0; i<np; i++) {
        if(i!=0)printf(" ");
        printf("%d",ms[i].r);
    }
    return 0;
}


Guess you like

Origin www.cnblogs.com/houzm/p/12274673.html