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:

  1. 11 3
  2. 25 18 0 46 37 3 19 22 57 56 10
  3. 6 0 8 7 10 5 9 1 4 2 3

Sample Output:

  1. 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


代码

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int weight[1001] = {0x0};
  5. int Rank[1001] = {0x0};
  6. int main() {
  7. int Np, Ng;
  8. cin >> Np >> Ng;
  9. // Read player
  10. vector<int> player;
  11. for(int i = 0; i < Np; i++)
  12. cin >> weight[i];
  13. for(int i = 0; i < Np; i++) {
  14. int tmp(0);
  15. cin >> tmp;
  16. player.push_back(tmp);
  17. }
  18. int last_standings = Np;
  19. while( last_standings > 1 ) {
  20. vector<int> nextRound;
  21. // Walk every group
  22. for(int i = 0; i < last_standings; i += Ng) {
  23. // Range of group: [i, i + Ng - 1] or [i, last_standings]
  24. int winner = i;
  25. for(int j = i + 1; j < last_standings && j <= i + Ng - 1; j++) {
  26. if( weight[ player[winner] ] < weight[ player[j] ] )
  27. winner = j;
  28. }
  29. nextRound.push_back( player[winner] );
  30. // Walk the play who doesn't win, and give them a rank
  31. for(int j = i; j < last_standings && j <= i + Ng - 1; j++) {
  32. if( j != winner )
  33. Rank[ player[j] ] = last_standings/Ng + (last_standings%Ng ? 1:0) + 1;
  34. }
  35. }
  36. player = nextRound;
  37. last_standings = last_standings/Ng + (last_standings%Ng ? 1:0);
  38. }
  39. Rank[ player[0] ] = 1;
  40. // Display Result
  41. for(int i = 0; i < Np; i++) {
  42. if(i) cout << " ";
  43. cout << Rank[i];
  44. }
  45. return 0;
  46. }