zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    CArray类模板封装示例
    环形队列CQueue类模板示例
    CStack栈模板封装示例
    TSIC506驱动程序
    proteus虚拟串口实现
    js学习笔记(一)
    提升网页性能
    JS性能优化笔记搜索整理
    @font-face制作Web Icon
    原生js动画效果(源码解析)
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5550479.html
Copyright © 2011-2022 走看看