PAT甲1056 Mice and Rice (25)(25 分)

#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

struct mouse
{
    int weight;
    bool flag;
    int rank;
    int r;
    mouse()
    {
        flag=false;
    }
}mice[100010];

int p[100010];

int N,M;

bool cmp(int a,int b)
{
    return mice[a].rank>mice[b].rank;
}

int main()
{
    scanf("%d%d",&N,&M);
    for(int i=0;i<N;i++)
    {
        scanf("%d",&mice[i].weight);
    }
    for(int i=0;i<N;i++)
    {
        scanf("%d",&p[i]);
    }
    int restnum=N;
    int r=1;
    while(restnum!=1)
    {
        int group=(restnum-1)/M+1;
        int j=0;
        for(int i=0;i<group;i++)
        {
            int MAXW=-1,step=0,pre=j,MAXI=j;
            while(j<N&&step<M)
            {
                int now=p[j];
                if(mice[now].flag==false)
                {
                    step++;
                    if(mice[now].weight>MAXW)
                    {
                        MAXW=mice[now].weight;
                        MAXI=now;
                    }
                }
                j++;
            }
            for(int k=pre;k<j;k++)
            {
                if(mice[p[k]].flag==false&&p[k]!=MAXI)
                {
                    mice[p[k]].flag=true;
                    mice[p[k]].rank=r;
                    restnum--;
                }
            }
        }
        r++;
    }
    for(int i=0;i<N;i++)
    {
        if(mice[i].flag==false)
        {
            mice[i].rank=r;
            mice[i].flag=true;
            break;
        }
    }
    sort(p,p+N,cmp);
    mice[p[0]].r=1;
    for(int i=1;i<N;i++)
    {
        if(mice[p[i]].rank==mice[p[i-1]].rank)
        {
            mice[p[i]].r=mice[p[i-1]].r;
        }
        else
        {
            mice[p[i]].r=i+1;
        }
    }
    for(int i=0;i<N;i++)
    {
        if(i!=0)printf(" ");
        printf("%d",mice[i].r);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yhy489275918/article/details/81878594