有 n 个骑士想决战。每个骑士都有能力值(互不相同),且身上带有一些金币。如果骑士 A 的能力值大于骑士 B ,那么骑士 A 就可以杀死骑士 B ,并获得骑士 B 身上的所有金币。但就算是骑士也不会残忍过度,他们最多只会杀死 k 个骑士。对于每一位骑士,请你求出在杀掉所有他能杀的人(只有他能杀人别人不动)后他身上金币的最大值。

Solution

注意到“互不相同”这个弱化条件,考虑对能力值从小到大排序,那么每个骑士能且只能杀他前面的人。所以动态维护前 \(k\) 大和就好了

(题意有点神仙)

#include <bits/stdc++.h>
using namespace std; #define int long long
const int N = 100005; struct knight {
int p,c,ans,id;
bool operator < (const knight &x) const {
return c > x.c;
}
} a[N]; int n,k,ans[N]; bool cmp(const knight &x, const knight &y) {
return x.p < y.p;
} signed main() {
ios::sync_with_stdio(false);
cin>>n>>k;
for(int i=1;i<=n;i++) a[i].id=i;
for(int i=1;i<=n;i++) cin>>a[i].p;
for(int i=1;i<=n;i++) cin>>a[i].c;
sort(a+1,a+n+1,cmp);
priority_queue <knight> q;
int sum=0;
for(int i=1;i<=n;i++) {
a[i].ans = sum;
sum += a[i].c;
q.push(a[i]);
if(q.size()>k) {
sum -= q.top().c;
q.pop();
}
}
for(int i=1;i<=n;i++) ans[a[i].id]=a[i].ans+a[i].c;
for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
}

[CF994B] 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. [C++]Knights of a Polygonal Table(骑士的多角桌)

    [程序结果:用例未完全通过,本博文仅为暂存代码之目的] /* B. Knights of a Polygonal Table url:http://codeforces.com/problemset/ ...

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

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

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

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

  5. Codeforces 994B. Knights of a Polygonal Table

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

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

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

  7. POJ 2942 Knights of the Round Table

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

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

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

  9. 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)

    Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...

随机推荐

  1. Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信

    场景 点击拨打电话按钮,跳转到拨打电话页面 点击发送短信按钮,跳转到发送短信页面 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程 ...

  2. Android 日期选择框 简洁常用

    效果 核心代码 >方法 /** * @description 选择日期弹出框 * @param listener 选择日期确定后执行的接口 * @param curDate 当前显示的日期 * ...

  3. 百度大脑发布“AI开发者‘战疫’守护计划”,AI支援抗疫再升级

    面对新冠肺炎疫情,AI开发者们正在积极运用算法.算力.软件等“武器”助力抗疫.针对开发者们在疫情防控期间的开发与学习需求,2月6日,百度大脑推出“AI开发者‘战疫’守护计划”, 正在进行疫情防控相关应 ...

  4. DEVIL MAY CRY V:《鬼泣5》

    “又是一个带孝子的故事”

  5. stlink 下载报错:Error Flash Download failed - "Cortext-M0+"

    stlink 下载报错:Error Flash Download failed - "Cortext-M0+" 解决方法: STM32 ST-LINK Utility 用这个软件把 ...

  6. go 并发编程

    进程 线程 协程 设置golang运行cpu数 1.主线程和协程同时执行 package main import ( "fmt" "strconv" " ...

  7. 自用浏览器的 User-agent 表

    浏览器版本号见请求. 浏览器 User-agent Internet Explorer Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11. ...

  8. [Python] 前程无忧招聘网爬取软件工程职位 网络爬虫 https://www.51job.com

    首先进入该网站的https://www.51job.com/robots.txt页面 给出提示: 找不到该页 File not found 您要查看的页已删除,或已改名,或暂时不可用. 请尝试以下操作 ...

  9. Wilson's Theorem

    ProofsSuppose first that $p$ is composite. Then $p$ has a factor $d > 1$ that is less than or equ ...

  10. ArcMap 新建空白图层,并添加元素

    想要添加新的图层,不能从File 里找,那里都是添加数据,貌似ArcGIS的定位是对数据的处理. 比如导入其他格式文件,或者导入对应的数据文件.excel csv cad 等,在此基础上进行分析,而不 ...