- 题意:m个数,n个一组,要求每组选出最大的数字晋级到下一轮,其余数字排名相同,直到决出最后一名。
代码
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int maxn = 1010;
struct mou
{
int wei, rank;
} mouse[maxn];
queue <int> q;
int main()
{
int p, g;
cin >> p >> g;
for(int i = 0; i < p; i++)
scanf("%d", &mouse[i].wei);
for(int i = 0; i < p; i++)
{
int a;
scanf("%d", &a);
q.push(a);
}
int remain = p;
while(q.size() > 1)
{
int group = remain / g;
if(remain % g > 0)
group++;
for(int i = 1; i <= group; i++)
{
int max = q.front();
//该轮的每一组
for(int j = 1; j <= g; j++)
{
//最后不足一组时
if(remain < (i - 1) * g + j)
break;
if(mouse[max].wei < mouse[q.front()].wei && !q.empty())
{
max = q.front();
}
mouse[q.front()].rank = group + 1;
q.pop();
}
q.push(max);
}
remain = group;
}
mouse[q.front()].rank = 1;
for(int i = 0; i < p; i++)
{
printf("%d", mouse[i].rank);
if(i < p - 1)
printf(" ");
}
return 0;
}