//
// main.cpp
// PATA1056
//
// Created by Phoenix on 2018/2/14.
// Copyright © 2018年 Phoenix. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
struct Node {
int weight;
int rank;
}node[1010];
bool cmp(int a, int b) {
return node[a].weight > node[b].weight;
}
int main(int argc, const char * argv[]) {
int n, k;
scanf("%d %d", &n, &k);
for(int i = 0; i < n; i++) {
scanf("%d", &node[i].weight);
}
queue<int> q;
for(int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
q.push(a);
}
int rank = (n + k - 1) / k, m = n;
while(n > 1) {
for(int i = 0; i < rank; i++) {
int maxn = -1, t, num = 0;
while(num < k && i * k + num < n) {
int top = q.front();
q.pop();
node[top].rank = rank + 1;
if(node[top].weight > maxn) {
maxn = node[top].weight;
t = top;
}
num++;
}
q.push(t);
}
n = rank;
rank = (n + k - 1) / k;
}
vector<int> v;
while(!q.empty()) {
int top = q.front();
q.pop();
v.push_back(top);
}
sort(v.begin(), v.end(), cmp);
for(int i = 1; i <= v.size(); i++) {
node[v[i - 1]].rank = i;
}
for(int i = 0; i < m; i++) {
printf("%d", node[i].rank);
if(i < m - 1) printf(" ");
else printf("\n");
}
return 0;
}
PATA1056题解
最新推荐文章于 2021-08-26 10:06:41 发布