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​Pprogrammers. Then every N​Gprogrammers 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​Gwinners 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 contains2 positive integers N​Pand NG(≤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 NP distinct non-negative numbers W​i (i=0,⋯,N​P−1) where eachW​iis the weight of the i-th mouse respectively. The third line gives the initial playing order whichis a permutation of 0,⋯,NP−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

思路:

题目是真的读了半天才读懂,题意是给Np只老鼠,每只老鼠有重量,每次Ng只老鼠为一组来比赛,体重最大的那只进入下一轮,最后输出所有老鼠的排名。
运用队列模拟比赛顺序。队列中存放还需要进行比赛的老鼠序号,先算出进行完整小组赛的轮数epoch,每轮刚好都是Ng只为一组比赛,余下的老鼠再额外进行一轮比赛;每轮比赛晋级一只老鼠,由于晋级的老鼠排名会被后来的比赛覆盖,因此无论晋级与否,排名统一为 rank=比赛轮数+1(比方说现在队列中剩余的老鼠总共还要进行3轮比赛,那么此次被淘汰掉的老鼠排名均为4),直到队列中剩余的老鼠数量为1,该老鼠即为第一名。

源码:

#include<bits/stdc++.h>
using namespace std;
queue<int> order;
typedef struct every{
    int weight;
    int rank;
}mouse;
int main()
{
    int n,group;cin>>n>>group;
    mouse a[n];
    for(int i=0;i<n;i++)
        cin>>a[i].weight;
    for(int i=0;i<n;i++)
    {
        int t;cin>>t;
        order.push(t);
    }

    //只要还有2只及以上的老鼠名次没确定,就要一直比赛
    while(order.size()!=1)
    {
        int epoch=order.size()/group;//组数(以group只老鼠为一组)
        int add=order.size()%group;//余下的老鼠数量

        //进行epoch次比赛,每次group只,找出最大的那只晋级
        for(int i=0;i<epoch;i++)
        {
            int max=-1;
            int max_index=-1;
            for(int j=0;j<group;j++)
            {
                int t=order.front();
                order.pop();
                a[t].rank=epoch+!!add+1;//!!add将add转换为逻辑值,表示有老鼠剩余则额外需要进行一场,rank=比赛轮数+1
                if(a[t].weight>max)
                {
                    max=a[t].weight;
                    max_index=t;
                }
            }
            order.push(max_index);//晋级
        }
        //剩余的add只老鼠再进行一轮
        int max=-1,max_index=-1;
        for(int i=0;i<add;i++)
        {
            int t=order.front();
            order.pop();
            a[t].rank=epoch+!!add+1;
            if(a[t].weight>max)
            {
                max=a[t].weight;
                max_index=t;
            }
        }
        if(max_index!=-1)
        order.push(max_index);
    }
    a[order.front()].rank=1;
    cout<<a[0].rank;
    for(int i=1;i<n;i++)
        cout<<" "<<a[i].rank;
}
发布了97 篇原创文章 · 获赞 12 · 访问量 2396

猜你喜欢

转载自blog.csdn.net/weixin_43301333/article/details/104092271