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. C语言----数据类型(基础篇一)

    C语言的入门程序模板 #include <stdio.h> /*使用或者包含系统里面的程序*/ main() /*程序入口点*/ { /*起点*/ +; /*叫计算机执行的指令*/ } / ...

  2. TRIO-basic指令--MOVEMODIFY

    Syntax: MOVEMODIFY(position) Parameters: position: Absolute position for the current move to complet ...

  3. U盘、移动硬盘等弹出 “文件或目录损坏且无法读取” 实测解决办法

    U盘跟其他的机器一样,使用久了难免会出故障,比如常见的弹出一个文件或目录损坏且无法读取的对话框,吓你一跳,整个U盘都损坏的意思,那里面的资料怎么办呢,所以很多人很着急,其实遇到这种情况一般都是之前使用 ...

  4. if...else 小练习

    # 需求:猜年龄,可以让用户最多猜三次 age = 60 for i in range(3): guess = int(input("Input Age: ")) if guess ...

  5. 个人作业-Week1(新增详细说明)

    快速看完整部教材,列出你仍然不懂的5到10个问题,发布在你的个人博客上. 如何提出有价值的问题? 请看这个文章:http://www.cnblogs.com/rocedu/p/5167941.html ...

  6. 个人博客作业_week14

    M1/M2阶段总结 我在M1阶段负责后端代码的开发,以及协助PM,在M2阶段负责PM,在为期将近一学期的团队软件开发过程中,我深刻体会到了团队协作的重要性,以及合理分配任务的重要性,没有一个好的时间规 ...

  7. 【Beta版本发布】爬虫队长装备全面更新!

    一.Beta阶段目标回顾 1.为了解决Alpha阶段线程异常泛滥的问题,我们需要一个线程池. 2.为了爬取得到的文件正确可用,我们需要一个异常清理器. 3.为了不间断爬取,管理员不必频繁运行程序点,我 ...

  8. 必应语音API(Bing text to speech API)

    前言 Link : Microsoft Speech API overview 通过这个链接,大致了解Bing speech API的语音识别和语音合成两部分, 这次是需要用到TTS,所以就直接看TT ...

  9. 业务-----添加Service常用逻辑

    1.参数不能为空 /** * 添加人员时判断是否字段全部传值 * @param request * @return */ private Boolean checkClientByCols(Clien ...

  10. [转载]Memory Limits for Windows and Windows Server Releases

    Memory Limits for Windows and Windows Server Releases This topic describes the memory limits for sup ...