题目描述
Mice and Rice is the name of a programming contest in which each programmermust write a piece of code to control the movements of a mouse in a givenmap. The goal of each mouse is to eat asmuch 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 amatch. The fattest mouse in a group winsand enters the next turn. All the losersin this turn are ranked the same. EveryNG winners are then grouped in the nextmatch until a final winner is determined.
For the sake of simplicity, assume that the weight of each mouse is fixed oncethe programmer submits his/her code. Given the weights of all the mice and the initial playing order, you aresupposed to output the ranks for the programmers.
输入描述:
Each input file contains one test case. For each case, the first line contains 2positive integers: NP and NG (<= 1000), the number of programmers and the maximum numberof mice in a group, respectively. Ifthere are less than NG mice at the end of the player's list, then all the mice leftwill be put into the last group. Thesecond line contains NP distinct non-negative numbers Wi (i=0,...NP-1) where each Wi is the weight of the i-th mouserespectively. The third line gives theinitial playing order which is a permutation of 0,...NP-1 (assume that the programmers arenumbered from 0 to NP-1). All the numbers in aline are separated by a space.
输出描述:
For each test case, print the final ranks in a line. The i-th number is the rank of the i-thprogrammer, and all the numbers must be separated by a space, with no extraspace at the end of the line.
输入例子:
11 3
25 18 0 46 37 3 19 22 57 56 10
6 0 8 7 10 5 9 1 4 2 3
输出例子:
5 5 5 2 5 5 5 3 1 3 5
首先,这一题读懂题目的难度比代码的难度大多了,假如我在正式考试的时候遇到这道题,肯定就是0分了,因为我半个小时还没读懂题目。。。。
首先这一题第二行给的每个老鼠的体重,而这里并没有说老鼠和程序员是怎样对应的,我原来以为是按照程序员上场的时间先后老鼠重量和程序员一一对应,后来发现老鼠重量直接就是和程序员编号一一对应的。。。。
然后是第三行,题目中说第三行描述了程序员出场的顺序,题目中并没有说是怎样描述的,我原来以为是第i个数据表示第i个程序员第几个出场,结果第I个数据表示的是第i个出场的是第几个程序员,这里的数据和程序员对应的方式和第一组数据不一样,我也是醉了
知道题目意思之后就简单了,使用一个队列记录当前参与比赛的选手,然后每隔Ng个取一组,取里面最重的老鼠作为这一组的胜利者,剩下的程序员的rank为当前回合胜利者的数量加一
#include <iostream.h>
#include <queue>
using namespace std;
#define MAX INT_MAX
int miceFat[1003];
int theRanks[1003];
int main()
{
int Np,Ng;
int a;
int count;
int theWinNum;
cin>>Np>>Ng;
queue<int> theProgramer;
for(int i=0;i<Np;i++)
{
cin>>miceFat[i];
}
for(int i=0;i<Np;i++)
{
cin>>a;
theProgramer.push(a);
}
count = Np;
int tmp,t;
int theMax,theMaxIndex;
vector<int> theStore;
while(1)
{
if(count == 1)
{
tmp = theProgramer.front();
theRanks[tmp] =1;
break;
}
if(count %Ng==0) theWinNum = count/Ng;
else theWinNum = count/Ng+1;
t=0;
theMax = -1;
for(int j=0;j<count;j++)
{
tmp =theProgramer.front();theProgramer.pop();
theStore.push_back(tmp);
if(miceFat[tmp]>theMax)
{
theMax=miceFat[tmp];
theMaxIndex=tmp;
}
t++;
if(t==Ng||j==count-1)
{
t=0;
theMax= -1;
for(int k = 0;k<theStore.size();k++)
{
if(theStore[k] == theMaxIndex)
theProgramer.push(theStore[k]);
else theRanks[theStore[k]] = theWinNum+1;
}
theStore.clear();
}
}
count = theWinNum;
}
for(int i=0;i<Np;i++)
{
cout<<theRanks[i];
if(i!=Np-1) cout<<" ";
}
return 0;
}