1056 Mice and Rice (25分)队列

①i * ng + j + 1 > sum,因为sum是个数,从1开始,而j是下标,从0开始,所以需要加1.
②不管有没有晋级,都先给他名词group+1,若晋级,等待后期覆盖。

//0  1  2 3  4  5 6  7  8  9  10    序号
//25 18 0 46 37 3 19 22 57 56 10    weight
//6 0 8 7 10 5 9 1 4 2 3            参赛顺序,放入队列

//8  7  9  3                        一轮
//8  3                              二轮
//8                                 三轮,比拼完毕
#include<iostream>
#include<queue>
struct mouse {
    
    
    int weight, rank;
}m[1010];
using namespace std;
int main()
{
    
    
    int i, j, max, np, ng;
    queue<int> q;
    cin >> np >> ng;
    for (i = 0; i < np; i++)
    {
    
    
        cin >> m[i].weight;
    }
    for (i = 0; i < np; i++)
    {
    
    
        cin >> j;
        q.push(j);
    }
    int sum = np, group;                       //初始化第一轮比拼数据
    while (q.size() > 1)
    {
    
    
        if (sum % ng == 0) group = sum / ng;
        else group = sum / ng + 1;
        for (i = 0; i < group; i++)
        {
    
    
            max = q.front();
            for (j = 0; j < ng; j++)
            {
    
    
                if (i * ng + j + 1 > sum) break;   
                if (m[q.front()].weight > m[max].weight)
                {
    
    
                    max = q.front();
                }
                m[q.front()].rank = group + 1;
                q.pop();
            }
            q.push(max);
        }
        sum = q.size();    
    }
    m[q.front()].rank = 1;
    cout << m[0].rank;
    for (i = 1; i < np; i++)
    {
    
    
        cout << " " << m[i].rank;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42835526/article/details/113308939