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
1、理解题意
本题重在理解题意,理解输入的数据含义。
题目给出老鼠的总只数n,并且按照k只老鼠进行分组,最后一组不满k只也可以。先将老鼠进行编号0~n-1,给出每只老鼠的体重,每组中体重最大的老鼠晋级。再依次给出进行游戏的老鼠号,比如样例中6排在第一位,表示6号老鼠第一个进行游戏。要求输出每只老鼠游戏结束后的排名。
2、解题思路
(1)每只老鼠在游戏中伴随着两个变量:体重weight--用来比较、排名rank--每次晋级都会形成,所以可以设置mouse结构体变量:
struct mouse{
int weight;
int rank;
}
(2)由于n只老鼠需要按照k进行分组,需要知道到底要分多少组。由于每一轮晋级需要进行分组的老鼠数temp不一样,每一轮需要分组的老鼠数都等于上一轮的组数group,所以初始时可设temp=n,由题意有:
int temp=n,group;
if(temp%k==0) group=temp/k;
else group=temp/k+1;
(3)对于每一组的k(或者不满k,即到达最后一组)只老鼠的处理可以用一个嵌套循环解决:
for(int i=0;i<group;i++){
for(int j=0;j<k;j++{
if(i*k+j>=temp) break;//如果此次处理的老鼠号大于本轮的总老鼠数,退出循环
……
}
}
(4) 由于需要在n只老鼠k个组中选择体重最大的老鼠,在下一轮还要比较此老鼠,不妨设置一个队列,存放游戏的老鼠号,晋级的老鼠号再次进队,直到队列中只有一个老鼠号,那个老鼠就是排名第一的老鼠。由于每组晋级一只老鼠,当前晋级的老鼠数为group,那么剩下的老鼠排名为group+1;
queue<int> q;
int order;
for(int i=0;i<n;i++){
scanf("%d",&order);
q.push(order);
}
int temp=n,group;
while(q.size!=1){
if(temp%k==0) group=temp/k;
else group=temp/k+1;
for(int i=0;i<group;i++){
int k=q.front();
for(int j=0;j<k;j++){
if(i*k+j>=temp) break;//如果没有此句,就会取到在最后一组不足k的情况下取到下一轮的数据,运行出错
int front=q.front();
if(mouse[k].weight<mouse[front].weight){
k=front;
}
mouse[front].rank=group+1;
q.pop();
}
q.push(k);
}
temp=group;
}
mouse[front].rank=1;
本题代码如下:
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=1010;
struct mouse{
int weight;
int rank;
}mouse[maxn];
int main(){
int n,k;
queue<int> q;
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++){
scanf("%d",&mouse[i].weight);
}
int order;
for(int i=0;i<n;i++){
scanf("%d",&order);
q.push(order);
}
int group,temp=n;
while(q.size()!=1){
if(temp%k==0) group=temp/k;
else group=temp/k+1;
for(int i=0;i<group;i++){
int m=q.front();//m存放体重最大的老鼠
for(int j=0;j<k;j++){
if(i*k+j>=temp) break;
int front=q.front();
if(mouse[m].weight<mouse[front].weight){
m=front;
}
mouse[front].rank=group+1;
q.pop();
}
q.push(m);
}
temp=group;
}
mouse[q.front()].rank=1;
for(int i=0;i<n;i++){
printf("%d",mouse[i].rank);
if(i<n-1) printf(" ");
}
return 0;
}
3、知识总结
(1)队列的应用:
#include<queue>
using namespace std;
queue<int> q;
//进队
q.push();
//出队
q.pop();
//取队首
q.front();
//取队尾
q.back;
//队列长度
q.size();
//队列判空
q.empth();
(2)队列最常用的是实现广度遍历搜索。