PAT 甲1056 Mice and Rice 队列

PAT 甲级 1056 Mice and Rice



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

#include<cstdio>
#include<queue>
using namespace std;
queue<int>q;
struct mouse{
    int w;
    int rank;
}mouse[1010];
int main(){
    int NP,NG,order;
    scanf("%d%d",&NP,&NG);
    for(int i=0;i<NP;i++){
        scanf("%d",&mouse[i].w);
    }
    for(int i=0;i<NP;i++){
        scanf("%d",&order);
    	q.push(order);
    }
    //temp记录每一轮多少参赛者 
	int temp=NP,group;
	int k;//记录一个group中最大的编号 
	while(q.size()!=1){
		if(temp%NG==0)
			group=temp/NG;
		else
			group=temp/NG+1;
		for(int i=0;i<group;i++){
			k=q.front();
			for(int j=0;j<NG;j++){
				if(i*NG+j>=temp) break;
				int o=q.front();
				if(mouse[o].w>mouse[k].w)
					k=o;
				mouse[o].rank=group+1;
				q.pop();
			}
			mouse[k].rank-=1;
			q.push(k);
		}
		temp=group;
	}
	mouse[q.front()].rank=1;
	for(int i=0;i<NP;i++)
	{
		if(i!=NP-1)
		printf("%d ",mouse[i].rank);
		else
		printf("%d\n",mouse[i].rank);
	}
}
发布了9 篇原创文章 · 获赞 3 · 访问量 263

猜你喜欢

转载自blog.csdn.net/weixin_39666736/article/details/96383454
今日推荐