1056 Mice and Rice

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​P​​ programmers. Then every N​G​​ 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​G​​ 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​P​​ and N​G​​ (≤), the number of programmers and the maximum number of mice in a group, respectively. If there are less than N​G​​ 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​P​​ distinct non-negative numbers W​i​​ (,) where each W​i​​ is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0 (assume that the programmers are numbered from 0 to N​P​​−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

 

题意:

  给出一个数组代表老鼠的重量,然后将这些老鼠按照给出的顺序,每Ng各一组,在每组中选出一个体重最重的,参与下一轮的比较,其余的老鼠淘汰且排名相同。输出每一个老鼠按照原来顺序的排名。

思路:

  这道题考查的是queue的应用,做的时候应该理清楚里面的关系,然后在写代码。我们可以用每一轮选拔晋级的选手的个数,来确定这轮选拔中没有晋级的选手的排名,即没有晋级的选手的排名 = 晋级的选手的个数 + 1. 我们用一个struct来存储每一个老鼠的信息,主要包括:体重,本来的顺序和参加比赛时的出场顺序。当队列中只有一个元素的时候代表冠军已经产生。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 struct Mouse {
 6     int weight;
 7     int index0;
 8     int index;
 9     int rank;
10 };
11 
12 bool cmp(Mouse a, Mouse b) { return a.index0 < b.index0; }
13 
14 int main() {
15     int Np, Ng, temp;
16     cin >> Np >> Ng;
17     vector<int> weight(Np);
18     vector<Mouse> mouses(Np);
19     queue<Mouse> que;
20     for (int i = 0; i < Np; ++i) cin >> weight[i];
21     for (int i = 0; i < Np; ++i) {
22         cin >> temp;
23         mouses[i] = {weight[temp], temp, i, 0};
24         que.push(mouses[i]);
25     }
26     while (!que.empty()) {
27         int size = que.size();
28         if (size == 1) {
29             Mouse tempMouse = que.front();
30             mouses[tempMouse.index].rank = 1;
31             break;
32         }
33         int group = size / Ng;
34         if (size % Ng != 0) group += 1;
35         int cnt = 0, maxn = -1;
36         Mouse maxWeightMouse;
37         for (int i = 0; i < size; ++i) {
38             Mouse tempMouse = que.front();
39             mouses[tempMouse.index].rank = group + 1;
40             que.pop();
41             cnt++;
42             if (tempMouse.weight > maxn) {
43                 maxn = tempMouse.weight;
44                 maxWeightMouse = tempMouse;
45             }
46             if (cnt == Ng || i == size - 1) {
47                 cnt = 0;
48                 maxn = -1;
49                 que.push(maxWeightMouse);
50             }
51         }
52     }
53     sort(mouses.begin(), mouses.end(), cmp);
54     for (int i = 0; i < Np; ++i) {
55         if (i == 0)
56             cout << mouses[i].rank;
57         else
58             cout << " " << mouses[i].rank;
59     }
60     return 0;
61 }

 

posted @ 2020-05-07 16:29  Veritas_des_Liberty  阅读(174)  评论(0编辑  收藏  举报