PAT A1056. Mice and Rice (25)

大兔子大兔子 提交于 2021-02-18 08:00:40

 

原文链接: PAT A1056. Mice and Rice (25)

1056. Mice and Rice (25)

https://www.patest.cn/contests/pat-a-practise/1056

时间限制

100 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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 NP 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: NP 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 Wi 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 7 10 5 9 1 4 2 3

Sample Output:

5 5 5 2 5 5 5 3 1 3 5

题目大意:np为老鼠的数量,ng为每组最多g个老鼠。先给出np个老鼠的重量,再给出老鼠的初始顺序(第i名的老鼠是第j号,j从0开始)。每ng个老鼠分为一组,对于每组老鼠,选出最重的那个,晋级下一轮比赛,然后依次再以np个老鼠一组分类,然后选出重量最大的。。。直到只剩下一只老鼠,排名为1.输出为老鼠的排名,这个排名是按照原输入老鼠的顺序输出的。

题目给出的老鼠们的初始顺序是2编号的顺序,即6号老鼠排第一个,0号排第二个...

思路:

每次比赛将老鼠分成的组数group, 设当前轮的参赛老鼠数有tmp只, 如果tmp%ng==0,则能完整划分,因此group=tmp/ng,否则,说明会有少于ng只老鼠会单独分为一组,此时组数group=tmp/ng+1

由于每组晋级一只老鼠,因此当前轮晋级的总老鼠数等于group,且该轮未晋级的老鼠的排名均为group+1

用tmp记录当前轮的参赛老鼠数(初始值为np),group记录当前轮的组数,初始化时把老鼠们的编号按照顺序加入队列

之后进入while循环,每一层代表一轮比赛

对每一轮比赛,枚举队列内的当前轮的tmp只老鼠,按照每ng只老鼠一组选出组内质量最大的老鼠,并将其入队表示晋级,而当前轮老鼠的排名(group+1)可在选出最大老鼠的过程中直接对每只老鼠都赋值,晋级的老鼠会在下一轮比赛时获得新的排名,这样直到队列中只剩下一只老鼠,就把它的排名记为1,最后输出所有老鼠排名

#include <iostream>
#include <cstdio>
#include <queue> 
using namespace std;
const int MAX = 1111;
struct Mouse {
	int weight;	//老鼠质量 
	int rank;	// 排名 
}m[MAX];
int main(int argc, char *argv[]) {
	int  np, ng, order;
	scanf("%d%d", &np, &ng);
	for (int i = 0; i < np; i++)
		scanf("%d", &m[i].weight);
	queue<int> q;	//定义队列,队列保存老鼠在数组中的下标 
	for (int i = 0; i < np; i++) {
		scanf("%d", &order);
		q.push(order);	//按照顺序把老鼠的下标入队 
	}

	int tmp = np, group;	//tmp是当前轮的比赛总老鼠数,group是组数
	while (q.size() != 1) {
		//计算当前轮需要进行几场比赛
		if (tmp%ng == 0) group = tmp / ng;
		else group = tmp / 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 >= tmp) break;
				int front = q.front();		//队首老鼠编号
				if (m[front].weight > m[k].weight)
					k = front; //找出质量最大的老鼠
				m[front].rank = group + 1; //该轮老鼠排名为group+1 
				q.pop();	 //出队这只老鼠 

			}
			q.push(k);	//把胜利的老鼠晋级 
		}
		tmp = group;	//一共有group只老鼠晋级,因此下轮总老鼠数为group 
	}
	m[q.front()].rank = 1;	//只有一只老鼠时,排名为1
	for (int i = 0; i < np; i++) {
		printf("%d", m[i].rank);
		if (i < np - 1) printf(" ");
	}
	return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!