PAT Advanced1056 Mice and Rice(队列,模拟)

版权声明:个人学习笔记记录 https://blog.csdn.net/Ratina/article/details/86524192

链接:PAT Advanced1056

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 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: N​P and NG​​ (≤1000), 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(i=0,⋯,NP​−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,⋯,NP−1 (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 87 10 5 9 1 4 2 3

Sample Output:

5 5 5 2 5 5 5 3 1 3 5


题意:
给出 NP 只老鼠的质量,并给出它们的初始顺序,按这个初始顺序把这这些老鼠按每 NG 只分为一组,最后不够 NG 只的也单独分为一组。对每组老鼠,选出它们中质量最大的 1 只晋级,这样晋级的老鼠数就等于该轮分组的组数。对这些晋级的老鼠再按上面的步骤每 NG 只分为一组进行比较,选出质量最大的一批继续晋级,这样到最后只剩下一只老鼠,排名为1。把这些老鼠的排名按原输入的顺序愉出。

注意, 同一轮被淘汰的老鼠名次相同,其名次取决于有多少老鼠排在他们前面,如样例中,第一轮4只老鼠晋级,7只老鼠淘汰,那么这7只老鼠的名次均为5。

方法:
将老鼠的序号以初始顺序存入队列中,每次按规则处理同时让晋级的老鼠重新入队列,当队列中只剩一只老鼠时结束。


以下代码:

#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
struct mouse
{
	int w;
	int rank;
}a[1010];
int main()
{
	int i,NP,NG;
	queue<int> q;
	cin>>NP>>NG;
	for(i=0;i<NP;i++)
		cin>>a[i].w;
	for(i=0;i<NP;i++)
	{
		int t;
		cin>>t;
		q.push(t);
	}
	while(q.size()>1)
	{
		int left=q.size(),maxw=0,maxi;    //left表示该轮还有多少老鼠
		for(i=1;i<=left;i++)
		{
			a[q.front()].rank=ceil(left*1.0/NG)+1;   //算出该轮被淘汰老鼠的名次
			if(a[q.front()].w>maxw)                  //即晋级的老鼠数+1
			{                                        //晋级的老鼠数=left/NG向上取整
				maxw=a[q.front()].w;
				maxi=q.front();
			}
			if(i%NG==0||i==left)          //每NG个一组
			{
				q.push(maxi);
				maxw=0;
			}
			q.pop();
		}
	}
	a[q.front()].rank=1;    //最后的剩下的一个排名为 1
	for(i=0;i<NP;i++)
	{
		if(i!=0)
			cout<<" ";
		cout<<a[i].rank;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Ratina/article/details/86524192
今日推荐