有 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. java8 常用代码

    1. 使用java8 提取出 list 中 bean 的某一属性 public static void main(String[] args) { List<Student> stuLis ...

  2. idea创建基于maven的web项目

    1.点击create new project,选择maven,点击next 2.输入项目信息,点击finish 3.进入项目后,点击菜单File->Project Structure开始配置项目 ...

  3. StarUML之六、StarUML规则与快捷键

    本章内容参考官网即可,不做详细说明,实践出真知! starUMl规则主要是在模型设计的约束条件 https://docs.staruml.io/user-guide/validation-rules ...

  4. redis中key键操作

    keys */查看所有的key remoteSelf:1>select 0 "OK" remoteSelf:0>keys * 1) "SUBCRIBEMAP& ...

  5. MYSQL使用group by,如何查询出总记录数

    比如有这样一条SQL,根据t.user_id,t.report_date两个字段统计 这样前端页面能展示数据,但往往需要一个总记录数,在有分页的情况下用到 一种解决方法是在外面套一层,然后对其coun ...

  6. 00.JS前言

    前言: 学习一门编程语言的基本步骤(01)了解背景知识 1.了解背景知识   1)什么是 JavaScript 语言?     JavaScript 是一种轻量级的脚本语言.所谓“脚本语言”(scri ...

  7. VBA-UTF-8文件的操作

    1.读入UTF-8文件    Dim value As String   Dim vLines As Variant Call ReadCsvUTF_8(vFile.Path, value) vLin ...

  8. 部署Nexus作为docker的私有仓库

    目录 Docker搭建Nexus私有仓库... 1 一.安装部署... 1 1.安装... 2 2.访问网页端... 2 二.配置使用... 2 1.创建本地仓库... 2 2.docker配置... ...

  9. 虚拟机(linux)怎么上网

    问题描述:本机并没有显示虚拟机(linux)的虚拟网卡,那能不能用虚拟机上网呢,如果要让本机显示出虚拟机的虚拟网卡会有一万步各种安装卸载,那么,在现有条件下可不可以上网呢,答案是可以的. 解决方案: ...

  10. Redis入门-01

    目录 使用场景 支持的数据类型 主从复制 原理 配置 哨兵机制 持久化 RDB(Redis Database) AOF(Append Only File) redis(Remote DIctionar ...