模拟每轮的结果,每小组的第一进入下一轮,即将对应的id放入下轮的order中,在这轮停止的则放入栈中,这样直到最后order中仅一个人表示已经选出最大的,结束模拟,然后从栈中全部取出(此时已经是有序的了),只需根据round值更新排名信息,最后按0-Np的顺序输出即可。
题目简单没有坑,只是有点繁琐。
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
int p,g;
struct Mice{
int weight, round = 0, id, rank;
Mice(){
}
bool operator < (const Mice &a) const {
return weight < a.weight;
}
};
Mice w[1005];
stack<Mice> ans;
vector<int> Choose(vector<int> order){
vector<int> new_order;
priority_queue<Mice> pq;
for(int i = 0; i < order.size(); ++i){
for(int k = 0; k < g && i < order.size(); ++i, ++k){
++w[order[i]].round;
pq.push(w[order[i]]);
}
--i;
new_order.push_back(pq.top().id);
pq.pop();
while(!pq.empty()){
ans.push(pq.top());
pq.pop();
}
}
return new_order;
}
int main(){
scanf("%d%d",&p,&g);
for(int i = 0; i < p; ++i){
scanf("%d",&w[i].weight);
w[i].id = i;
}
int j;
vector<int> order;
for(int i = 0; i < p; ++i){
scanf("%d",&j);
order.push_back(j);
}
while(order.size() != 1){
order = Choose(order);
}
int cnt = 0, rank = 1, round;
w[order[0]].rank = rank++;
round = ++w[order[0]].round;
while(!ans.empty()){
Mice tmp = ans.top();
if(round == tmp.round){
tmp.rank = rank;
w[tmp.id] = tmp;
++cnt;
}
else{
tmp.rank = cnt + rank;
rank = tmp.rank;
cnt = 1;
round = tmp.round;
w[tmp.id] = tmp;
}
ans.pop();
}
for(int i = 0; i < p; ++i){
if(i != 0)
printf(" %d",w[i].rank);
else
printf("%d",w[i].rank);
}
return 0;
}