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 3
25 18 0 46 37 3 19 22 57 56 10
6 0 8 7 10 5 9 1 4 2 3
5 5 5 2 5 5 5 3 1 3 5
题意:
有11只老鼠,每三只分一个小组,第一行输入这11只老鼠编号0到11的体重,下一行给出一个序列,为11只老鼠的编号,问给这11只老鼠根据体重排名,排名规则是三个一小组,选出三个里面体重最大的老鼠晋级,这是第一轮,晋级后又分小组,再晋级,这是第二轮...同一轮没晋级的老鼠排名相同;
思路:用结构题保存老鼠的编号和体重,用队列queue储存这些老鼠的结构体,然后分组,遍历组内成员,组内遍历一只老鼠就出队一只,遍历组内老鼠后,找到体重最大老鼠的编号,再放到队列的后面,代表晋级,这样一来,遍历完一次所有老鼠后再队内的就是晋级了一轮的,这样多来几轮后,直到组内只剩下一只老鼠,这只老鼠就是老鼠王,给其排名设为1,最后再按这个顺序把老鼠的排名遍历出来;
代码:
#include
#include
using namespace std;
const int maxn=10010;
struct mouse{//结构体类型可以和结构体变量同名;
int weight;
int rank;
}mouse[maxn];
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i>mouse[i].weight;
}
queue q;
int num;
for(int i=0;i>num;
q.push(num);
}
int allnum=n;//mouse的当前总数,晋级后总数会变,这个数用来判断该轮是否遍历了所有老鼠,以为算出该轮有多少个小组;
while(q.size()!=1){//控制轮数;
int group;
int allnum=q.size();
allnum%m==0?group=allnum/m:group=allnum/m+1;
for(int i=0;i=allnum){//因为下一轮的老鼠也是进入对了里面,所以i不能用empty来判断这一轮的老鼠是否遍历完;
break;
}
if(mouse[q.front()].weight>mouse[k].weight){
k=q.front();
}
mouse[q.front()].rank=group+1;
q.pop();
}
q.push(k);//把这一组晋级的老鼠放到q里面,为下一轮晋级做准备;
}
}
mouse[q.front()].rank=1;
for(int i=0;i
队列queue:
1.clea,队列和栈一样不能直接用clear清理掉队内元素,可以用
while(q.empty()==false){
q.pop();
}
或者直接初始化,即新定义一个queue,其时间复杂度为o(1);
2.empty,返回得是bool类型,不为空就是false;
3.q.push是入队,q.poo()是出队,返回是void,而front(),rear()分别是取队首元素,和取队尾元素,取元素和出队前必须用empty判断队是否有元素;
4.front指针指向队首的上一个元素,rear指向队尾元素;
后面又重新写了一次
#include
#include
using namespace std;
const int Maxn = 10010;
struct node {
int weight;
int rank;
}mouse[Maxn];
int main() {
queue q;//存放入队顺序;
int n, m;
cin >> n >> m;//老鼠数和多少个老鼠分一组;
for(int i = 0; i < n; i++)
cin >> mouse[i].weight;//按顺序输出0到10老鼠的体重;
for(int i = 0; i < n; i++) {
int num;
cin >> num;
q.push(num);//输出老鼠的入队顺序;
}
while(q.size() != 1) {
int allnum = q.size();
int group = allnum % m == 0 ? allnum / m : allnum / m + 1;
for(int i = 0; i < group; i++) {
int k = q.front();//假设第一只老鼠的质量最大,记下其序号;
for(int j = 0; j < m; j++) {
if(i * m + j >= allnum)//这条限制是用在分组中有没满的情况的,如果满了就是for循环进行限制了,可以保证遍历完所有的老鼠而不超过
break;//就比如题目这种情况13/4,有一组只有一个老鼠,如果都分满的话不走这一条;
if(mouse[q.front()].weight > mouse[k].weight)
k = q.front();
mouse[q.front()].rank = group + 1;
q.pop();
}
q.push(k);
}
}
mouse[q.front()].rank = 1;
for(int i = 0; i < n; i++) {
if(i != 0)
cout << " ";
cout << mouse[i].rank;
}
return 0;
}