原题链接:
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;
}