+ 问题描述

  + Link: https://codeforces.com/contest/994/problem/B

  + 描述

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.

  + 大意

    n个骑士, 每个骑士有战力p, 钱c, 每个骑士可以抢战力比他低的钱, 每个骑士最多抢k次, 对每个骑士求出最大钱数。

+ 输入

The first line contains two integers nn and kk (1≤n≤10^5,0≤k≤min(n−1,10))(1≤n≤10^5,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,…,pn (1≤pi≤10^9)(1≤pi≤10^9) — powers of the knights. All pipi are distinct.

The third line contains nn integers c1,c2,…,cn (0≤ci≤10^9)(0≤ci≤10^9) — the number of coins each knight has.

+ 输出

Print nn integers — the maximum number of coins each knight can have it only he kills other knights.

+ 样例

+ 网友思路

  1 贪心 +(优先队列)

  2 线段树

  n ...

+ 代码

【程序结果:用例未完全通过,本博文仅为暂存代码之目的】

  1. /*
  2. B. Knights of a Polygonal Table
  3. url:http://codeforces.com/problemset/problem/994/B
  4. 思路:
  5. step0.对骑士进行编号
  6. step1.按照权力值进行顺序排序
  7. step2.分别计算骑士所能杀死的其他骑士及其对应的金币值
  8. step3.按照编号重新恢复原始排序.
  9. */
  10. //#include<bits/stdc++.h>
  11. #include<iostream>
  12. #include<algorithm>
  13. using namespace std;
  14.  
  15. typedef struct Knight{
  16. int id;
  17. int power;
  18. int coins;
  19. int expected_coins;
  20. }Knight;
  21.  
  22. bool compare_id(Knight a, Knight b){
  23. if(a.id>b.id)
  24. return false;
  25. else return true;
  26. }
  27.  
  28. bool compare_power(Knight a, Knight b){
  29. if(a.power>b.power)
  30. return false;
  31. else return true;
  32. }
  33.  
  34. int main(){
  35. int n,k,id = 0;
  36. Knight *persons;
  37. scanf("%d %d", &n, &k);
  38. persons = (Knight*)malloc(n*sizeof(Knight));
  39. //printf("\n%d %d", k, n);
  40. for(int i=0;i<n;i++){
  41. persons[i].id = ++id;
  42. scanf("%d", &(persons[i].power));
  43. }
  44. for(int i=0;i<n;i++){
  45. scanf("%d", &(persons[i].coins));
  46. }
  47.  
  48. sort(persons, persons+n, compare_power);//顺序排序:<algorithm>
  49.  
  50. for(int i=0;i<n;i++){//分别计算各骑士能够杀死的人和可以获得的金币最大值
  51. persons[i].expected_coins = persons[i].coins;
  52. for(int j=1;j<=k;j++){
  53. if(i - j >= 0){
  54. persons[i].expected_coins += persons[i - j].coins;
  55. }
  56. }
  57. }
  58.  
  59. sort(persons, persons+n, compare_id);//依照id恢复原始排序
  60.  
  61. for(int i=0;i<n;i++){
  62. printf("%d%s", persons[i].expected_coins, ((i + 1) != n ?" ":"\n"));//test
  63. //printf("%d %d %d\n", persons[i].power, persons[i].coins, persons[i].expected_coins);//test
  64. }
  65.  
  66. return 0;
  67. }

  

[C++]Knights of a Polygonal Table(骑士的多角桌)的更多相关文章

  1. CF994B Knights of a Polygonal Table 第一道 贪心 set/multiset的用法

    Knights of a Polygonal Table time limit per test 1 second memory limit per test 256 megabytes input ...

  2. Knights of a Polygonal Table CodeForces - 994B (贪心)

    大意:n个骑士, 每个骑士有战力p, 钱c, 每个骑士可以抢战力比他低的钱, 每个骑士最多抢k次, 对每个骑士求出最大钱数 按战力排序后, 堆维护动态前k大即可 #include <iostre ...

  3. Codeforces 994B. Knights of a Polygonal Table

    解题思路 将骑士按力量从小到大排序,到第i个骑士的时候,前面的i-1个骑士他都可以击败,找出金币最多的k个. 用multiset存金币最多的k个骑士的金币数,如果多余k个,则删除金币数最小的,直到只有 ...

  4. [CF994B] Knights of a Polygonal Table - 贪心,堆

    有 n 个骑士想决战.每个骑士都有能力值(互不相同),且身上带有一些金币.如果骑士 A 的能力值大于骑士 B ,那么骑士 A 就可以杀死骑士 B ,并获得骑士 B 身上的所有金币.但就算是骑士也不会残 ...

  5. CodeForces 994B Knights of a Polygonal Table(STL、贪心)

    http://codeforces.com/problemset/problem/994/B 题意: 给出n和m,有n个骑士,每个骑士的战力为ai,这个骑士有bi的钱,如果一个骑士的战力比另一个骑士的 ...

  6. POJ2942 UVA1364 Knights of the Round Table 圆桌骑士

    POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...

  7. poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 9169   Accep ...

  8. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  9. POJ 2942 Knights of the Round Table

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10911   Acce ...

随机推荐

  1. Python 分布式进程

    #-*-coding:utf-8-*- '''分布式进程指的是将Process进程分不到多台机器上,充分利用多台机器的性能完成复杂的任务''' #服务器端 #--------------------- ...

  2. SQL表的基本操作

    1.创建表: create table 表名 ( [列名] [数据类型] [约束], [列名] [数据类型] [约束], ) 2.修改基本表: alert table[表名] [add 新列名 数据类 ...

  3. BZOJ3881 Divljak

    解:对被包含的那些串建AC自动机. 每次加一个串,就在AC自动机上面跑,可知能够跑到一些节点. 这些节点都是一些前缀的形式,我们跳fail树就是跳后缀,这样就能够得到所有能匹配的子串. 我们分别对AC ...

  4. A1118. Birds in Forest

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  5. 跟我一起写一个hello-world react组件并发布到npm

    第一步:初始化我们的配置 $ mkdir react-hello-world $ cd react-hello-world/ $ npm init -y 修改我们的package.json文件 //p ...

  6. Day037--Python--线程的其他方法,GIL, 线程事件,队列,线程池,协程

    1. 线程的一些其他方法 threading.current_thread()  # 线程对象 threading.current_thread().getName()  # 线程名称 threadi ...

  7. POJ 2253 Frogger (Floyd)

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:57696   Accepted: 18104 Descript ...

  8. Mac idea中git igenore设置

  9. eclipse 中新建文件报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Buil

    在eclipse中新建文件报错错误提示如下: The superclass "javax.servlet.http.HttpServlet" was not found on th ...

  10. win10的cmd中显示:telnet不是内部或外部命令也不是可运行的程序或批处理?

    win10的cmd中显示:telnet不是内部或外部命令也不是可运行的程序或批处理? 摘录自:https://blog.csdn.net/haijing1995/article/details/664 ...