题目描述
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.
翻译:老鼠和大米是一个训练程序,每位程序设计者必须写一段代码来控制老鼠在图中的移动。每个老鼠的目标就是尽可能的吃大米来成为一只胖老鼠。
首先所有的NP个程序设计者随机决定运行顺序。接着每NG个程序设计者会被分到一场比赛。该组最肥的老鼠胜利并进入下一场比赛。该轮所有的失败者排名相同。每NG个胜者会被组队进入下一场比赛,这样循环直到决定出最后的赢家。
为了简便,假设每位老鼠的重量在程序设计者上传他的代码后就固定了。给你每个老鼠的重量和开始的运行顺序,你需要输出每位程序设计者的排名。
INPUT FORMAT
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.
翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括两个正整数NP和NG (<= 1000),分别为程序设计者数量和每一组中的最大老鼠数。第二行包括NP个唯一的非负数Wi (i=0,…NP-1) ,Wi分别对应第i个老鼠的重量。第三行给出初始的运行顺序,为从0到NP-1的序列(假设程序设计者被编号为0到NP-1)。一行内所有数字直接用空格隔开。
OUTPUT FORMAT
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.
翻译:对于每组输入数据,输出一行最终排名。第i个数字代表第i个程序设计者的排名,所有的数字之间必须用空格隔开,并且行的末尾不能有空格。
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
解题思路
这道题就是用队列不断的插入抛出,每当计数器到M或当前轮老鼠选择完毕时进行抛出和压入,当队列中只剩最后一只老鼠时结束循环,具体实现看代码即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,M,d[1010],ans[1010];
struct Data{
int w,v,t;
Data(int weight,int vocation):w(weight),v(vocation){}
bool operator <(const Data &a)const{
return w<a.w;
}
};
queue<Data> q;
priority_queue<Data> p;
int main(){
scanf("%d%d",&N,&M);
for(int i=0;i<N;i++)scanf("%d",&d[i]);
int a;
for(int i=0;i<N;i++)scanf("%d",&a),q.push(Data(d[a],a));
while(1){
int rank=(q.size()%M?q.size()/M+1:q.size()/M)+1;
int ccount=0;
int flag=q.size();
while(!q.empty()){
Data Qtemp=q.front();q.pop();
p.push(Qtemp);
ccount++;
flag--;
if(ccount==M||flag==0){
int Cflag=0;
while(!p.empty()){
Data Ptemp=p.top();p.pop();
if(Cflag==0){
Cflag=1;
Ptemp.t++;
q.push(Ptemp);
}
else ans[Ptemp.v]=rank;
}
ccount=0;
if(flag==0)break;
}
}
if(q.size()==1){
Data Qtemp=q.front();q.pop();
ans[Qtemp.v]=rank-1;
break;
}
}
for(int i=0;i<N;i++){
if(i==0)printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
return 0;
}