一开始理解错了题意,以为第二行的第i个数是第一行数的order,实际上是根据第二行排序,例如第二行第一个数是6,就是第一行6个数作为第一组第一号
所以这样看用queue是更好的选择
#include<stdio.h>
#include<algorithm>
#include<vector>
#define SIZE 1000
using namespace std;
struct mice{
int index;
int order;
int weight;
int prize;
int level;
} node[SIZE];
vector<int> test;
bool cmp_order(const int &a, const int &b){
return node[a].order < node[b].order;
}
int prize = 1;
void Rice(int n, int group, int level){
if (n == 1){
node[test[0]].prize = prize;
prize++;
return;
}
vector<int>tmp;
tmp = test;
test.clear();
int max = -1;
int index;
int i, j;
for (i = 0; i + group < tmp.size(); i += group){
max = -1;
for (j = i; j < i + group; j++)
if (node[tmp[j]].weight > max){
max = node[tmp[j]].weight;
index = tmp[j];
}
node[index].level = level + 1;
test.push_back(index);
}
max = -1;
for (j = i; j < tmp.size();j++)
if (node[tmp[j]].weight > max){
max = node[tmp[j]].weight;
index = tmp[j];
}
node[index].level = level + 1;
test.push_back(index);
Rice(test.size(), group, level + 1);
int count = 0;
for (i = 0; i < tmp.size();i++)
if (node[tmp[i]].level == level){
node[tmp[i]].prize = prize;
count++;
}
prize += count;
}
int main(){
freopen("1.in", "r", stdin);
int n, group;
scanf("%d%d", &n, &group);
int i;
for (i = 0; i < n; i++){
scanf("%d", &node[i].weight);
test.push_back(i);
node[i].index = i;
}
int order;
for (i = 0; i < n; i++){
scanf("%d", &order);
node[order].order = i;
}
sort(test.begin(), test.end(), cmp_order);
Rice(n, group, 0);
for (i = 0; i < n; i++){
printf("%d", node[i].prize);
if (i == n - 1)
putchar('\n');
else putchar(' ');
}
return 0;
}