PAT甲级1056

PAT甲级1056

原题:
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 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 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 7 10 5 9 1 4 2 3
Sample Output:
5 5 5 2 5 5 5 3 1 3 5

题目大意:
给出总人数以及每组最大人数,按照一定序列进行淘汰赛

注意点:
1、题目虽然说是非负数,但实际上都是整数,且没有重复,可以在数组末尾设置一个负数作为判断点
2、分组要用到ceil函数
3、要记录每轮队列中最后一个选手,以便判断该轮结束
4、排名从组数+1开始递减
5、最后一个也就是第一名要单独判断,不然输出的是2
6、用队列进行模拟,赢家继续进队列,输的踢掉

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;

int main(){
	int np,ng;	//np总人数,ng每组最大人数
	scanf("%d%d",&np,&ng);
	vector<int> a(np+1),b(np);	//a每个人的序号,b每个人的排名
	queue<int> q;
	for(int i=0;i<np;i++) scanf("%d",&a[i]);
	a[np]=-1;	//哨兵
	for(int i=0;i<np;i++){	//所有选手按分组序列进队列
		int tmp;
		scanf("%d",&tmp);
		q.push(tmp);
	}
	while(!q.empty()){
		bool flag=false;	//记录每轮结束
		int t=q.back(),l=q.size(),length=ceil(l*1.0/ng);	//length记录分组情况
		for(int i=0;i<length;i++){
			int max=np;	//记录每轮赢家
			for(int j=0;j<ng;j++){
				if(a[q.front()]>a[max]) max=q.front();
				b[q.front()]=length+1;
				if(q.front()==t){
					q.pop();
					flag=true;	//该轮已结束
					break;
				}
				q.pop();
			}
			if(l!=1) q.push(max);	//当队列就剩最后一个时就不用继续处理了
			else b[max]=1;	//将最终排名矫正
			if(flag) break;
		}
	}
	for(int i=0;i<np;i++){
		printf("%d",b[i]);
		if(i!=np-1) printf(" ");
	}
	system("pause");
	return 0;
}
### 关于 PAT 甲级 1024 题目 PAT (Programming Ability Test) 是一项编程能力测试,其中甲级考试面向有一定编程基础的学生。对于 PAT 甲级 1024 题目,虽然具体题目描述未直接给出,但从相似类型的题目分析来看,这类题目通常涉及较为复杂的算法设计。 #### 数据结构的选择与实现 针对此类问题,常用的数据结构包括但不限于二叉树节点定义: ```cpp struct Node { int val; Node* lchild, *rchild; }; ``` 此数据结构用于表示二叉树中的节点[^1]。通过这种方式构建的二叉树能够支持多种遍历操作,如前序、中序和后序遍历等。 #### 算法思路 当处理涉及到图论的问题时,深度优先搜索(DFS)是一种常见的解题策略。特别是当需要寻找最优路径或访问尽可能多的节点时,结合贪心算法可以在某些情况下提供有效的解决方案[^2]。 #### 输入输出格式说明 根据以往的经验,在解决 PAT 类型的问题时,输入部分往往遵循特定模式。例如,给定 N 行输入来描述每个节点的信息,每行按照如下格式:“Address Data Next”,这有助于理解如何解析输入并建立相应的数据模型[^4]。 #### 数学运算示例 有时也会遇到基本算术表达式的求值问题,比如分数之间的加减乘除运算。下面是一些简单的例子展示不同情况下的计算结果: - \( \frac{2}{3} + (-2) = -\frac{7}{3}\) -2) = -\frac{4}{3}\) - \( \frac{2}{3} ÷ (-2) = -\frac{1}{3}\) 这些运算是基于样例提供的信息得出的结果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值