CodeForces - 994B Knights of a Polygonal Table(STL 优先队列)

VJ 原题

原题:

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 (1≤n≤105,0≤k≤min(n−1,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 (1≤pi≤109)(1≤pi≤109) — powers of the knights. All pipi are distinct.

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

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 

题意:

一堆骑士,互相杀。当一个骑士的战力值大于等于另一个骑士时,这个骑士就能把他杀掉。但是每个骑士都还是很有良心的(呵呵),他们最多杀死k个骑士。每个骑士身上都有钱,把别人杀了之后就可以全都拿走。现在每个骑士都在算如果只有自己去杀别人,自己最多能拿到多少钱。

解决过程:

真是一道好现实的题啊哈哈哈哈。思路很简单,就是先选出来自己能杀那些,挑着有钱的杀呗(讽刺仇富,举报了)。一开始我是直接扔到优先队列里边,每个都操作一遍。但是TLE了。

于是我想,我先把骑士们按照战力值从小到大排序,这样每次考虑下一个人的时候,他能杀掉的骑士就是前n-1个再加上第n-1个(哈哈哈刚才杀别人那个现在要被杀了)。还有一个问题,就是每次只能杀k个人,所以可能杀掉k个人之后队列里面还有好多人,如果判断第n个人的时候重新把前n-1个人全都入队,很浪费之间,所以就把第n-1个人杀掉的k个人再入队,再把第n-1个人入队,现在优先队列中的人就是第n个人能杀的全部的人了。优先队列自定义优先级,按照谁的钱多谁优先级高的原则,pop出来min(k, pq.size())个人,把钱加起来,就行啦。

当然用优先队列可以,用multiset也是可以的,并且更简单一点,都不用再push回去。不过运行时间差不多,所以我就没用multiset再写一遍(其实是懒)。

#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <list>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <vector>
//#include <bits/stdc++.h>
using namespace std;

#define INF 0x3f3f3f3f
#define PI 3.141592653579
#define FRER() freopen("input.txt" , "r" , stdin);
#define FREW()  freopen("output.txt" , "w" , stdout);
typedef long long ll;
const int maxn = 100000+1000;
struct soldier
{
    ll pow, mon, get, id;
    soldier():get(0) {}
}  ss[maxn];
bool operator<(soldier a, soldier b)
{ return a.mon < b.mon; }
bool cmp1(soldier a, soldier b)
{ return a.pow < b.pow; }
soldier temp[maxn];
ll res[maxn];
int main()
{
    ll n, k;
    scanf("%lld %lld", &n, &k);
    for(int i = 0 ; i < n ; i++)
    {
        scanf("%lld", &ss[i].pow);
        ss[i].id = i;
    }
    for(int i = 0 ; i < n ; i++)
        scanf("%lld", &ss[i].mon);
    sort(ss, ss+n,cmp1);
    priority_queue<soldier> pq;
    for(int i = 0 ; i < n ; i++)
    {
        if(i != 0)
            pq.push(ss[i - 1]);
        ll cnt = 0;
        while(!pq.empty() && cnt < k)
        {
            temp[cnt] = pq.top();
            ss[i].get += temp[cnt].mon;
            pq.pop();
            cnt++;
        }
        for(int j = 0 ; j < cnt ; j++)
            pq.push(temp[j]);
        res[ss[i].id] =  ss[i].get + ss[i].mon;
    }
    for(int i = 0 ; i < n ; i++)
        if(i) printf(" %lld", res[i]);
        else printf("%lld", res[i]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值