解题思路

  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. C#自定义控件实现控件随窗口大小改变

    1.新建用户控件,取名MyForm. 2.将默认的UserControl改成Form 3.在类中添加以下代码 private float X, Y; //获得控件的长度.宽度.位置.字体大小的数据 p ...

  2. 图片放大不失真软件PhotoZoom的工具栏

    PhotoZoom是一款极其简单的图片无损放大工具,简单几即可渲染出完美的放大照片,呈现无与伦比的画质效果.虽然简单,菜单和面板的功能很少,但却是设计师的必备神器,因为其简单易用性,它的软件菜单命令和 ...

  3. ubuntu18.0安装redis

    ubuntu版本:Ubuntu-Server 10.04.1 LTS redis版本:4.0.9 安装 使用apt安装 sudo apt-get update sudo apt-get install ...

  4. 【转】Oracle基础结构认知—初识oracle 礼记八目 2017-12-12 21:19:30

    Oracle服务器(oracle server)由实例和数据库组成.其中,实例就是所谓的关系型数据库管理系统(Relational Database Management System,RDBMS), ...

  5. 从U盘安装CentOS7.3教程(转载)

    0.准备工作: 一台没系统的普通电脑u盘一个(大于1G,最小安装的话不超过1G,根据选择系统大小匹配U盘即可) CentOS7.3 iso文件一个UltraISO工具 1.制作U盘 ①使用UltraI ...

  6. Linux常用命令速查

    索引表格 命令 功能简述 目录与文件基本操作 pwd 显示当前目录 ls 列出目录和文件名称 cp 复制文件或目录 mv 移动或更名现有的文件或目录 rm 删除文件或目录 mkdir 新建目录 rmd ...

  7. linux下RTP编程(使用JRTPLIB)(转)

    流媒体指的是在网络中使用流技术传输的连续时基媒体,其特点是在播放前不需要下载整个文件,而是采用边下载边播放的方式,它是视频会议.IP电话等应用场合的技术基础.RTP是进行实时流媒体传输的标准协议和关键 ...

  8. JavaScript之Ajax技术

  9. robot Framework选择单选框

  10. 使用githug游戏提高git水平