解题思路

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

我就是因为没有用multiset在最后5分钟被hack了。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,k;
//输入,p表示力量,c表示金币,idx表示输入时的位置
struct node{
ll p;ll c;int idx;
}a[500050]; bool cmp(node x, node y){
return x.p < y.p;
}
ll ans[500050];
int main(){
ios::sync_with_stdio(false);
cin >> n >> k;
//输入并按力量从小到大排序
for(int i = 1;i <= n; ++i) cin >> a[i].p, a[i].idx = i;
for(int i = 1;i <= n; ++i) cin >> a[i].c;
sort(a+1,a+1+n,cmp); multiset <int> s; //存第i个骑士可以击败的不超过k个骑士的金币数
for(int i = 1;i <= n; ++i){
ans[a[i].idx] = a[i].c;
for(auto t:s){
ans[a[i].idx] += t;
}
s.insert(a[i].c);
//如果将第i个骑士的金币数插入之后大于k个数字,就删除到只有k个
while(s.size() > k){
s.erase(s.begin());
}
}
for(int i = 1;i <= n; ++i) cout << ans[i] << " ";
cout << endl;
return 0;
}

Codeforces 994B. Knights of a Polygonal Table的更多相关文章

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

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

  2. [C++]Knights of a Polygonal Table(骑士的多角桌)

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

  3. 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 ...

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

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

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

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

  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. 移植开源QT软件-SameGame

    前言: QML是一种描述性的脚本语言,文件格式以.qml结尾.语法格式非常像CSS(参考后文具体例子),但又支持javascript形式的编程控制.我个人认为它结合了QtDesigner UI和QtS ...

  2. java学习笔记3——异或

    异或原理: 转换两个字符或数为2进制的ASCII码,再按位异或,即 0001 0001 ---> 0000 0000 0000 ---> 0000 0001 0000 ---> 00 ...

  3. spring中log4j的使用---转载

    原文链接:http://www.codeceo.com/article/log4j-usage.html 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供 ...

  4. mongodb主从副本集配置

    创建路径: mkdir -p /datassd/mongo_20011/{data,conf,log}     配置文件示例: #mongo.conf dbpath=/datassd/mongo/da ...

  5. win 2016 添加系统组件注册表,

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ServerManager\ServicingS ...

  6. 优动漫PAINT(clip studio paint)怎么画一幅水墨竹子图

    今天小编分享使用优动漫PAINT绘制一个水墨竹子教程,绘画的过程中我只用到了两个笔刷,即钢笔模式下的“美术字”和“效果线专用”,并且全程鼠标绘制哦,所以生疏的笔触效果大家见谅,没有数位板的小伙伴不妨试 ...

  7. 如何设置root登录(滴滴云)

    购买云服务器DC2后,首先进入控制台,找到对应的EIP,如图1所示: 图1 然后使用您终端电脑的远程登录软件登录DC2(Windows系统推荐xshell,Mac电脑推荐iterm2).默认登录用户名 ...

  8. vue学习笔记——脚手架安装

    项目启动:npm run build 脚手架 vue cli vue cli --2 安装命令 #全局安装 npm install --global vue-cli #卸载vue-cli  npm u ...

  9. sublime Text3的使用

    sublime text百度百科: Sublime Text 是一个代码编辑器,也是HTML和散文先进的文本编辑器.Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python ...

  10. ELK介绍及搭建 Elasticsearch 分布式集群

    上:https://blog.51cto.com/zero01/2079879 下:https://blog.51cto.com/zero01/2082794