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 N programmers. Then every N 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 N 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: N and N (≤1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than N 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 N distinct non-negative numbers W (i=0,⋯,N−1) where each W is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,⋯,N−1 (assume that the programmers are numbered from 0 to N−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
思路
题意大概是给一组老鼠,分成group
组比赛,每组选出最重的那一个晋级。
第一轮是组别是【6号、 0号、 8号】,【7号、10号、5号】,【9号、1号、4号】,【2号、3号】。晋级情况如下:
参赛次序 | 6 | 0 | 8 | 7 | 10 | 5 | 9 | 1 | 4 | 2 | 3 |
---|---|---|---|---|---|---|---|---|---|---|---|
第一轮 | 19 | 25 | 57 | 22 | 10 | 3 | 56 | 18 | 37 | 0 | 46 |
第二轮 | 57 | 22 | 56 | 46 | |||||||
第三轮 | 57 | 46 | |||||||||
第四轮 | 57 |
排名根据晋级的人数决定,比如有 n 个人晋级,那么这一批未晋级的人的排名就为 n+1 。
代码
#include <iostream>
#include <vector>
using namespace std;
int weight[1001] = {0x0};
int Rank[1001] = {0x0};
int main() {
int Np, Ng;
cin >> Np >> Ng;
// Read player
vector<int> player;
for(int i = 0; i < Np; i++)
cin >> weight[i];
for(int i = 0; i < Np; i++) {
int tmp(0);
cin >> tmp;
player.push_back(tmp);
}
int last_standings = Np;
while( last_standings > 1 ) {
vector<int> nextRound;
// Walk every group
for(int i = 0; i < last_standings; i += Ng) {
// Range of group: [i, i + Ng - 1] or [i, last_standings]
int winner = i;
for(int j = i + 1; j < last_standings && j <= i + Ng - 1; j++) {
if( weight[ player[winner] ] < weight[ player[j] ] )
winner = j;
}
nextRound.push_back( player[winner] );
// Walk the play who doesn't win, and give them a rank
for(int j = i; j < last_standings && j <= i + Ng - 1; j++) {
if( j != winner )
Rank[ player[j] ] = last_standings/Ng + (last_standings%Ng ? 1:0) + 1;
}
}
player = nextRound;
last_standings = last_standings/Ng + (last_standings%Ng ? 1:0);
}
Rank[ player[0] ] = 1;
// Display Result
for(int i = 0; i < Np; i++) {
if(i) cout << " ";
cout << Rank[i];
}
return 0;
}