A1056 Mice and Rice (25 point(s))

queue 取本轮最大重量大mouse进入下一轮

1. 原文

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 N**P 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:

1
2
3
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:

1
5 5 5 2 5 5 5 3 1 3 5

2. 解析

第三行是控制老鼠🐭的程序员的编号程序员i对应老鼠

每组size/ng个或size/ng++个,取组中最大值进入下一轮q.push(maxnode),组内其他老鼠的排名相同pro[temp.id].rank=group+1

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int w[1010]={};
struct node{
int id,mouse,weight,rank;
}pro[1010];
bool cmp(node a,node b){
return a.mouse<b.mouse;
}
int main()
{
int np,ng;
scanf("%d%d",&np,&ng);
for (int i = 0; i < np; ++i)
{
scanf("%d",&w[i]);
}
int a;
for (int i = 0; i < np; ++i)
{
scanf("%d",&a);
pro[i].id=i;
pro[i].mouse=a;
pro[i].weight=w[a];
}
queue<node> q;
for (int i = 0; i < np; ++i)
{
q.push(pro[i]);
}
while(!q.empty()){
int size=q.size();
if (size==1)
{
pro[q.front().id].rank=1;
break;
}
int group=size/ng;
if (size%ng!=0)
{
group++;
}
int max=-1; node maxnode; int cnt=0;
for (int i = 0; i < size; ++i)
{
node temp=q.front();
cnt++;
q.pop();
pro[temp.id].rank=group+1;
if (pro[temp.id].weight>max)
{
max=pro[temp.id].weight;
maxnode=temp;
}
if (cnt==ng||i==size-1)
{
cnt=0;
max=-1;
q.push(maxnode);
}
}
}
sort(pro,pro+np,cmp);
for (int i = 0; i < np; ++i)
{
if (i!=0)
{
printf(" ");
}
printf("%d",pro[i].rank);
}
printf("\n");
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1056/
  • 发布时间: 2019-09-03 12:33
  • 更新时间: 2021-10-29 14:02
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!