1056 Mice and Rice(25)-PAT甲级模拟
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.
Input Specification:
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.
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
【题目大意】
程序设计竞赛排名,程序员设计老鼠吃东西的路线,吃到最多的赢,已给定老鼠的吃到最后的重量结果,求排名。np为老鼠的数量,ng为每组最多g个老鼠。先给出np个老鼠的重量,再给出老鼠的初始顺序(第i名的老鼠是第j号,j从0开始)。每ng个老鼠分为一组,对于每组老鼠,选出最重的那个,晋级下一轮比赛,然后依次再以np个老鼠一组分类,然后选出重量最大的。。。直到只剩下一只老鼠,排名为1.输出为老鼠的排名,这个排名是按照原输入老鼠的顺序输出的
【任务】
- 计算每个人的比赛排名
【难点】
- 【题目理解】一开始看例子不是很懂到底规则是怎样的,怎么推算都不能得到案例的比赛结果;后来才知道第二行给的老鼠的重量就标志着选手的初始序号(1…N),最后一行是打乱顺序后的比赛分组的序号
- 如何利用数据结构进行模拟比赛的晋级
解题思路
- 【数据结构】利用直接的比赛模拟的思维,vector的二维数据结构来模拟晋级,q[0]是第一轮的比赛,放置所有的老鼠,q[1]是第二轮晋级一轮之后的比赛,这样子数据结构完全不用担心内存情况,因为1000个老鼠分三个为一组,只需要3^7>1000,只需要7次
- 【如何进行分组】i % NG == NG - 1,以3个为一组为例:012 345 678,但这样需要考虑最后一组NG个的情况或者不满NG个选手的轮次的情况,因为当前轮次的数量是NG的整数倍时,会都考虑在内,参赛选手都进行晋级。而不是整数倍时,会有最后一组的参赛选手没被考虑,所以需要在for循环之后判断 q[round].size() % NG != 0
- 【晋级的考虑】每遍历完一组之后,选出最终的那个老鼠,老鼠的数据结构中jinji默认是false,首先将晋级的老鼠放进下一轮 q[round+1],然后再将当前轮次的老鼠的jinji改为true,这样在将当前未晋级的轮次的老鼠放入最终的结果vector r的时候,不会将晋级的老鼠放进去;同时考虑下一轮的老鼠的时候仍然是false
- 【未晋级的排名】晋级了N个,那么未晋级的所有人排名都是N+1,在while循环最后遍历当前轮次的结果时,进行赋值
【正在刷题,记录日常,代码未简化,keep learing】
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct mice {
int id, weight, rank;
bool jinji = false;
};
bool cmp(mice m1, mice m2) {
return m1.id < m2.id;
}
int NP, NG, index;
vector<mice>v;
vector<mice>q[1000];
vector<mice>r;
int main() {
scanf_s("%d%d", &NP, &NG);
v.resize(NP);
for (int i = 0; i < NP; i++) {
scanf_s("%d", &v[i].weight);
v[i].id = i;
}
for (int i = 0; i < NP; i++) {
scanf_s("%d", &index);
q[0].push_back(v[index]);
}
int round = 0;
while (q[round].size() > 1) {
int groupNum = ((q[round].size() % NG == 0) ? q[round].size() / NG : q[round].size() / NG + 1);
int indexMax = 0, groupMax = 0;
for (int i = 0; i < q[round].size(); i++) {
if (q[round][i].weight > groupMax) {
groupMax = q[round][i].weight;
indexMax = i;
}
if (i % NG == NG - 1) {
q[round + 1].push_back(q[round][indexMax]);
q[round][indexMax].jinji = true;
groupMax = 0;
}
}
if (q[round].size() % NG != 0) {
q[round + 1].push_back(q[round][indexMax]);
q[round][indexMax].jinji = true;
}
for (int i = 0; i < q[round].size(); i++) {
if (q[round][i].jinji == false) {
q[round][i].rank = groupNum + 1;
r.push_back(q[round][i]);
}
}
round++;
}
q[round][0].rank = 1;
r.push_back(q[round][0]);
sort(r.begin(), r.end(), cmp);
printf("%d", r[0].rank);
for (int i = 1; i < r.size(); i++) {
printf(" %d", r[i].rank);
}
return 0;
}