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​​ (≤), 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​​ (,) 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 (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

注意:

1.仔细理解题目给出的初始顺序的理解,结合给出示例理解

2.因为排名和重量一一对应,可以建立结构体,而比赛顺序可以放进队列,逐轮减少

3.这里比较巧妙的是每一轮都覆盖赋值一遍现在老鼠的排名:group+1

4.每一轮的group不一样,是根据当前老鼠总数算得,故用temp保存当前老鼠总数

#include<cstdio>
#include<queue>
using namespace std;

struct Mou{
    int weight;
    int r;
}a[1010];

int main(){
    int n,m,t;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i].weight);
    queue<int> q;
    for(int i=0;i<n;i++){
        scanf("%d",&t);
        q.push(t);
    }
    int temp=n,group;//temp:numbers of mice
    while(q.size()!=1){
        if(temp%m==0) group=temp/m;
        else group=temp/m+1;
        for(int i=0;i<group;i++){
            int max=q.front();
            for(int j=0;j<m;j++){
                if(i*m+j>=temp) break;//last mouse
                int front=q.front();
                if(a[front].weight>a[max].weight) max=front;
                a[front].r=group+1;
                q.pop();
            }
            q.push(max);
        }
        temp=group;
    }
    a[q.front()].r=1;
    for(int i=0;i<n;i++){
        printf("%d",a[i].r);
        if(i<n-1) printf(" ");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/exciting/p/10398955.html