Knights of a Polygonal Table CodeForces - 994B(思维+贪心 优先队列)

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,有n个骑士,每个骑士的战力为ai,这个骑士有bi的钱,如果一个骑士的战力比另一个骑士的战力高,那么,他就可以夺取这个骑士的钱,但是每个骑士最多夺取k个人,问,这些骑士最多可以获得多少钱。


思路:由于每个骑士都要从比他战力低的人中选出k个(如果有k个)人夺取他们的钱,那么肯定要夺取前k大的骑士的钱了!,所以,我们选择用优先队列来做!从战力最小的开始入队。每次取出前k个可掠夺的骑士!这样就保证了队列中的骑士都是可以被掠夺的,并且,取出的k个骑士的钱一定是前k大的。(要注意的是,每次取出队列中的数,还要重新加回去!!因为是求最大可能!)

#include "iostream"
#include "algorithm"
#include "queue"
#include "map"
using namespace std;
typedef long long ll;
const int Max=1e5+10;
typedef pair<int,int> P;
bool cmp(P a,P b){ return a.second<b.second;}
P p[Max],tmp[15];
int a[Max];
map<int,ll> mp;
int main()
{
    ios::sync_with_stdio(false);
    priority_queue<P> que;//对于pair 优先队列自动按firs从大到小取!
    int n,k,t,x,cnt;
    mp.clear();
    cin>>n>>k;
    for(int i=0;i<n;i++) {cin>>p[i].second;a[i]=p[i].second;}//a数组是用来保证输出和输入对应的
    for(int i=0;i<n;i++) {cin>>p[i].first;mp[p[i].second]=p[i].first;}
    sort(p,p+n,cmp);//按战力升序排序
    for(int i=0;i<n;i++){
        t=k,cnt=0;
        while(que.size()&&t--){//选k个战力比自己小的骑士掠夺
            P x=que.top();
            que.pop();
            mp[p[i].second]+=x.first;
            tmp[cnt++]=x;//记录被掠夺的骑士,后面需要重新入队
        }
        que.push(p[i]);//自己入队
        for(int j=0;j<cnt;j++) que.push(tmp[j]);//重新入队
    }
    for(int i=0;i<n;i++) cout<<mp[a[i]]<<" "; cout<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80717942