这类题目是C++标准模板库的使用,是重点知识。
题目要描述如下:
题目大致意思:
np为老鼠的数量,ng为每组最多g个老鼠。先给出np个老鼠的重量,再给出老鼠的初始顺序(第i名的老鼠是第j号,j从0开始)。每ng个老鼠分为一组,对于每组老鼠,选出最重的那个,晋级下一轮比赛,然后依次再以np个老鼠一组分类,然后选出重量最大的。。。直到只剩下一只老鼠,排名为1.输出为老鼠的排名,这个排名是按照原输入老鼠的顺序输出的。
大致思路:
可以使用两个数组,分别来存储老鼠的重量和序列信息,然后按照序列信息,让对应老鼠的重量入队列,全部入队列后,获得当前队列的长度,进而获得分组数,让队列进行出队操作,出队的元素进入一个新的数组,每出队m个元素,找出数组中最大的那个元素,让其再入队。
提交结果如下:
有两个测试用例未能通过,放在第二轮刷题时解决。
提交的代码如下:
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
struct Node
{
int wei;
int ord;
};
Node find_max(int duishu);
vector<Node> res;
vector<Node> weight;
int paiming[1000] = {0};
int main()
{
vector<int> order;
queue<Node> que1;
int n, m;
cin >> n >> m;
for (int i = 0;i < n;i++)
{
Node node;
cin >> node.wei;
node.ord = i;
weight.push_back(node);
}
for (int i = 0;i < n;i++)
{
int temp;
cin >> temp;
order.push_back(temp);
}
for (int i = 0;i < n;i++)
{
que1.push(weight[order[i]]);
}
while (!que1.empty())
{
int size = que1.size();
if (size == 1)
{
paiming[que1.front().ord] = 1;
break;
}
int duishu = size / m + 1;
for (int i = 0;i < size;i++)
{
res.push_back(que1.front());
que1.pop();
if ((i+1) % m == 0)
{
que1.push(find_max(duishu));
res.clear();
}
}
que1.push(find_max(duishu));
res.clear();
}
int i = 0;
for (i;i < n-1;i++)
{
cout << paiming[i] << " ";
}
cout << paiming[i];
}
Node find_max(int duishu)
{
Node max = res[0];
for (int i = 0;i < res.size();i++)
{
if (res[i].wei > max.wei)
{
paiming[max.ord] = duishu + 1;
max = res[i];
}
if (res[i].wei < max.wei)
{
paiming[res[i].ord] = duishu + 1;
}
}
return max;
}
本次提交后累计得分1076。