PAT Advanced 1056 Mice and Rice (25) [queue的⽤法]
题目
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
解题思路
- 定义queue队列,模拟比赛流程,每场比赛所有参赛者入队,并在最后设置哨兵(哨兵表明本轮比赛结束)
- 与层级遍历树结构的差别:本题目中退出队列循环标识为参赛人数为1,而层级遍历树退出队列循环标识为队列中没有节点
- 每次循环到下一组第一个参赛者时,将前一组的获胜者设置排名添加到队列最后,准备下一场比赛
Code
#include <iostream>
#include <queue>
using namespace std;
struct M {
int w=-1;//weight
int i=-1;//id
int r=-1;//rank
};
int main(int argc,char * argv[]) {
int np,ng,t;
scanf("%d%d",&np,&ng);
M ms[np+1];
for(int i=0; i<np; i++) {
scanf("%d",&ms[i].w);
ms[i].i=i;
}
M em;
ms[np]=em;
queue<int> q;
for(int i=0; i<np; i++) {
scanf("%d",&t);
q.push(t);
}
int group = np%ng==0?np/ng:np/ng+1;
int max=np,cs=np;
q.push(-1);//第一轮最后加一个哨兵
for(int i=1; group>=1&&q.size()>=1; i++) {
int cur = q.front();
if(cur!=-1)ms[cur].r = group+1;
if(cur==-1) { // 若本轮比赛结束
q.pop(); //将哨兵弹出,方便计算下一场比赛人数
ms[max].r = group; //添加上一轮比赛最后一组的获胜者到队列,预备下一场比赛
if(cs==1)break; //如果当前参赛人员只有一人,并且已完成排名,退出
q.push(max); //将本组获胜者加入队列,预备下一轮比赛
cs=q.size(); //下一轮参赛人数
group=(cs%ng==0?cs/ng:cs/ng+1); //重新计算group
q.push(-1); //每一轮最后加一个哨兵
max=np; //max置为哨兵参照,weight=-1
i=0; //重置 指针
continue;
} else if(i!=1&&i%ng==1) { //一组统计完成,当前为下一组第一个参赛者
ms[max].r = group;
q.push(max);//将本组获胜者加入队列,预备下一轮比赛
max=cur; //cur为下一组的第一个参赛者
} else if(ms[max].w<ms[cur].w) { //更新本组获胜者
max=cur;
}
q.pop();
}
for(int i=0; i<np; i++) {
if(i!=0)printf(" ");
printf("%d",ms[i].r);
}
return 0;
}
PAT Advanced 1056 Mice and Rice (25) [queue的⽤法]的更多相关文章
- pat 甲级 1056. Mice and Rice (25)
1056. Mice and Rice (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice an ...
- 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 ...
- 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 ...
- 1056. Mice and Rice (25)
时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and Rice is the name of a pr ...
- PAT (Advanced Level) 1056. Mice and Rice (25)
简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...
- PAT甲题题解-1056. Mice and Rice (25)-模拟题
有n个老鼠,第一行给出n个老鼠的重量,第二行给出他们的顺序.1.每一轮分成若干组,每组m个老鼠,不能整除的多余的作为最后一组.2.每组重量最大的进入下一轮.让你给出每只老鼠最后的排名.很简单,用两个数 ...
- 【PAT甲级】1056 Mice and Rice (25 分)
题意: 输入两个正整数N和M(<=1000),接着输入两行,每行N个数,第一行为每只老鼠的重量,第二行为每只老鼠出战的顺序.输出它们的名次.(按照出战顺序每M只老鼠分为一组,剩余不足M只为一组, ...
- pat1056. Mice and Rice (25)
1056. Mice and Rice (25) 时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and ...
- PAT 1056 Mice and Rice[难][不理解]
1056 Mice and Rice(25 分) Mice and Rice is the name of a programming contest in which each programmer ...
随机推荐
- 二十一、JavaScript之访问对象属性
一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...
- Lombok认知
Lombok的简介 Lombok是一款Java开发插件,公司项目到处使用,整体效果很棒,代码更干净.Java开发人员可以节省出重复构建,诸如hashCode和equals这样的方法以及各种业务对象模型 ...
- Swift Json解析与model互转
Json的解码与编码操作,这里使用swift自带的类JSONDecoder 和 JSONEncoder 1.基础处理 如果你的 JSON 数据结构和你使用的 Model 对象结构一致的话,那么解析过程 ...
- 实验吧-密码学-变异凯撒(ascii码规律运算)
密文是:afZ_r9VYfScOeO_UL^RWUc 刚开始很迷,不知道往什么方向,到最后才发现,原来和ASCII有关. 将flag{的ASCII码和密文的ASCII码对比: 97 102 90 95 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-lock
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- Django 数据库配置
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'books', #你的数据库名称 'USER': ' ...
- C语言拾遗——inttypes.h
今天偶然间看到这个头文件inttypes,好奇有什么用,去找度娘玩了一波,发现这头文件挺有意思的. 这个头文件适配于C99标准,它提供整数输入的各种进制转换的宏,这是在Ubuntu上扣下来的代码(wi ...
- EUI库 - 9 - 数据集合 - 列表
List 和DataGroup的区别 1 选中一项 会触发 eui.ItemEvent.ITEM_TAP 事件, 2 有选中项的概念,可以设置 List 里的默认选中项 selectedIn ...
- 2018出炉50道iOS面试题
基础: 1.如何令自己所写的对象具有拷贝功能? 若想令自己所写的对象具有拷贝功能,则需实现 NSCopying 协议.如果自定义的对象分为可变版本与不可变版本,那么就要同时实现 NSCopying与 ...
- java初学小项目-酒店客房管理系统
最近初次接触JAVA,感觉之前学的C语言很有用,跟着视频做了一个小项目-酒店客房管理系统 /* 酒店客房管理系统 */ import java.util.Scanner;//通过键盘来输入命令需要的引 ...