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的更多相关文章

  1. 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 ...

  2. 1056. Mice and Rice (25)

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

  3. PAT1056:Mice and Rice

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

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

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

  5. pat1056. Mice and Rice (25)

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

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

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

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

  8. 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 ...

  9. PAT-1056 Mice and Rice (分组决胜问题)

    1056. Mice and Rice Mice and Rice is the name of a programming contest in which each programmer must ...

随机推荐

  1. libmysqlclient.so.16: cannot open shared object file: No such file or directory

    编译安装的mysql5.6.39,安装目录是/usr/local/mysql,启用程序时报错:libmysqlclient.so.16: cannot open shared object file: ...

  2. Tomcat通过Memcached实现session共享的完整部署记录

    对于web应用集群的技术实现而言,最大的难点就是:如何能在集群中的多个节点之间保持数据的一致性,会话(Session)信息是这些数据中最重要的一块.要实现这一点, 大体上有两种方式:一种是把所有Ses ...

  3. mariadb第二章-增删改

    MariaDB 数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的 常用的数据类型 整数:int, bit 小数:de ...

  4. package-lock.json的作用

    其实用一句话来概括很简单,就是锁定安装时的包的版本号,并且需要上传到git,以保证其他人在npm install时大家的依赖能保证一致. 引用知乎@周载南的回答 根据官方文档,这个package-lo ...

  5. 牛客训练赛25-A-最长区间

    https://www.nowcoder.com/acm/contest/158#question 这题问最长的严格连续递增序列的最长长度是多少? 最开始感觉这道题不可做,因为有1e5个点,还有1e5 ...

  6. Flask、Celery、RabbitMQ学习计划

    Flask (9.16-9.23) 相关组件了解 (9.16-17) WSGI:Werkzeug 数据库:SQLAlchemy   *重点查看 urls和视图 (9.18-19) session和co ...

  7. 五子棋游戏SRS文档

        SRS技术文档,包括对SRS的解释说明.SRS描述规范.软件需求规格说明书(SRS,Software Requirement Specification)是为了软件开发系统而编写的,主要用来描 ...

  8. HDU 2087 剪花布条 (字符串哈希)

    http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图 ...

  9. HDU 2029 Palindromes _easy version

    http://acm.hdu.edu.cn/showproblem.php?pid=2029 Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“ ...

  10. Difference between prop and attr in different version of jquery

    jQuery <1.9$('#inputId').attr('readonly', true); jQuery 1.9+$('#inputId').prop('readonly', true); ...