您的位置:首页 > 其它

1056. Mice and Rice (25)

2015-09-04 21:03 357 查看
题目链接:http://www.patest.cn/contests/pat-a-practise/1056

题目:

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 NGwinners 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 Wiis 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


分析:

题目很难理解:输入第三行是指第6号参赛者是第1个,不是第1号第6个比;第二点是,当遇到少于NG人时,比如两人,那么就晋级1人淘汰1人;

*用队列来做,每次判断出队的level,注意红色部分是要先判断!Q.empty()再判断level,否则当Q为空时会出错。

AC代码:

#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string>
using namespace std;
struct Node{
 int weight;
 int order;
 int level;
 int mc;
 Node(){
  level = 0;
  mc = 0;
 }
};
queue<Node>Q;
bool cmp(const Node A,const Node B){
 return A.level > B.level;
}
bool cmp2(const Node A, const Node B){
 return A.order < B.order;
}
int main(void){
 freopen("F://Temp/input.txt", "r", stdin);
 int Np, NG;
 scanf("%d%d", &Np, &NG);
 Node *All = new Node[Np];
 for (int i = 0; i < Np; i++){
  scanf("%d", &(All + i)->weight);
  (All + i)->order = i;
 }
 for (int i = 0; i < Np; i++){
  int tmp;
  scanf("%d", &tmp);
  Q.push(*(All + tmp));
 }
 int curLevel;
 while (!Q.empty()){
  curLevel = Q.front().level;
  int max = Q.front().weight;
  int count = 0;
  Node tmp = Q.front();
  while (count < NG && !Q.empty() &&Q.front().level == curLevel){//*point
   if (Q.front().weight > max){
    max = Q.front().weight;
    tmp = Q.front();
   }
   count++;
   Q.pop();
  }
  tmp.level++;
  (All + tmp.order)->level++;
  if (Q.empty())break;
  Q.push(tmp);
 }//while
 sort(All, All + Np, cmp);
 int idx = 1;
 int sum = 0;
 curLevel++;
 for (int i = 0; i < Np; i++){
  if ((All + i)->level == curLevel){
   (All + i)->mc = idx;
   sum++;
  }
  else{
   idx += sum;
   curLevel--;
   (All + i)->mc = idx;
   sum = 1;
  }
 }
 sort(All, All + Np, cmp2);
 for (int i = 0; i < Np; i++){
  if (i == Np - 1)printf("%d\n", (All + i)->mc);
  else printf("%d ", (All + i)->mc);
 }
 return 0;
}


截图:



——Apie陈小旭
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: