A1056. Mice and Rice
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 left 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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct node{
int weight;
int rank;
int rank_finall;
}info;
info mice[];
queue<int> qu;
int rk[];
bool cmp(int a, int b){
return mice[a].rank > mice[b].rank;
}
int main(){
int NP, NG, comp, temp;
scanf("%d%d", &NP, &NG);
for(int i = ; i < NP; i++){
scanf("%d", &mice[i].weight);
mice[i].rank = ;
rk[i] = i;
}
for(int i = ; i < NP; i++){
scanf("%d", &temp);
qu.push(temp);
}
comp = NP; //该轮比赛的人数
int rank = ;
while(comp > ){
for(int j = ; j < comp; j += NG){
int maxW = -;
int maxIndex, tempIndex;
for(int k = ; k < NG && j + k < comp; k++){ //防止最后一轮人数不足
tempIndex = qu.front();
mice[tempIndex].rank = rank;
qu.pop();
if(mice[tempIndex].weight > maxW){
maxW = mice[tempIndex].weight;
maxIndex = tempIndex;
}
}
qu.push(maxIndex);
}
rank++;
comp = comp % NG == ? comp / NG : comp / NG + ;
}
int maxPt = qu.front();
mice[maxPt].rank = rank;
sort(rk, rk + NP, cmp);
mice[rk[]].rank_finall = ;
for(int i = ; i < NP; i++){
if(mice[rk[i]].rank == mice[rk[i - ]].rank)
mice[rk[i]].rank_finall = mice[rk[i - ]].rank_finall;
else mice[rk[i]].rank_finall = i + ;
}
printf("%d", mice[].rank_finall);
for(int i = ; i < NP; i++){
printf(" %d", mice[i].rank_finall);
}
cin >> NP;
return ;
}
总结:
1、题意:给出NP只老鼠比体重大小,比法是:每NG个分为一组选出最大的进入下一轮,下一轮依旧每NG各分一组......直到选出最大的。题目要求按照输入的老鼠的顺序输出他们的排名(并列的老鼠名次相同,但占用排位)。另外注意给老鼠分组时,最后一组不满NG个的情况但仍然算一组。
2、queue、stack、vector等存储的是数据的拷贝而不是引用,所以在对struct数据使用这些容器时,最好的办法是将所有的struct存储在data数组中,而将它们的数组下标作为vector等的元素,相当于存储了引用。
3、最好使用STL的queue而非自己写一个队列。
A1056. Mice and Rice的更多相关文章
- PAT甲级——A1056 Mice and Rice
Mice and Rice is the name of a programming contest in which each programmer must write a piece of co ...
- 1056. Mice and Rice (25)
时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and Rice is the name of a pr ...
- PAT1056:Mice and Rice
1056. Mice and Rice (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice an ...
- PAT 1056 Mice and Rice[难][不理解]
1056 Mice and Rice(25 分) Mice and Rice is the name of a programming contest in which each programmer ...
- 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 (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 ...
- 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 ...
- PAT-1056 Mice and Rice (分组决胜问题)
1056. Mice and Rice Mice and Rice is the name of a programming contest in which each programmer must ...
随机推荐
- webpack教程(五)——图片的加载
首先安装的依赖 npm install file-loader --save-devnpm install image-webpack-loader --save-devnpm install url ...
- 有道云笔记导入txt文件的方法
有道云笔记pc版迷之不能导入txt文件 尝试很多方法后发现 通过网页版 有道云 可以直接上传 但是pc版不能查看而移动端可以查看 很迷~
- 阿里云OSS下载pdf文件,并在pdf文件上添加水印
代码: 兵马未动,粮草先行 作者: 传说中的汽水枪 如有错误,请留言指正,欢迎一起探讨. 转载请注明出处. 公司要求从阿里云OSS下载pdf文件并且需要添加水印. 因此这里总结一下. 首先添加了一个F ...
- c++ 中关于一些变量不能声明的问题
j0,j1,jn,y0,y1,yn被c++中某些函数占用了,所以是不能被声明的,今天就遇到了这个问题,结果我在自己写的程序中找了半天都没找到重复申明的y1
- poj1426 Find The Multiple(c语言巧解)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36335 Accepted: 151 ...
- hash函数补分博客
题目要求: 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,60,65,(你的8位学 ...
- varnish页面缓存服务
varnish页面缓存服务 https://www.cnblogs.com/L-dongf/p/9310144.html http://blog.51cto.com/xinzong/1782669 阅 ...
- Maven的课堂笔记1
1 什么是maven? Maven是一个跨平台的项目管理工具,主要用于基于java平台的项目构建,依赖管理. Clean compile test package install run ...
- js选择排序。
<script> , , , , , , , ]; ; j<len; j++ ){ // 假设min为最小值 var minIndex = j; var min = arr[j]; ...
- PHP多进程编之pcntl_fork的实例详解
PHP多进程编之pcntl_fork的实例详解 其实PHP是支持并发的,只是平时很少使用而已.平时使用最多的应该是使用PHP-FMP调度php进程了吧. 但是,PHP的使用并不局限于做Web,我们完全 ...