Codeforces994B——Knights of a Polygonal Table

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?
ou 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.
Eamples
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 <algorithm>
#include <set>
using namespace std;
const int N=100050;
int n,k;
struct node{
    int i;
    int p;
    int c;
    long long res;
}a[N];
bool cmp(node a,node b){
    return a.p<b.p;
}
bool cmp2(node a,node b){
    return a.i<b.i;
}
int main(void){
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i].p);
        a[i].i=i;
    }
    for(int i=0;i<n;i++){
        scanf("%d",&a[i].c);
        a[i].res=a[i].c;
    }
    sort(a,a+n,cmp);
    // for(int i=0;i<n;i++){
   
    // printf("%d %d\n",a[i].p,a[i].c);
    // }
    multiset<int,greater<int>> s;
    for(int i=0;i<n;i++){
        int cnt=0;
        for(auto t:s){
            cnt++;
            if(cnt>k){
                break;
            }
            a[i].res+=t;
        }
        s.insert(a[i].c);
    }
    sort(a,a+n,cmp2);
    for(int i=0;i<n-1;i++){
        printf("%lld ",a[i].res);
    }
    printf("%lld\n",a[n-1].res);
    return 0;
}
全部评论

相关推荐

04-08 13:31
已编辑
门头沟学院 前端工程师
D0cC:京东营收1万多亿人民币,阿里9000多亿,虽然他俩利润都没腾讯和字节多,但是很恐怖了啊,负担了多少打工人的薪水
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
繁华的街道两旁,湿漉漉的下午,两个青涩的脸庞互相张望。宽大卫衣下娇小的她,向我奔来。不约而同的卫衣,斯文的半框眼镜掩饰着一个穷臭屌丝气息。这是我和我牛爱网第一死忠粉兼专属女嘉宾最初的见面。火速恋爱,但是没有所谓的快节奏,相识半年,还是一样的热恋。吃着肉夹馍坐过西安的小三轮洱海边自行车的气球胖吃着她最喜欢的酸酸水果和小乳扇在南山某店爷爷穿孙子衣服,摸肥猫就算我在忙也要抽出时间陪她去吃他喜欢的漂亮饭生活总是平凡,但平凡不平淡还记得见面第一件事儿:“我去上个厕所。”现在早上第一件事儿:“拉*”第一次上我车的她:“我可以坐副驾吗?”现在的她:“老子把jio翘到上面得得挡到你后视镜。”这小孩,虽然花了我...
Stan_蹒跚者:确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务