1056 Mice and Rice (25分)
作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:200 ms
内存限制:64 MB
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
题意:
第一行输入总老鼠数量,第二行输入每只老鼠的重量,第三行输入选取第二行的第几只老鼠先参赛。如6 0 8,那就那第6(重19),0(重25),8(重57)只老鼠参赛。比赛采用晋级赛,如例题中,每组规定最多3只老鼠,那么11只老鼠共可以分4组,每组选出一只最重的老鼠(也就是说在第一轮选出了前4名的老鼠),所以其余第一轮参赛老鼠排名都为第5。然后第二轮4只老鼠共可以分,3和1两组(选出了前2),另外两只老鼠就是第三名。最后一轮,共一组,分出第一和第二名。
思路:
利用队列进行轮次比较,直到队列中只剩一只。从队列中出列一组老鼠,将这组老鼠的名次定为组数+1,然后选最重的放置队尾作为下一轮的比较(所以较重的老鼠的名次是会覆盖的,所以不用担心)。
参考代码:
#include <iostream>
#include <queue>
using namespace std;
struct mouse{
int weight,rank,index;
};
int main() {
int np,ng,index;
scanf("%d%d",&np,&ng);
mouse mice[np];
for(int i=0;i<np;i++){
scanf("%d",&mice[i].weight);
mice[i].index=i;
}
queue<mouse> q;
for(int i=0;i<np;i++){
scanf("%d",&index);
q.push(mice[index]);
}
int maxweight=-1;
while(!q.empty()){
int size=q.size();
if(size==1){
mice[q.front().index].rank=1;
break;
}
int part=size%ng==0?size/ng:size/ng+1;
int cnt=0;
mouse maxmouse;
for(int i=0;i<size;i++){
index=q.front().index;
mice[index].rank=part+1;
if(mice[index].weight>maxweight){
maxweight=mice[index].weight;
maxmouse=mice[index];
}
cnt++;
q.pop();
if(cnt==ng||i==size-1){
q.push(maxmouse);
maxweight=-1;
cnt=0;
}
}
}
for(int i=0;i<np;i++){
printf("%d",mice[i].rank);
printf("%s",i<np-1?" ":"\n");
}
return 0;
}
如有错误,欢迎指正