PAT1056.Mice and Rice (25)

题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1056

这题为浙大2013年考研复试上机的倒数第二题,分值为25分。此题得分率很低。

在考试之后,很多考生反映,题目读不懂。

读不懂应该是源于sample input中的第三行。第三行的序列给出的是依次要进行比较的老鼠的编号。

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 "stdafx.h"
#include <cstdio>
#include <cstring>

const int N=1005;
int mouse[N],order[N],rank[N];
bool used[N];

int main()
{
	//freopen("D://test.txt","r",stdin);
    int NP,NG,i,group;
	bool flag=true;
	memset(used,0,sizeof(used));
	scanf("%d %d",&NP,&NG);
	for(i=0;i<NP;++i)
		scanf("%d",mouse+i);	
	for(i=0;i<NP;++i)
		scanf("%d",order+i);	
	NP%NG==0?group=NP/NG:group=NP/NG+1;

	int last,max,countNG;
	while(flag){
		if(group==1)
			flag=false;
		countNG=0,last=-1,max=-1;
		
		for(i=0;i<NP;++i){
			if(!used[order[i]]){
				if(countNG==NG){
					last=-1;
					max=-1;
					countNG=1;
				}
				else
					++countNG;
				if(mouse[order[i]]>max){
					if(last!=-1){
						rank[last]=group+1;
						used[last]=true;
					}
					last=order[i];
					max=mouse[order[i]];
				}
				else{
					rank[order[i]]=group+1;
					used[order[i]]=true;
				}
			}
		}
		group%NG==0?group=group/NG:group=group/NG+1;
	}
	rank[last]=1;

	for(i=0;i<NP;++i)
		i==NP-1?printf("%d\n",rank[i]):printf("%d ",rank[i]);

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值