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 ...
随机推荐
- jenkins 构建后发送钉钉消息通知(插件)
钉钉,越来越多的公司采用,那么我们在持续集成中,也可以直接选择钉钉插件的,在之前的博客中 ,对发送的钉钉消息进行了定制,那样的话会开启一个新的任务, 其实今天呢,我们可以直接安装一个插件就可以发送了, ...
- [T-ARA][남주긴 아까워][给别人可惜了]
歌词来源:http://music.163.com/#/song?id=29343992 作曲 : 二段横踢/Radio Galaxi [作曲 : 二段横踢/Radio Galaxi] 作词 : 二段 ...
- Redis常见问题和解决办法梳理
=============Redis主从复制问题和解决办法 ================= 一.Redis主从复制读写分离问题 1)数据复制的延迟读写分离时,master会异步的将数据复制到sla ...
- win10下安装GLPK
认识GLPK GLPK是一个解决线性规划问题的工具.是GNU计划下一个用于解线性规 划(Linear Programming)的工具包.它可以方便的描述线性规划问题,并给出相应解. 因此在linux系 ...
- 小学四则运算APP 第二阶段冲刺-第三天
团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第二次冲刺阶段时间:11.29~12.09 本次发布的是判断题的部分代码 panduanset.java import com.examp ...
- PAT 1029 旧键盘
https://pintia.cn/problem-sets/994805260223102976/problems/994805292322111488 旧键盘上坏了几个键,于是在敲一段文字的时候, ...
- Docker 执行nginx以及简单进入container
1. docker run -d --name mynginx nginx 运行起来nginx镜像之后 2. docker exec -ti mynginx /bin/bash 能够进入操作界面 发现 ...
- Excel 使用AutoFill提示“类Range的AutoFill方法无效”
今天遇到一个神奇的问题,之前一直使用很好的代码突然报错:“类Range的AutoFill方法无效”,在网上搜索了一番,感觉没有一个与我遇到的情况相同的.debug,查看一下代码,发现程序里,AutoF ...
- HADOOP实战
一.软件版本Centos6.5.VMware 10CDH5.2.0(Hadoop 2.5.0)Hive-0.13 sqoop-1.4.5 二.学完课程之后,您可以:①.一个人搞定企业Hadoop平台搭 ...
- node upgrade bug & node-sass
node upgrade bug & node-sass bug solution rebuild $ npm rebuild node-sass OK