PAT A1056 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 NGwinners 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,⋯,N​P​​−1) where each Wi​​ is the weight of the iii-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为每组最多ng个老鼠。先给出np个老鼠的重量,再给出老鼠的初始顺序(第 i 名的老鼠是第 j 号,j 从0开始)。每ng个老鼠分为一组,对于每组老鼠,选出最重的那个,晋级下一轮比赛,然后依次再以np个老鼠一组分类,然后选出重量最大的。直到只剩下一只老鼠,排名为1输出为老鼠的排名,这个排名是按照原输入老鼠的顺序输出的。

思路:

  1. 这道题目主要是因为读了题目两遍还是没有读懂题目。。。。所以说英语还是很重要的,主要是对一些英语句式的理解不透彻导致的。
  2. 开一个结构体,如何定义其实可以自己定义,看了一下别人的代码,也有用和书上不一样的方法,但是目前来说葵花宝典的代码长度最少,而且最简单。
  3. 使用while循环体,每一层循环代表一轮比赛,对于每轮比赛,枚举队列中所有老鼠,按每ng只一组选出组内质量最大的老鼠,并将其入队表示晋级,而当前拉搜狐的排名在筛选过程中直接对排名赋值(晋级下一轮的老鼠会获得新的排名),直到只剩一只老鼠,排名记为1。最后输出所有老鼠的排名。

代码如下:

#include<cstdio>
#include<queue>
using namespace std;
const int maxn = 1010;
struct mouse
{
 int weight, r;
}m[maxn];
int main()
{
 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 temp = np, group;
 while(q.size() != 1)
 {
  if(temp % ng == 0) group = temp / ng;
  else group = temp / ng + 1;
  for(int i = 0; i < group; i++)      //对于每一组的数据进行处理 
  {
   int k = q.front();
   for(int j = 0; j < ng; j++)
   {
    if(i * ng + j >= temp) break;           //最后一组的元素个数不够时,退出循环
    int front = q.front();
    if(m[front].weight > m[k].weight) k = front;
    m[front].r = group + 1;
    q.pop();
   }
   q.push(k);
  }
  temp = group;
 }
 m[q.front()].r = 1;
 for(int i = 0; i < np; i++)
 {
  printf("%d", m[i].r);
  if(i != np-1) printf(" ");
 }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值