【题意】
就是类似各种赛事的排名规则
【思路】
用一个vector存下需要继续比较的数的下标,重复比较直到胜者只剩一个即可
#include <iostream>
#include <vector>
using namespace std;
typedef struct{
int weight;
int rank;
}mouse;
int main(int argc, char const *argv[])
{
vector<mouse> mice;
vector<int> winners;
int Np,Ng;
cin >> Np >> Ng;
mice.resize(Np);
winners.resize(Np);
for(int i=0; i<Np; i++){
cin >> mice[i].weight;
}
for(int i=0; i<Np; i++){
cin >> winners[i];
}
while(winners.size()>1){
vector<int> newWinners;
int newRank = (winners.size()+Ng-1)/Ng+1;
int i;
for(i=0; i<winners.size(); i+=Ng){
int index = i;
for(int j=i+1; j<i+Ng && j<winners.size(); j++){
if(mice[winners[j]].weight>mice[winners[index]].weight){
index = j;
}
}
newWinners.push_back(winners[index]);
for(int j=i; j<i+Ng && j<winners.size(); j++){
if(j!=index){
mice[winners[j]].rank = newRank;
}
}
}
winners = newWinners;
}
mice[winners[0]].rank = 1;
for(int i=0; i<Np; i++){
if(i){
cout << " ";
}
cout << mice[i].rank;
}
system("pause");
return 0;
}