994B - Knights of a Polygonal Table

B. Knights of a Polygonal Table
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 kk 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 nn and kk (1n105,0kmin(n1,10))(1≤n≤105,0≤k≤min(n−1,10)) — the number of knights and the number kk from the statement.

The second line contains nn integers p1,p2,,pnp1,p2,…,pn (1pi109)(1≤pi≤109) — powers of the knights. All pipi are distinct.

The third line contains nn integers c1,c2,,cnc1,c2,…,cn (0ci109)(0≤ci≤109) — the number of coins each knight has.

Output

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

Examples
input
Copy
4 2
4 5 9 7
1 2 11 33
output
Copy
1 3 46 36 
input
Copy
5 1
1 2 3 4 5
1 2 3 4 5
output
Copy
1 3 5 7 9 
input
Copy
1 0
2
3
output
Copy
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=2k=2 other knights. It is optimal to kill the second and the fourth knights: 2+11+33=462+11+33=46.
  • The fourth knight should kill the first and the second knights: 33+1+2=3633+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个人里面依次去找力量值比他小的并且金币最多的人杀掉。用c++写了好久,写到后面,发现有个不好处理的地方就是你算出每个骑士最大金币后,还要按照原来骑士的序列依次输出每个骑士最多的金币。然后没处理出来...看大佬们好多用优先队列,不过还是python大法好啊....

#include<bits/stdc++.h>
using namespace std;
int n,k,a[100001],b[100001],p[100001]; long long A[100001];
priority_queue<int,vector<int>,greater<int> > pq;
bool cmp(int i,int j){return a[i]<a[j];}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i) scanf("%d",a+i);
	for(int i=1;i<=n;++i) scanf("%d",b+i);
	for(int i=1;i<=n;++i) p[i]=i;
	sort(p+1,p+n+1,cmp);
	long long S=0;
	for(int I=1;I<=n;++I){
		int i=p[I];
		A[i]=S+=b[i];
		pq.push(b[i]);
		if(pq.size()>k) S-=pq.top(), pq.pop();
	}
	for(int i=1;i<=n;++i) printf("%I64d ",A[i]);
	return 0;
}
R=lambda:map(int,input().split())
n,k=R()
l=[]
ans=[0]*n
s=0
a=sorted(zip(R(),R(),range(n)))
for p,c,i in a:
    s+=c;ans[i]=s
    l=sorted(l+[c])
    if len(l)>k:
       s-=l[0]
       del l[0]
for i in ans:
   print(i,end=' ')
           


猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/80733891