Knights of a Polygonal Table CodeForces - 994B 贪心+思维

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

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

The third line contains n integers c1,c2,…,cn (0≤ci≤109) — 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.

Examples
Input
4 2
4 5 9 7
1 2 11 33
Output
1 3 46 36
Input
5 1
1 2 3 4 5
1 2 3 4 5
Output
1 3 5 7 9
Input
1 0
2
3
Output
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.

思路详见代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <functional>
using namespace std;
typedef long long ll;
int const N=1e5+100;
multiset<ll>ms;//multiset 能自动排序,但是能有重复的数据,因为题目中给出的coins可以重复
ll n,k;
struct Node
{
	ll pow;//用来存储每个战士的力量 
	ll coin;//每个战士的coins 
	ll pos;//由于输出要按照原先的顺序,所以记录一下位置 
}a[N];
ll ans[N];
bool cmp(Node x,Node y)
{
	return x.pow<y.pow;
}
int main()
{
	scanf("%lld%lld",&n,&k);
	//正常读入结构体 
	for(int i=0;i<n;i++)
	{
		scanf("%lld",&a[i].pow);
		a[i].pos=i;
	}
	for(int i=0;i<n;i++)
	{
		scanf("%lld",&a[i].coin);
	}
	//排序   按照力量从小到大排序 
	sort(a,a+n,cmp);
	
	// 这题的思路在于,每次把coin丢到一个multiset里,超过k个数了,说明要删了,就把最小的删了,然后剩余的保证了是最大值。 
	for(int i=0;i<n;i++)
	{
		ans[a[i].pos]=a[i].coin;
		for(auto x:ms)
		{
			ans[a[i].pos]+=x;
		}
		ms.insert(a[i].coin);
		while(ms.size()>k)
		{
			ms.erase(ms.begin());
		}
	}
	for(int i=0;i<n;i++)
	{
		printf("%lld%c",ans[i],i==n-1?'\n':' ');
	} 
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值