题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1056
思路,每次选出符合条件的然后放在新的数组里,一直循环直到剩一个,然后更新排名,此轮losers排名=这轮winners个数+1.
1 #include<cstdio> 2 #include<vector> 3 using namespace std; 4 5 int main() 6 { 7 int N,M; 8 scanf("%d%d",&N,&M); 9 vector<int> weight(N); 10 vector<int> outcome(N,0); 11 vector<int> order[1000]; 12 int length(0); 13 for(int i=0; i<N; ++i) 14 scanf("%d", &weight[i]); 15 for(int i=0; i<N; ++i) 16 { 17 int d; 18 scanf("%d", &d); 19 order[length].push_back(d); 20 } 21 ++length; 22 while(order[length-1].size() != 1) 23 { 24 int count =order[length-1].size(); 25 if(count % M == 0) 26 count /= M; 27 else 28 count = count/M+1; 29 for(int i=0; i<count; ++i) 30 { 31 int max(-1), index(-1); 32 for(int j=i*M; j<i*M+M && j<order[length-1].size(); ++j) 33 { 34 if(weight[order[length-1][j]] > max) 35 { 36 max = weight[order[length-1][j]]; 37 index = order[length-1][j]; 38 } 39 } 40 order[length].push_back(index); 41 } 42 ++length; 43 } 44 for(int i=length-1; i>=0; --i) 45 { 46 if(order[i].size()==1) 47 outcome[order[i][0]] = 1; 48 else 49 { 50 int rank = order[i+1].size()+1; 51 for(int j=0; j<order[i].size(); ++j) 52 if(outcome[order[i][j]] == 0) 53 outcome[order[i][j]] = rank; 54 } 55 } 56 for(int i=0; i<N-1; ++i) 57 printf("%d ", outcome[i]); 58 printf("%d ", outcome[N-1]); 59 return 0; 60 }