有 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. Linux内核的LED设备驱动框架【转】

    /************************************************************************************ *本文为个人学习记录,如有错 ...

  2. opencv —— boundingRect、minAreaRect 寻找包裹轮廓的最小正矩形、最小斜矩形

    寻找包裹轮廓的最小正矩形:boundingRect 函数 返回矩阵应满足:① 轮廓上的点均在矩阵空间内.② 矩阵是正矩阵(矩形的边界与图像边界平行). Rect boundingRect(InputA ...

  3. DataX的使用——大数据同步技术

    准备工作: 1.视频教学http://113.31.104.47/portal/#/course/dashboard/b34d160db64624732ef152a1118af11a 2.DataX的 ...

  4. Python+PyQT5的子线程更新UI界面的实例《新手必学》

    今天小编就为大家分享一篇Python+PyQT5的子线程更新UI界面的实例,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧子线程里是不能更新UI界面的,在移动端方面.Android的U ...

  5. VSCode 完美整合前后端框架(angular2+.NET core)

    首先打开命令行查看本地.NET版本. 通过命令行安装模板. dotnet new --install Microsoft.AspNetCore.SpaTemplates::* 创建demo目录,并用v ...

  6. VUE中集成echarts时 getAttribute of null错误

    错误 错误场景一: 错误提示: 在运行Vue项目时出现了上述错误,出现该错误的原因是Echarts的图形容器还未生成就对其进行了初始化所造成的,代码如下: // 基于准备好的dom,初始化echart ...

  7. switch 语句 总结笔记

    1.switch 语句 语法: switch(expression) { case value1 : statement1; break; case value2 : statement2; brea ...

  8. css3神奇的圆角边框、阴影框及其图片边框

    css3圆角,建议IE10以上 如果border-radius 单位是百分比,则参考为自身宽高,因此当宽高不一致时,圆角为不规则形状 如果border-radius 为50%,则为椭圆:当宽高一致时, ...

  9. 数据库->神奇的concat_ws函数

    神奇的concat_ws函数 了解到concat_ws函数起因是因为:朋友给我发了一份面试题,其中有一句看似很简单的查询,但是我就是没做出来.如下图:   经查阅资料了解通过两个字段拼接并且用“_”进 ...

  10. npm run build 打包后空白页解决问题两种情况

    问题一:assetsPublicPath配置错误 解决办法:打开config/index.js文件 build:{        // assetsPublicPath: '/'        ass ...