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 (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

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.

http://codeforces.com/problemset/problem/994/B

题意:给你n个人,每个人都有一个力量值和一些金钱

现在假设一个人可以攻击任意人,如果攻击力量值比他低的人

就可以得到那个人的金钱,输出每个人攻击最多k个人后最多可以有

多少金钱.

这个题是真的坑

最后FST了,55555,比赛的时候没有注意到一个小知识点

求k个人可以得到最多的钱的时候如果是用二叉平衡树的set容器的话

如果容器中有重复的元素的话会自动去重

multiset集合容器:

------ 和set的区别:set容器中所有的元素必须独一无二,而multiset容器中元素可以重复

但是由于不太会用multiset 所以使用vector,每次都排个序,一样的结果

思路在代码中

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+;
int n,k;
struct node {
int p,id;
} a[maxn];
bool cmp(node a,node b) {
return a.p<b.p;
}
vector<long long> v;
long long c[maxn];
long long ans[maxn];
int main() {
ios::sync_with_stdio(false);
cin>>n>>k;
for(int i=; i<n; i++) {
cin>>a[i].p;
a[i].id=i;
}
for(int i=; i<n; i++) {
cin>>c[i];
}
//k=0时特殊处理
if(k==) {
for(int i=; i<n; i++) {
printf("%d ",c[i]);
}
printf("\n");
return ;
} else {
//对力量值排序
sort(a,a+n,cmp);
ans[a[].id]=;
long long sumk=;//前k大和
for(int i=; i<n; i++) {
if(v.size()<k) {
/在k个数之前时 加上前面这些数的cost
v.push_back(c[a[i-].id]);
sumk+=c[a[i-].id];//前i个数的和
} else if(c[a[i-].id]>v[]) {
//前面数中最大的k个数的和
sumk+=c[a[i-].id]-v[];//更新最大值
v[]=c[a[i-].id];//更新第k大值
}
sort(v.begin(),v.end());//对于k个数重新排序
ans[a[i].id]=sumk;
}
for(int i=; i<n; i++) {
printf("%lld ",ans[i]+c[i]);
}
printf("\n"); }
return ;
}

补上multiset的做法

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
int p[maxn],coin[maxn]; bool cmp(int x,int y){
return p[x]<p[y];
} ll ans[maxn];
int id[maxn]; int main(){
ios::sync_with_stdio(false);
cin.tie();
int n,k;
while(cin>>n>>k){
for(int i=;i<n;i++){
cin>>p[i];
}
for(int i=;i<n;i++){
cin>>coin[i];
}
for(int i=;i<n;i++){
ans[i]=coin[i];
id[i]=i;
}
sort(id,id+n,cmp);
multiset<int> s;
multiset<int>::iterator it;
for(int i=;i<n;i++){
int j=;
it=s.end();
if(s.size()!=){
for(it--;j<k;j++,it--){
ans[id[i]]+=*it;
if(it==s.begin()) break;
}
}
s.insert(coin[id[i]]);
}
for(int i=;i<n;i++){
cout<<ans[i]<<" ";
}
cout<<endl;
}
}

code forces 994B的更多相关文章

  1. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  2. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  3. Code Forces 833 A The Meaningless Game(思维,数学)

    Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...

  4. Code Forces 543A Writing Code

    题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...

  5. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  6. code forces 382 D Taxes(数论--哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  7. code forces Watermelon

    /* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...

  8. code forces Jeff and Periods

    /* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostrea ...

  9. Code Forces Gym 100971D Laying Cables(单调栈)

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. 批量更新python库

    import pip from subprocess import call for dist in pip.get_installed_distributions(): try: call(&quo ...

  2. python简介,数据类型,input,if语句

      1. python的起源 python的创始人为吉多·范罗苏姆(龟叔Guido van Rossum),1989年的圣诞节期间,龟叔为了在阿姆斯特丹打发时间 决心开发一个新的脚本程序解释器,作为A ...

  3. python——字符串的操作判断

    s为字符串 s.isalnum()  所有字符都是数字或者字母,为真返回 Ture,否则返回 False. s.isalpha()   所有字符都是字母,为真返回 Ture,否则返回 False. s ...

  4. P1198 [JSOI2008]最大数【树状数组】

    题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: L 不超过当前数列的长度. (L &g ...

  5. Pandas库入门

    pandas库的series类型

  6. Oozie 实战之 shell

    说明:使用 shell action 执行 shell 脚本 hive-select-test.sh 来通过已经配置好的 Hive -f 来执行 HQL 查询脚本文件 select.sql 1.创建脚 ...

  7. 4.Mongodb数据查询2

    1.limit &skip (1)Limit 方法limit():用于读取指定数量的文档 语法: db.集合名称.find().limit(NUMBER) 参数NUMBER表示要获取文档的条数 ...

  8. django之python3.4及以上连接mysql的一些问题记录

    首先,祭出大杀器whl https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient django1.x与django2.x 在项目的写法上有一些区别 ...

  9. Maven学习 (五) Elipse中发布一个Maven项目到Tomcat

    对于maven初学者的我,经常遇到一个问题就是,maven项目创建成功后,本来已经添加了jar的依赖,但是发布到Tomcat中就是没有jar包存在, 启动Tomcat总是报没有找到jar包,可项目结构 ...

  10. 《Cracking the Coding Interview》——第4章:树和图——题目9

    2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...