PAT 1056-Mice and Rice (25)

来源:互联网 发布:电脑录视频软件 编辑:程序博客网 时间:2024/06/10 14:02
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard 

题目描述

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 325 18 0 46 37 3 19 22 57 56 106 0 8 7 10 5 9 1 4 2 3

输出例子:

5 5 5 2 5 5 5 3 1 3 5

解题思路


这道题真的是。。。严重怀疑出题人的语言表达能力有问题。所以尽力读懂题目吧,反正我是根据给出的输入例子艰难的读懂了题目。读懂题目之后,这道题目就相当简单了,甚至可以说不涉及任何算法。

1. 题目的第一行输入是Np和Ng,分别代表参加比赛的总人数和小组赛的规模。第二行为参赛人员的mouse weight,这里可以看作积分,积分越高就越牛逼。注意这里的积分是没有重复的,减小了题目的难度。第三行输入则是比赛顺序队列,比如题目的输入例子,0号队员排第六,1号队员排第零,二号队员排第八......

2. 按照题目要球,按照比赛队列,没Ng个队员一组比赛,积分最高者晋级下一轮,其他人则排名相同。若该轮比赛人数不是Ng的整数倍,比如8人每3人一组,那么剩下的两人则一组进行比赛。

3. 最重要的就是排名机制了,一轮比赛未晋级的队员的排名一样,都是晋级人数+1,所以排名为该轮比赛总人数/Ng+1,若比赛总人数%Ng不等于0,则排名还需要加1,仔细想想就可以明白。

#include<iostream>#include<vector>using namespace std;int main(){int P, G;vector<int>order, rank, weight;cin >> P >> G;order.resize(P);rank.resize(P);weight.resize(P);for (int i = 0; i < P; i++)cin >> weight[i];for (int i = 0; i < P; i++)cin >> order[i];while (order.size() > 1){vector<int>next;next.clear();int currank = order.size() / G + 1;if (order.size() % G)currank++;for (int i = 0; i < order.size() / G; i++){int max = -1, index = 0;for (int j = 0; j < G; j++){rank[order[i*G + j]] = currank;if (weight[order[i*G + j]] > max){max = weight[order[i*G + j]];index = order[i*G + j];}}next.push_back(index);}if (order.size() % G){int max = -1, index = 0;for (int i =( order.size() / G)*G; i < order.size(); i++){rank[order[i]] = currank;if (weight[order[i]] > max){max = weight[order[i]];index = order[i];}}next.push_back(index);}order = next;}rank[order[0]] = 1;for (int i = 0; i < P-1; i++)cout << rank[i] << " ";cout << rank[P - 1];return 0;}



0 0
原创粉丝点击