PAT 1056 Mice and Rice (25)

Posted mr-stn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 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 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.

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.

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

这道题好绕啊, 读不懂!!
第三行的数字不是第i位选手的比赛顺序,而是比赛选手的序号; 如上面的sample input的第三行, 6 0 8 第一组的三个人的序号是6 0 8, 而不是第一个人的比赛顺序是第六;
思路:建立一个结构保存选手老鼠的重量以及排名;
   用queue来保存晋级选手的序号
如何确定比赛名次? 用第一轮比赛来举例:第一轮有11人, 3人一组,则分为4组, 一定有四个人晋级,那么淘汰的人的名次就在这四人之后,即排名为5; 第二轮有4个人可以分成2组,
可以晋级2人,这一轮比赛淘汰的名次为3
如何确定每一轮的比赛次数? 可以发现queue中的元素个数就是该轮比赛的人数, 每一轮的比赛次数与这一轮的分组数目相等,则比赛次数 rank=q.size()/g + (q.size()%g==0 ? 0 : 1)
   加好后面加的数,是为了解决比赛人数有剩余的情况(g表示每一组的人数)
   如何找到每一组比赛重量最大的老鼠? 采用顺序遍历的方法,每次遍历弹出queue最前面的元素,即比赛选手的序号, 遍历每一组中的每一个选手,就能找出最终选手的序号, 并且在该过程中更新选手排名
把每一轮每一组比赛的优胜者的序号添加到queue<int> temp中,改队列中记录的就是下一轮比赛的选手序号, 一轮比赛完成后, 把temp的值复制到queue中

注意点:注意循环的中值条件,应该是q.size()!=1, 而非去,q.size()!=0; 否则会陷入死循环
    对于在循环中长度会变化的结构来说,不能用i<q.size() 来判断,这样会导致错误,应该在循环前取得q的长度,再做判断
 1 #include<iostream>
 2 #include<queue>
 3 #include<algorithm>
 4 using namespace std;
 5 struct node{ int weight, rank;};
 6 
 7 int main(){
 8   int n, g, i, j, index, maxid;
 9   cin>>n>>g;
10   queue<int> q;
11   vector<node> v(n);
12   for(i=0; i<n; i++) cin>>v[i].weight;
13   for(i=0; i<n; i++){ cin>>index; q.push(index);}
14   
15   while(q.size()!=1){
16     int rank = q.size()/g + (q.size()%g==0 ? 0 : 1);
17     queue<int> temp;
18     for(i=0; i<rank; i++){
19       maxid=q.front();
20       int len=q.size();
21       for(j=0; j<g && j<len; j++){
22         int idxx=q.front();
23         if(v[maxid].weight < v[idxx].weight) maxid=idxx;
24         v[idxx].rank=rank+1;
25         q.pop();
26       }
27       temp.push(maxid);
28     }
29     q=temp;
30   }
31   
32   v[maxid].rank=1;
33   cout<<v[0].rank;
34   for(i=1; i<n; i++) cout<<" "<<v[i].rank;
35   return 0;
36 }

 















以上是关于PAT 1056 Mice and Rice (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT甲级1056 Mice and Rice (25 分)

STL_A1056 Mice and Rice (25 分)

1056 Mice and Rice

1056 Mice and Rice (25 分)难度: 一般 / 知识点: 模拟

A1056Mice and Rice (25分)

数据结构专题——队列的应用 A1056.Mice and Rice ( 25)