当前位置: 代码迷 >> 综合 >> PAT甲级题目1056 Mice and Rice
  详细解决方案

PAT甲级题目1056 Mice and Rice

热度:0   发布时间:2024-02-13 05:05:16.0

代码:

#include<cstdio>
#include<vector>
#define maxn 1010
using namespace std;
struct mouse
{int weight;int rank;
}mice[maxn]; 
int main()
{int NP,NG;int Rat,Group;int i,j,k,front,current,u,max;vector<int> Seq,temp;scanf("%d%d",&NP,&NG);for(i=0;i<NP;i++){scanf("%d",&mice[i].weight);}for(i=0;i<NP;i++){scanf("%d",&j);Seq.push_back(j);}Rat=NP;while(1){            Group=(Rat+NG-1)/NG;//组数front=0;temp.clear();for(i=0;i<Group;i++){u=-1,max=-1;for(j=0;j<NG;j++){current=Seq[front++];mice[current].rank=Group+1;if(mice[current].weight>max){max=mice[current].weight;u=current;}if(front==Rat){break;}}temp.push_back(u);}Rat=Group;Seq=temp;if(Rat==1){mice[Seq[0]].rank=1;break;}}for(i=0;i<NP;i++){printf("%d%s",mice[i].rank,i==NP-1?"":" ");}return 0;
}