数据结构专题——队列的应用 A1056.Mice and Rice ( 25)

#include <bits/stdc++.h>
#include<math.h>
#include <string>
using namespace std;
const int maxn = 1010;
struct mouse{
    int weight;//质量
    int R;//排名
}mouse[maxn];
int main(){
    int np,ng,order;
    scanf("%d%d",&np,&ng);
    for(int i=0;i<np;++i){
        scanf("%d",&mouse[i].weight);
    }
    queue<int> q;//定义一个队列
    for(int i=0;i<np;++i){
        scanf("%d",&order);
        q.push(order);
    }
    int temp = np,group;//temp为当前轮的比赛总老鼠数,group为组数
    while(q.size() != 1){
        //计算group,即当前轮分为几组进行比赛
        if(temp % ng == 0){
            group = temp/ng;
        }else{
            group = temp/ng + 1;
        }
        //枚举每一组,选出该组老鼠中质量最大的
        for(int i = 0;i<group;++i){
            int k = q.front();//k存放该组质量最大的老鼠的编号
            for(int j =0;j<ng;++j){
                //在最后一组老鼠数不足NG时起作用,退出循环
                if(i * ng + j >= temp) break;
                int front = q.front();//队首老鼠编号
                if(mouse[front].weight > mouse[k].weight){
                    k = front;
                }
                mouse[front].R = group + 1;//该轮老鼠排名为group + 1
                q.pop();
            }
            q.push(k);
        }
        temp = group;//group只老鼠晋级,因此下轮总老鼠数为group
    }
    mouse[q.front()].R = 1;//当队列中只剩下1只老鼠时,令其排名为1
    //输出所有老鼠的信息
    for(int i=0;i<np;++i){
        printf("%d",mouse[i].R);
        if(i < np - 1){
            printf(" ");
        }
    }
    system("pause");
    return 0;
} 

原文地址:https://www.cnblogs.com/JasonPeng1/p/12208702.html