PAT_(队列应用) 1056 Mice and Rice

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.

题目大意:就是说有np只老鼠,每ng只mouse分为一组,每组选出最重的那只,晋级下一轮,直到最终的winner产生

 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#define maxn 1010
using namespace std;
struct node{
	int weight;
	int rank;
}pre[maxn];
queue<int> qu;
int main(){
	int np,ng,order; //11个num,3个一组 
	scanf("%d%d",&np,&ng);
	for(int i=0;i<np;i++){
		scanf("%d",&pre[i].weight);
	}
	for(int i=0;i<np;i++){
		scanf("%d",&order);
		qu.push(order);
	}
	int num=np,group; //num正在比赛的人数, group组数
	while(qu.size()!=1){
		if(num%ng==0) group=num/ng;
		else group=num/ng+1;
		for(int i=0;i<group;i++){  //每次一组  
			int k=qu.front();
			for(int j=0;j<ng;j++){ //每组的max 
				if(i*ng+j>=num) break; //针对最后一组 
				int first=qu.front();
				if(pre[k].weight<pre[first].weight){
					k=first;
				}
				pre[first].rank=group+1; //前面选出了group个mouse,所以这些mice的排名就是group+1
				qu.pop();
			} 
			qu.push(k);//晋级 
		}//一轮结束
		num=group;	
	} 
	pre[qu.front()].rank=1;
	for(int i=0;i<np;i++){
		printf("%d",pre[i].rank);
		if(i<np-1) printf(" ");	
	}
	return 0;
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值