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

本文介绍了一个基于C++的比赛排名系统实现方法。系统通过读取参赛者的成绩和比赛轮次信息,利用双端队列和数组来跟踪每一轮比赛的胜者,并最终确定所有参赛者的排名。适用于需要进行多轮淘汰赛的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <cmath>
#include <string.h>
#include <deque>
using namespace std;

int main(int argc, char *argv[]) {
    int np, ng;
    scanf("%d %d", &np, &ng);
    int i, j, k, in;
    int *w = new int[np];
    int *rank = new int[np];
    deque<int> order;
    memset(rank, 0, np * sizeof(int));
    for (i = 0; i < np; i++) {
        scanf("%d", &w[i]);
    }
    for (i = 0; i < np; i++) {
        scanf("%d", &in);
        order.push_back(in);
    }
    order.push_back(-1);

    int winner_count = 0;
    int max = -1;
    int maxIndex = 0;
    while (order.size() >= 3) {
        winner_count = (order.size() - 1) / ng + 1;
        if ((order.size() - 1) % ng == 0) winner_count--;
        while (order.front() != -1) {
            max = -1;
            maxIndex = 0;
            for (i = 0; i < ng && order[i] != -1; i++) {
                if (max < w[order[i]]) {
                    max = w[order[i]];
                    maxIndex = i;
                }
            }
            for (i = 0; i < ng && order.front() != -1; i++) {
                int tmp = order.front();
                order.pop_front();
                if (i == maxIndex) {
                    order.push_back(tmp);
                } else {
                    rank[tmp] = winner_count + 1;
                }
            }
        }
        order.pop_front();
        order.push_back(-1);
    }
    rank[order.front()] = 1;


    for (i = 0; i < np; i++) {
        printf("%d", rank[i]);
        if (i < np - 1) printf(" ");
    }
    printf("\n");

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值