题目

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 lef 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

题目分析

已知参赛人数N,每组人数M,参赛者体重,起始参赛顺序。每组最重者获胜,每轮有M个人获胜进入下一组进行下一轮比赛

注:因为每轮比赛,有M个人获胜,所以其余失败者排名为M+1,获胜者排名为M

解题思路

  1. 定义queue队列,模拟比赛流程,每场比赛所有参赛者入队,并在最后设置哨兵(哨兵表明本轮比赛结束)
  2. 与层级遍历树结构的差别:本题目中退出队列循环标识为参赛人数为1,而层级遍历树退出队列循环标识为队列中没有节点
  3. 每次循环到下一组第一个参赛者时,将前一组的获胜者设置排名添加到队列最后,准备下一场比赛

Code

  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. struct M {
  5. int w=-1;//weight
  6. int i=-1;//id
  7. int r=-1;//rank
  8. };
  9. int main(int argc,char * argv[]) {
  10. int np,ng,t;
  11. scanf("%d%d",&np,&ng);
  12. M ms[np+1];
  13. for(int i=0; i<np; i++) {
  14. scanf("%d",&ms[i].w);
  15. ms[i].i=i;
  16. }
  17. M em;
  18. ms[np]=em;
  19. queue<int> q;
  20. for(int i=0; i<np; i++) {
  21. scanf("%d",&t);
  22. q.push(t);
  23. }
  24. int group = np%ng==0?np/ng:np/ng+1;
  25. int max=np,cs=np;
  26. q.push(-1);//第一轮最后加一个哨兵
  27. for(int i=1; group>=1&&q.size()>=1; i++) {
  28. int cur = q.front();
  29. if(cur!=-1)ms[cur].r = group+1;
  30. if(cur==-1) { // 若本轮比赛结束
  31. q.pop(); //将哨兵弹出,方便计算下一场比赛人数
  32. ms[max].r = group; //添加上一轮比赛最后一组的获胜者到队列,预备下一场比赛
  33. if(cs==1)break; //如果当前参赛人员只有一人,并且已完成排名,退出
  34. q.push(max); //将本组获胜者加入队列,预备下一轮比赛
  35. cs=q.size(); //下一轮参赛人数
  36. group=(cs%ng==0?cs/ng:cs/ng+1); //重新计算group
  37. q.push(-1); //每一轮最后加一个哨兵
  38. max=np; //max置为哨兵参照,weight=-1
  39. i=0; //重置 指针
  40. continue;
  41. } else if(i!=1&&i%ng==1) { //一组统计完成,当前为下一组第一个参赛者
  42. ms[max].r = group;
  43. q.push(max);//将本组获胜者加入队列,预备下一轮比赛
  44. max=cur; //cur为下一组的第一个参赛者
  45. } else if(ms[max].w<ms[cur].w) { //更新本组获胜者
  46. max=cur;
  47. }
  48. q.pop();
  49. }
  50. for(int i=0; i<np; i++) {
  51. if(i!=0)printf(" ");
  52. printf("%d",ms[i].r);
  53. }
  54. return 0;
  55. }

PAT Advanced 1056 Mice and Rice (25) [queue的⽤法]的更多相关文章

  1. pat 甲级 1056. Mice and Rice (25)

    1056. Mice and Rice (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice an ...

  2. PAT 甲级 1056 Mice and Rice (25 分) (队列,读不懂题,读懂了一遍过)

    1056 Mice and Rice (25 分)   Mice and Rice is the name of a programming contest in which each program ...

  3. 1056 Mice and Rice (25分)队列

    1.27刷题2 Mice and Rice is the name of a programming contest in which each programmer must write a pie ...

  4. 1056. Mice and Rice (25)

    时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and Rice is the name of a pr ...

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

    简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...

  6. PAT甲题题解-1056. Mice and Rice (25)-模拟题

    有n个老鼠,第一行给出n个老鼠的重量,第二行给出他们的顺序.1.每一轮分成若干组,每组m个老鼠,不能整除的多余的作为最后一组.2.每组重量最大的进入下一轮.让你给出每只老鼠最后的排名.很简单,用两个数 ...

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

    题意: 输入两个正整数N和M(<=1000),接着输入两行,每行N个数,第一行为每只老鼠的重量,第二行为每只老鼠出战的顺序.输出它们的名次.(按照出战顺序每M只老鼠分为一组,剩余不足M只为一组, ...

  8. pat1056. Mice and Rice (25)

    1056. Mice and Rice (25) 时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and ...

  9. PAT 1056 Mice and Rice[难][不理解]

    1056 Mice and Rice(25 分) Mice and Rice is the name of a programming contest in which each programmer ...

随机推荐

  1. 104-PHP定义并实例化类

    <?php class ren{ //定义人类 } class mao{ //定义猫类 } echo '实例化一个人类:'; var_dump(new ren()); //实例化人类 echo ...

  2. Java 逆序打印链表

    递归 package cookie; public class PrintListReversal { public void reversalOut(Node head) { if (head != ...

  3. vue 中 {{}} 和 v-text 和 v-html 区别

    data: { message:'<h3>我是一只小小小小鸟!</h3>' }, <div class="" >{{message}}</ ...

  4. mongodb安装到配置问题

    一.所有问题 Xshell 连接不上 报错类型:Could not connect to '192.168.122.1' (port 22): Connection failed.原因:IP地址未生成 ...

  5. Linux Shell编程case语句

    http://blog.csdn.net/dreamtdp/article/details/8048720 case语句适用于需要进行多重分支的应用情况. case分支语句的格式如下: case $变 ...

  6. IntelliJ IDEA ULTIMATE 2019.3 破解注册详细教程【亲测有效,持续更新~】

    ​ 申明:本教程 IntelliJ IDEA 破解补丁.激活码均收集于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除. 注意 本教程适用于 IntelliJ IDEA 所有版本,请放心食用 ...

  7. __getattr__在python2.x与python3.x中的区别及其对属性截取与代理类的影响

    python2.x中的新类型类(New-style class)与python3.x的类一致,均继承object类,而不继承object的类称为经典类(classic class),而对于这两种类,一 ...

  8. Codeforces 1299B/1300D - Aerodynamic

    题目大意: 给定一个图形S,让这个图形任意平移,但是要保证原点(0,0)一直在它的内部或者边上 最后把它能移动到的所有位置进行拼合可以得到一个图形T 问图形S与图形T是否相似 点会按照逆时针顺序给出 ...

  9. 将list等分成n份

    public static <T> Map<Integer, List<T>> spiltList(List<T> list, int num) { M ...

  10. 对python里的装饰器

    内裤可以用来遮羞,但是到了冬天它没法为我们防风御寒,聪明的人们发明了长裤,有了长裤后宝宝再也不冷了,装饰器就像我们这里说的长裤,在不影响内裤作用的前提下,给我们的身子提供了保暖的功效. 再回到我们的主 ...