• 欢迎光临~

# Mice and Rice (25)

### 题目描述

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

### 输入描述:

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

### 输出描述:

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

### 输入例子:

```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`

tip：

1.因为数据量小我是直接正向模拟的，相当于暴力了

2.由于反转map的消耗比较大，所以使用反向迭代器reverse_iterator，相应的把头尾指针改为rbegin和rend

``` 1 #include<bits/stdc++.h>
2 using namespace std;
3 int np,ng;
4 int weight[1005];
5 map<int,vector<int>> rounds;
6 int ranks[1005];
7 int counts=1;
8 queue<int> initial;
9 void match(queue<int> q,int level){ //正向进行比赛，level代表出局轮次
10     if (q.size()==1){ //当队列中只剩下一人时比赛结束
11         rounds[level].push_back(q.front());
12         return;
13     }
14     queue<int> next_turn;
15     while (!q.empty()){
16         int mmax=q.front();
17         for (int i=0;i<ng&&(!q.empty());++i){
18             int temp=q.front();
19             q.pop();
20             if (weight[temp]>=weight[mmax]){
21                 if (temp!=mmax)
22                     rounds[level].push_back(mmax);
23                 mmax=temp;
24             }
25             else
26                 rounds[level].push_back(temp);
27         }
28         next_turn.push(mmax);
29     }
30     return match(next_turn,level+1);
31 }
32 int main(){
33     cin>>np>>ng;
34     memset(weight,0,sizeof(weight));
35     memset(ranks,0,sizeof(ranks));
36     for (int i=0;i<np;++i){
37         cin>>weight[i];
38     }
39     int order;
40     for (int i=0;i<np;++i){
41         cin>>order;
42         initial.push(order);
43     }
44     match(initial,1);
45     //根据每轮淘汰的选手生成名次
46     map<int,vector<int>>::reverse_iterator m_it;
47     vector<int>::iterator v_it;
48     for (m_it=rounds.rbegin();m_it!=rounds.rend();++m_it){
49         vector<int> temp=m_it->second;
50         for (v_it=temp.begin();v_it!=temp.end();++v_it){
51             ranks[*v_it]=counts;
52         }
53         counts+=temp.size();
54     }
55     for (int i=0;i<np;++i){
56         cout<<ranks[i];
57         if (i!=np-1)
58             cout<<" ";
59     }
60 }```