PAT (Advanced Level) 1056. Mice and Rice (25)

简单模拟。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<stack>
#include<vector>
using namespace std;

const int maxn=1000+10;
struct X
{
    int val;
    int id;
}s[maxn];
int n,m;
int ans[maxn];
int h[maxn],cnt;
queue<X>Q[2];

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&s[i].val);
    for(int i=1;i<=n;i++) s[i].id=i;
    for(int i=1;i<=n;i++)
    {
        int f; scanf("%d",&f); f++;
        Q[0].push(s[f]);
    }

    int now=0;

    while(1)
    {
        memset(h,0,sizeof h);
        if(Q[now].size()==1)
        {
            ans[Q[now].front().id]=1;
            break;
        }

        while(1)
        {
            if(Q[now].size()<=m)
            {

                X tmp; tmp.val=-99999;
                while(!Q[now].empty())
                {
                    h[Q[now].front().id]=1;
                    if(Q[now].front().val>tmp.val) tmp=Q[now].front();
                    Q[now].pop();
                }
                h[tmp.id]=0;
                Q[now^1].push(tmp);
                break;
            }
            else
            {
                X tmp; tmp.val=-99999;
                for(int i=0;i<m;i++)
                {
                    h[Q[now].front().id]=1;
                    if(Q[now].front().val>tmp.val) tmp=Q[now].front();
                    Q[now].pop();
                }
                h[tmp.id]=0;
                Q[now^1].push(tmp);
            }
        }
        now=now^1;
        for(int i=1;i<=n;i++)
            if(h[i]==1) ans[i]=Q[now].size()+1;
    }
    for(int i=1;i<=n;i++)
    {
        printf("%d",ans[i]);
        if(i<n) printf(" ");
        else printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5550479.html