PAT A 1056 Mice and Rice (25分)

一、思路

弄了一个来小时…题目没看懂…

题目大意:
Np为老鼠总数,Ng为最大成员数。则对Np个老鼠每Ng个为一组,最重的胜出进入下一轮,并统计老鼠排名,知道产生冠军;
并按照老鼠的序号给出重量,然后给出比赛的初始顺序;

例:
Np = 11, Ng = 3
初始顺序 6 0 8 7 10 5 9 1 4 2 3,即序号6、0、8为第一组,序号7、10、5为第二组,依次类推。

二、代码

#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
    int Np, Ng;
    scanf("%d %d", &Np, &Ng);
    vector<int> w(Np), rank(Np), match(Np);
    for( int i = 0; i < Np; ++i )
        scanf("%d", &w[i]);
    for( int i = 0; i < Np; ++i )
        scanf("%d", &match[i]);
    while( match.size() > 1 )
    {
        vector<int> temp;
        int r = match.size() / Ng + 1;
        if( match.size() % Ng )
            ++r;
        for( int i = 0; i < match.size(); i += Ng )
        {
            int max = match[i];
            for( int j = i; j < match.size() && j < i + Ng; ++j )
                if( w[ match[j] ] > w[max] )
                    max = match[j];
            for( int j = i; j < match.size() && j < i + Ng; ++j )
                if( match[j] != max )
                    rank[ match[j] ] = r;
            temp.push_back(max);
        }
        match = temp;
    }
    rank[ match[0] ] = 1;
    for( int i = 0; i < Np; ++i )
        printf("%s%d", i ? " ":"", rank[i]);
}

发布了152 篇原创文章 · 获赞 144 · 访问量 9212

猜你喜欢

转载自blog.csdn.net/qq_43749739/article/details/103947155