# Codeforces 994B Knights of a Polygonal Table （#488 Div 2 B）题解【堆】【快速排序】【贪心】

算是第一次打CF……

思维量不算大，做法比较巧妙，而且k≤10，就很好做了。

## Description

Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than k other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim’s coins.

Now each knight ponders: how many coins he can have if only he kills other knights?

You should answer this question for each knight.

## Input

The first line contains two integers n and k $(1\leq n\leq 10^5,0\leq k\leq min(n-1,10))$  — the number of knights and the number k from the statement.

The second line contains n integer $p_1,p_2,…,p_n (1≤p_i≤10^9)$ — powers of the knights. All $p_i$ are distinct.

The third line contains n integers $c1,c2,…,c_n (0≤c_i≤10^9)$ — the number of coins each knight has.

## Output

Print n integers — the maximum number of coins each knight can have it only he kills other knights.

input1
4 2
4 5 9 7
1 2 11 33
output1
1 3 46 36
input2
5 1
1 2 3 4 5
1 2 3 4 5

output2

1 3 5 7 9

input3

1 0
2
3

output3

3

## Note

Consider the first example.

• The first knight is the weakest, so he can’t kill anyone. That leaves him with the only coin he initially has.
• The second knight can kill the first knight and add his coin to his own two.
• The third knight is the strongest, but he can’t kill more than k=2 other knights. It is optimal to kill the second and the fourth knights: 2+11+33=46.
• The fourth knight should kill the first and the second knights: 33+1+2=36.

In the second example the first knight can’t kill anyone, while all the others should kill the one with the index less by one than their own.

In the third example there is only one knight, so he can’t kill anyone.

题意就是有n个骑士，每个骑士可以杀死能力值比他弱的k个骑士，每个骑士都有金币，杀死一个骑士后可以获得他的金币。求每个骑士能拥有的金币最大数量。（每次计算所有骑士都是活着的情况）

首先读题可以看出来一个细节，骑士拥有的金币是包含他原来自己的。我们看到k是≤10的，那么我们要让每个骑士从比他能力值低的骑士中找出金币数最多的k个（如果这个骑士的排名i小于等于k那么就只能杀死i-1个骑士），计算这k+1个骑士的金币数之和。

我们就可以根据能力值排序这些骑士，存储它们现在的编号。能力值从小到大计算，将每个骑士前面的骑士塞到一个大根堆里去，每次从堆中取出min(i,k)个骑士，计算它们的和，计算完还要放回去：可以用一个栈来存储已经拿出来的骑士。最后再将这一个骑士放进去，做后面的工作就可以了。

## Code:

#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
struct node
{
int p,c,i;//p,c由题意，i是编号
friend bool operator <(node a,node b)//大根堆重载小于号
{
return a.c<b.c;
}
}kni[110000];
long long ans[110000];
priority_queue<node> q;
bool cmp(node a,node b)
{
return a.p<b.p;
}
int main()
{
memset(ans,0,sizeof(ans));
int n,k,cnt=0;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&kni[i].p);
kni[i].i=i;
}
node t;
for(int i=1;i<=n;i++)
scanf("%d",&kni[i].c);
sort(kni+1,kni+1+n,cmp);
for(int i=1;i<=n;i++)//枚举骑士
{
cnt=0;
ans[kni[i].i]=kni[i].c;
stack<node> s;
while(!q.empty()&&cnt<k)
{
t=q.top();
q.pop();
s.push(t);
cnt++;
ans[kni[i].i]+=t.c;
}
while(!s.empty())//放回去
{
t=s.top();
q.push(t);
s.pop();
}
q.push(kni[i]);
}
for(int i=1;i<=n;i++)
printf("%I64d ",ans[i]);//CF的longlong要I64d输出
return 0;
}

Subscribe

/* */