PAT——A1056 Mice and Rice(queue)

题目链接:

#include<utility>
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define maxn 1010
struct number{
  int pm;
  int z;
}num[maxn];
int arr[maxn];
queue<int >st;
int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;i++)
    {
        scanf("%d",&num[i].z);
    }
    for(int i=0;i<m;i++)
    {
        int order;
        scanf("%d",&order);
        st.push(order);
    }
    int temp=m;
    int group;
    while(st.size()!=1)
    {
        if(temp%n==0)
            group=temp/n;
        else
            group=temp/n+1;
        for(int i=0;i<group;i++)
        {
            int k=st.front();
            for(int j=0;j<n;j++)
            {
                if(i*n+j>=temp)
                    break;
                    int front=st.front();
                if(num[front].z>num[k].z)
                    k=front;
                      num[front].pm=group+1;
                st.pop();
            }
            st.push(k);
        }
         temp=group;
    }
    num[st.front()].pm=1;
    for(int i=0;i<m;i++)
    {
        printf("%d",num[i]);
        if(i<m-1)
            printf(" ");
    }
    return 0;
}

【分析】

熟练运用队列先进先出的概念

找到组里面最大的

另外要知道排名就是分组数+1

入队的只是编号

排序比较的则是按照结构体的值

另外排名也是存储在结构体里面

猜你喜欢

转载自blog.csdn.net/qq_42232118/article/details/82084466
今日推荐