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_size, vector<int> &W) {
    int wmax = INT_MIN;
    list<int>::iterator ret = player.end();
    int cnt = group_size;
    //printf("check group:\n\t");
    while (cur != player.end() && cnt > 0) {
        --cnt;
        //printf(" %d(%d)", *cur, W[*cur]);
        if (W[*cur] >= wmax) {
            wmax = W[*cur];
            ret = cur;
        }
        cur++;
    }
    //printf("\n");
    return ret;
}

int main() {

    int N = 0, G = 0;
    scanf("%d%d", &N, &G);

    if (N < 1) return 0;

    vector<int> W(N,  0);
    vector<int> R(N, 0);
    vector<int> L;
    list<int> P;

    for (int i=0; i<N; i++) {
        scanf("%d", &W[i]);
    }
    for (int i=0; i<N; i++) {
        int t = 0;
        scanf("%d", &t);
        P.push_back(t);
    }

    int level = 0;
    int level_cnt = 0;

    list<int> tmp;
    auto cur = P.begin();
    // number of elements in P should be larger than 1 to perform reduce processing
    while (G > 1 && ++(cur = P.begin()) != P.end()) {
        tmp.clear();
        auto cur = P.begin();
        while (cur != P.end()) {
            list<int>::iterator fat = group_pick(P, cur, G, W);
            //printf("pick %d\n", *fat);
            tmp.splice(tmp.end(), tmp, fat);
        }

        swap(tmp, P);
        auto iter = tmp.begin();
        while (iter != tmp.end()) {
            R[*(iter++)] = level;
            level_cnt++;
        }
        L.push_back(level_cnt);
        level_cnt = 0;
        level++;
    }
    // now there must be only one element in P, the final winner
    L.push_back(1);
    R[P.front()] = level;
    int sum = 0;
    for (int i=L.size() - 1; i>=0; i--) {
        //printf("level cnt: %d\n", L[i]);
        int next_sum = sum + L[i];
        L[i] = sum + 1;
        sum  = next_sum;
    }

    int len = R.size();
    printf("%d", L[R[0]]);
    for (int i=1; i<len; i++) {
        printf(" %d", L[R[i]]);
    }
    return 0;
}

有点烦啊

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

PAT 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 (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

1056 Mice and Rice

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

数据结构专题——队列的应用 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

pat 1056 组合数的和

题目如下: 代码如下: #include<cstdio> #include<iostream> using namespace std; int main(){ int n,sum=0;//别忘记初始化,有时候你忘记了,但是编译器又不显示错误,就尴尬了 scanf("%d",&n); int a[n]; for(int i=0;i<n;i++) scanf("%d",&a[i]); for(int i=0;i<n;

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

PAT Basic 1056

1056 组合数的和 给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入格式: 输入在第一行中给出 N(1 < N < 10),随后一行给出 N 个不同的非 0 个位数字.数字间以空格分隔. 输出格式: 输出所有可能组合出来的2位数字的和. 输入样例: 3 2 8 5 输出样例: 330 题解:这道题看上去很想使用暴力,

PAT乙级-1056. 组合数的和(15)

给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入格式: 输入在一行中先给出N(1<N<10),随后是N个不同的非0个位数字.数字间以空格分隔. 输出格式: 输出所有可能组合出来的2位数字的和. 输入样例: 3 2 8 5 输出样例: 330 分析:两位数,根据排列组合,每个数出现(n-1)次个位 , (n-1)次十位 sum=(a*10+a)*