1056マウスとライス(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

これは最初の質問ですああ、問題の意味を理解していないので、
プログラマーがシーケンスを実行し、マウスにはすべてのプログラマーの対応する重みを制御する権利が与えられますが、グループによってランキングが乱れている場合は
、ポイントに注意を払う必要があります:
覚えておいてください最終キューの残りの要素のランキングを処理し、pを分母としてpの代わりにmouse[q.front()].r = 1;temp使用if(temp % g == 0)して、グループ化するたびにタイムアウトを報告しますが、なぜそれがちょうど行うように感じるのかわかりません分数は非常に大きな影響を及ぼします。

#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