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
题意:
就是一个人他可以杀掉比他pow力量值小的人,从而吃掉杀死的那个人的金币,从而自己的金币会增长,但是杀死人的数量有一个上限就是k值,本题求每个人的最多可以拥有多少金币
题解:
1.本题的重点就是比自己pow值低的都可以被杀死,但是由于有一个上限值k,所以就要杀死其中money最大的前k位(就要对pow值和money值都要用sort快排排序)
2.这个题暴力开两个数组分别排序两层for循环也可以做,但是会一直超时
用两层for循环可以,但是要注意第二层嵌套在里面的for循环只能是k次
所以选择用STL里面的multiset容器(看的网上大佬的思路,自己一直超时)
3.由于multiset容器可以自动排序,所以选择用,还可以遍历
代码:
#include <bits/stdc++.h>
using namespace std;
#define speed_up ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
struct num
{
long long pow;
long long money1;
long long money2;
long long d;
};
bool comp(num& n1,num& n2)//对pow值有小到大进行排序
{
return n1.pow<n2.pow;
}
num nn1[100001];
long long num1[100001];
int main()
{
speed_up;
long long n,k;
cin>>n>>k;
multiset<long long>s;
for(long long i=0;i<n;i++)
{
cin>>nn1[i].pow;
nn1[i].d=i;
}
for(long long i=0;i<n;i++)
{
cin>>nn1[i].money1;
nn1[i].money2=nn1[i].money1;
}
sort(nn1,nn1+n,comp);
for(long long i=0;i<n;i++)
{
for(auto it=s.begin();it!=s.end();it++)//保证嵌套在第二层的for循环只有k次
//这样可以保证不超时
{
nn1[i].money2+=*it;
}
s.insert(nn1[i].money1);//multiset会自动排序从小到大
//输入的都是比后面的人pow值小的人的money值
while(s.size()>k)
{
s.erase(s.begin());//删除头的最小的元素
//保证嵌套在第二层的for循环只有k次
}
num1[nn1[i].d]=nn1[i].money2;
}
cout<<num1[0];
for(long long i=1;i<n;i++)
{
cout<<" "<<num1[i];
}
cout<<endl;
}