题目地址: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;
}