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("\n");
    }
    return 0;
}
时间: 2024-10-11 23:05:37

PAT (Advanced Level) 1056. Mice and Rice (25)的相关文章

【PAT甲级】1056 Mice and Rice (25 分)

题意: 输入两个正整数N和M(<=1000),接着输入两行,每行N个数,第一行为每只老鼠的重量,第二行为每只老鼠出战的顺序.输出它们的名次.(按照出战顺序每M只老鼠分为一组,剩余不足M只为一组,每组只能有一个胜者,其他老鼠排名均为这一轮胜者数量+1) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int n,m;int a[1007],b[1007];int num;int ans[1

PAT 1056 Mice and Rice (25)

Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse. First

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

PAT (Advanced Level) 1089. Insert or Merge (25)

简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=200; int a[maxn],b[maxn],

PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)

简单DP. 注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence. #include<iostream> #include<cstring> #include<cmath> #include<al

PAT (Advanced Level) 1113. Integer Set Partition (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; long long a[100000+10]; long long sum=0,sum2; i

PAT (Advanced Level) 1040. Longest Symmetric String (25)

暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[2000]; int ans,len,sum; void check(int a,int b) { if(s[a]!=s[b]) return; if(a==b) sum=1; else sum=2; int left=a-1,right=b+1; while(!(left<0||right&

PAT (Advanced Level) 1009. Product of Polynomials (25)

简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; double a[3000],b[3000],c[3000]; int k; int main() { for(int i=0;i<=2000;i++) a[i]=b[i]=c[i]=0; int Max=-1

PAT (Advanced Level) 1094. The Largest Generation (25)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; const int maxn=200; int n,m; vector<int>