题目描述
C++代码(队列+结构体+模拟)
样例
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1010;
struct mouse {
int weight;
int rank;
}m[N];
int main()
{
int np, ng;
cin >> np >> ng;
int temp = np;
for (int i = 0; i < np; i++)
{
cin >> m[i].weight;
}
queue<int> q;
for (int i = 0; i < np; i++)
{
int x;
cin >> x;
q.push(x);
}
while (q.size() > 1)
{
int group;
if (temp % ng == 0) group = temp / ng;
else group = temp / ng + 1;
for (int i = 0; i < group; i++)
{
int t = q.front();
for (int j = 0; j < ng; j++)
{
if (i * ng + j >= temp)
{
break;
}
int tt = q.front();
if (m[t].weight < m[tt].weight)
{
t = tt;
}
m[tt].rank = group + 1;
q.pop();
}
q.push(t);
}
temp = group;
}
m[q.front()].rank = 1;
cout << m[0].rank;
for (int i = 1; i < np; i++)
{
cout << " " << m[i].rank;
}
return 0;
}