1056 Mice and Rice (25分)【队列模拟】

原题链接
在这里插入图片描述
输入:

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

这题啊 首先就是看不懂题意 害
大致意思就是程序员按顺序排并且给出了每个程序员对应的控制的老鼠的权重 但是排名的时候是打乱顺序按组来进行
注意点:
要记得处理最后队列中剩余元素的排名mouse[q.front()].r = 1;还有每次分组用tempif(temp % g == 0)而不是p做分母 p做分母报给我超时 不过我也不太理解为啥 感觉只是做了个除法 居然影响这么大

#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 10010;

struct mouse
{
    
    
	int weight;
	int r;
}mouse[maxn];

int main()
{
    
    
	int p,g;
	queue<int> q;//用来存放初始顺序
	cin >> p >> g;
	for(int i = 0; i < p; i++)
	{
    
    
		cin >> mouse[i].weight;
	}
	for(int i = 0; i < p; i++)
	{
    
    
		int order;
		cin >> order;
		q.push(order);
	}
	int group,temp;
	temp = p;
	while(q.size()!=1)
	{
    
    
		//分组
		if(temp % g == 0)
		{
    
    
			group = temp / g;
		}
		else group = temp / g + 1;
		//组内操作 
		for(int i = 0; i < group; i++)
		{
    
    

			int k = q.front();//记录最大
			//最小组 内操作:找最大值
			for(int j = 0; j < g; j++)
			{
    
    
				//最后一组不满q需要break;
				if(i * g + j == temp) break;
				int front = q.front();
				if(mouse[front].weight > mouse[k].weight)
				{
    
    
					k = front;
				}
				mouse[front].r = group + 1;//group代表前group名 剩下的自然是group+1名
				q.pop();
			}
			q.push(k);//挑选出代表最小分组的优胜者进行下一次重新分组
		}
		temp = group;
	}
	mouse[q.front()].r = 1; //一定记得处理队列中最后一个元素
	for(int i = 0; i < p; i++)
	{
    
    
		cout << mouse[i].r;
		if( i < p-1) cout << " ";
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/moumoumouwang/article/details/112482828