1056 Mice and Rice

题意:略

思路:利用queue来模拟一轮一轮的比赛。自己第一遍做的时候完全没有用queue做的意识,代码写的贼烦最后还只得了17分,非常郁闷。通过本题反映出对queue的应用场景季度不熟悉,STL里面用的最少的就是队列了。另外还有一点,在当前这一轮被淘汰的老鼠排名均为当前组数+1,这一点我也没看出来,自己做的时候拐了18个弯去实现这一点,真是惭愧!

代码:

#include <cstdio>
#include <queue>
using namespace std;

struct Mouse{
    int w;
    int r;
}mouse[1005];

int main()
{
    //freopen("pat.txt","r",stdin);
    int n,step;
    scanf("%d%d",&n,&step);
    int order;
    queue<int> q;
    for(int i=0;i<n;i++)
        scanf("%d",&mouse[i].w);
    for(int i=0;i<n;i++){
        scanf("%d",&order);
        q.push(order);
    }
    int temp=n,group;//temp为当前这一轮参加的老鼠个数,初始化为n;group为组数
    while(q.size()!=1){
        //计算这一轮的分组
        group=(temp%step==0 ? temp/step : temp/step+1);
        //遍历每一组,找出该组的老鼠质量的最大值
        for(int i=1;i<=group;i++){
            int maxIdx=q.front();//记录该组质量最大的下标
            for(int j=1;j<=step;j++){          //用于判断最后一组不满step个的情况
                if((i-1)*step+j>temp) break;
                if(mouse[q.front()].w > mouse[maxIdx].w)
                    maxIdx=q.front();
                mouse[q.front()].r=group+1;//关键,规律
                q.pop();
            }
            //maxIdx即这一组的胜利者,push进队列,进入下一轮的比赛
            q.push(maxIdx);
        }
        temp=group;//下一轮参加比赛的老鼠个数就是这一轮的组数
    }
    mouse[q.front()].r=1;
    printf("%d",mouse[0].r);
    for(int i=1;i<n;i++)
        printf(" %d",mouse[i].r);
    return 0;
}

原文地址:https://www.cnblogs.com/kkmjy/p/9562478.html

时间: 2024-08-05 08:20:08

1056 Mice and Rice的相关文章

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) 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

PAT 1056 Mice and Rice

#include <cstdio> #include <climits> #include <cstdlib> #include <vector> #include <list> using namespace std; list<int>::iterator group_pick(list<int> &player, list<int>::iterator &cur, int group_si

【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

数据结构专题——队列的应用 A1056.Mice and Rice ( 25)

#include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; const int maxn = 1010; struct mouse{ int weight;//质量 int R;//排名 }mouse[maxn]; int main(){ int np,ng,order; scanf("%d%d",&np,&ng); for(int

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

1056. 组合数的和

1056. 组合数的和(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入格式: 输入在一行中先给出N(1<N<10),随后是N个不同的非0个位数字.数字间以空格分隔. 输出格式: 输出所有可能组合出来的2

计算理论中的莱斯定理(Rice&#39;s Theorem)——证明与应用

我们给出一个在探讨不可判定性时非常有用的结论--莱斯定理(Rice's Theorem).首先,我们来看前面讨论过的几个不可判定的例子: 这些都是由图灵机识别之语言的性质.而莱斯定理告诉我们,任何由图灵机识别之语言的非平凡性质(nontrivial property)都是不可判定的. 最后通过几个例子来探讨一下莱斯定理的应用.来看看下面这个语言能否使用莱斯定理来确定其可判定性. {<M> | M是一个TM,且L(M)可由一些拥有偶数个状态的图灵机识别} 首先来确定这是否是一个语言属性,显然是的

poj 1056 IMMEDIATE DECODABILITY

题目链接:http://poj.org/problem?id=1056 思路: 检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法.这里为了练习KMP算法使用了KMP算法. 代码: #include <iostream> using namespace std; const int N = 10; const int Len = 20; char A[N][Len]; int Next[N][Len]; void get_nextval( char P[], int Next[]