PAT甲级 1056 Mice and Rice

原题链接:

https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7?problemSetProblemId=994805419468242944&page=0

代码:

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

#define endl '\n'

const int N = 1e5;

int Np, Ng;
int w[N];//记录每只老鼠的重量
int r[N];//记录每只老鼠的淘汰轮次
queue<int> q;//记录比赛的顺序
vector<pair<int, int> > v;//<编号,r[编号]>
int ans[N];

queue<int> tQ;
int lastMatch = 0;
void match(int round) {
	int matchCnt = q.size() / Ng;//当前回合比赛次数
	if (q.size() % Ng)
		matchCnt++;
	for (int i = 0; i < matchCnt; i++) {
		int winner=0, winnerWeight=0;
		int x = 0;
		while (q.size()&&x<Ng) {
			int nowMouse = q.front();
			if (w[nowMouse] >= winnerWeight) {
				winner = nowMouse;
				winnerWeight = w[nowMouse];
			}
			r[nowMouse] = round;
			q.pop();
			x++;
		}
		r[winner]++;
		lastMatch = r[winner];
		tQ.push(winner);
	}
}

bool cmp(pair<int, int>& a, pair<int, int>& b) {
	return a.second > b.second;
}

signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	cin >> Np >> Ng;
	for (int i = 0; i < Np; i++) {
		cin >> w[i];
	}
	for (int i = 0; i < Np; i++) {
		int temp;
		cin >> temp;
		q.push(temp);
	}

	int record = 1;
	while (q.size() != 1) {
		while (tQ.size()) {
			tQ.pop();
		}
		match(record);
		record++;
		q = tQ;
	}

	for (int i = 0; i < Np; i++) {
		v.push_back({ i, r[i] });
	}

	sort(v.begin(), v.end(), cmp);
	for (int i = 0; i < Np; i++) {
		if (i == 0) {
			ans[v[0].first] = 1;
			continue;
		}
		if (v[i].second == v[i - 1].second) {
			ans[v[i].first] = ans[v[i - 1].first];
		}
		else {
			ans[v[i].first] = i + 1;
		}
	}
	for (int i = 0; i < Np; i++) {
		if (i != 0) {
			cout << " ";
		}
		cout << ans[i];
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值