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

#include 
#include 
#include 
#include 
#include 
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;iscanf("%d",&mice[i].weight);
    }
    for(int i=0;iscanf("%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;iint MAXW=-1,step=0,pre=j,MAXI=j;
            while(jint 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;kif(mice[p[k]].flag==false&&p[k]!=MAXI)
                {
                    mice[p[k]].flag=true;
                    mice[p[k]].rank=r;
                    restnum--;
                }
            }
        }
        r++;
    }
    for(int i=0;iif(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;iif(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;iif(i!=0)printf(" ");
        printf("%d",mice[i].r);
    }
    return 0;
}

你可能感兴趣的:(PAT,1056)