PAT A1056 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 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个编程者在比赛中为一组。组内最胖的老鼠胜利并进入下一轮比赛。这一轮中所有的失败者排名相同。每NG个获胜者为一组进入下一个比赛。

为了简单起见,假设一旦程序员提交了他/她的代码,每个鼠标的重量都是固定的。给出所有鼠标的重量和初始播放顺序,您应该为程序员输出排名。

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N​P​​ and N​G​​ (≤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 N​P​​ distinct non-negative numbers W​i​​ (i=0,⋯,N​P​​−1) where each W​i​​ is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,⋯,N​P​​−1 (assume that the programmers are numbered from 0 to N​P​​−1). All the numbers in a line are separated by a space.

第一行包含2个正数:NP和NG。第二行包含NP个互不相同的非负数Wi  分别表示第i个老鼠的重量。第三行给出初始顺序

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.

给出最终排名 第i个数字是第i个程序员的排名。

#include<cstdio>
#include<queue>
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;
	
	while(q.size()!=1){
		//分组 
		if(temp%ng==0) group = temp/ng;
		else group = temp/ng +1;
		
		//遍历每一组 用k存储每一组胜利者编号 
		for(int i=0;i<group;i++){
			int k = q.front(); 
			for(int j=0;j<ng;j++){
				if(i*ng+j>=temp) break;//超出np,就停止循环 
				int front = q.front();
				if(mouse[front].weight>mouse[k].weight)
					k=front;
					
				mouse[front].R = group+1;
				q.pop();//出队 
			}
			q.push(k);//晋级者入队 
		}
		
		
		temp=group;//group只老鼠晋级,因此下一轮老鼠数为group 
	}
	
	//当队列中只剩1只老鼠时,令其排名为1	
	mouse[q.front()].R=1;
	
	
	for(int i=0;i<np;i++){
		printf("%d",mouse[i].R);
		if(i<np-1) printf(" ");
	} 
	return 0;
 } 
扫描二维码关注公众号,回复: 4826667 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_38179583/article/details/86023376
今日推荐