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 }

原文地址:https://www.cnblogs.com/mr-stn/p/9217204.html

时间: 2024-10-08 05:10:27

PAT 1056 Mice and Rice (25)的相关文章

PAT (Advanced Level) 1056. Mice and Rice (25)

简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace std

【PAT甲级】1056 Mice and Rice (25 分)

题意: 输入两个正整数N和M(<=1000),接着输入两行,每行N个数,第一行为每只老鼠的重量,第二行为每只老鼠出战的顺序.输出它们的名次.(按照出战顺序每M只老鼠分为一组,剩余不足M只为一组,每组只能有一个胜者,其他老鼠排名均为这一轮胜者数量+1) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int n,m;int a[1007],b[1007];int num;int ans[1

PAT 1056 Mice and Rice

#include <cstdio> #include <climits> #include <cstdlib> #include <vector> #include <list> using namespace std; list<int>::iterator group_pick(list<int> &player, list<int>::iterator &cur, int group_si

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

#include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; const int maxn = 1010; struct mouse{ int weight;//质量 int R;//排名 }mouse[maxn]; int main(){ int np,ng,order; scanf("%d%d",&np,&ng); for(int

1056 Mice and Rice

题意:略 思路:利用queue来模拟一轮一轮的比赛.自己第一遍做的时候完全没有用queue做的意识,代码写的贼烦最后还只得了17分,非常郁闷.通过本题反映出对queue的应用场景季度不熟悉,STL里面用的最少的就是队列了.另外还有一点,在当前这一轮被淘汰的老鼠排名均为当前组数+1,这一点我也没看出来,自己做的时候拐了18个弯去实现这一点,真是惭愧! 代码: #include <cstdio> #include <queue> using namespace std; struct

PAT 1074. Reversing Linked List (25)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.  For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6. Input Specifica

PAT 1040. Longest Symmetric String (25)

1040. Longest Symmetric String (25) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", the longest symmetric sub-string is "s PAT&TAP s", hence

PAT乙级1085-----PAT单位排行 (25分)

1085 PAT单位排行 (25分) 输入样例: 10 A57908 85 Au B57908 54 LanX A37487 60 au T28374 67 CMU T32486 24 hypu A66734 92 cmu B76378 71 AU A47780 45 lanx A72809 100 pku A03274 45 hypu 输出样例: 5 1 cmu 192 2 1 au 192 3 3 pku 100 1 4 hypu 81 2 4 lanx 81 2 思路:(struct sc

PAT乙级 1065. 单身狗(25) by Python

1065. 单身狗(25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数:随后N行,每行给出一对夫妻/伴侣--为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔:之后给出一