用队列来打比赛:PAT_A1056:Mice and Rice

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

struct mouse {
	int weight;
	int rank;
} mice[1005];

queue<int> game;

int main() {
	int NP, NG;
	cin >> NP >> NG;
	for (int i = 0; i < NP; i++) {
		cin >> mice[i].weight;
	}
	int order, count = 0, group;
	for (int i = 0; i < NP; i++) {
		cin >> order;
		count++;
		game.push(order);
	}
	if (count % NG == 0)
		group = count / NG;
	else
		group = count / NG + 1;
	int temp = NP;
	while (game.size() != 1) {
		for (int i = 0; i < group; i++) {
			int greatest = game.front();
			//game.pop();
			for (int j = 0; j < NG; j++) {
				if (i * NG + j >= temp)
					break;
				if (mice[game.front()].weight > mice[greatest].weight) {
					greatest = game.front();
				}
                mice[game.front()].rank = group + 1;
                game.pop();
			}
			game.push(greatest);
		}
        temp = group;
		if (group % NG == 0)
			group /= NG;
		else
			group = group / NG + 1;
	}
	mice[game.front()].rank = 1;
	for (int i = 0; i < NP; i++) {
		if (i < NP - 1)
			cout << mice[i].rank << " ";
		else
			cout << mice[i].rank << "\n";
	}
	return 0;
}

通过控制比赛组数,调整出队入队来实现对比赛的模拟。 

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值