pat 1056

一开始理解错了题意,以为第二行的第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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值