有 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. Mybatis注解开发多表一对一,一对多

    Mybatis注解开发多表一对一,一对多 一对一 示例:帐户和用户的对应关系为,多个帐户对应一个用户,在实际开发中,查询一个帐户并同时查询该账户所属的用户信息,即立即加载且在mybatis中表现为一对 ...

  2. java 企业 网站源码 模版 屏幕自适应 有前后台 springmvc SSM 生成静态化

    前台: 支持四套模版, 可以在后台切换 系统介绍: 1.网站后台采用主流的 SSM 框架 jsp JSTL,网站后台采用freemaker静态化模版引擎生成html 2.因为是生成的html,所以访问 ...

  3. Spring Boot源码(六):Bean的创建详解

    继续之前的项目: People加上无参构造方法: @Component public class People { // private User user; public People(){ Sys ...

  4. maven的核心概念——仓库

    第十章仓库 10.1 分类 [1]本地仓库:为当前本机电脑上的所有Maven工程服务. [2]远程仓库 (1)私服:架设在当前局域网环境下,为当前局域网范围内的所有Maven工程服务. (2)中央仓库 ...

  5. Python、Django、Celery中文文档分享

    1.Python:链接:https://pan.baidu.com/s/12uzxbI-nMkpF7aMa966bTQ 密码:i1x9 2.Django:链接:https://pan.baidu.co ...

  6. 洛谷P1551 亲戚

    洛谷P1551 亲戚 原题 题目背景 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. 题目描述 规定:x和y是亲戚,y和z是 ...

  7. 剑指offer-面试题22-链表中倒数第k个节点-双指针

    /* 题目: 链表中倒数第k个节点 */ /* 思路: 考虑边界条件:链表为空,k值超过链表长度. 双指针: q指针指向第k个节点,p指针指向第1个节点. q指针指向最后一个节点,p指针指向倒数第k个 ...

  8. simmon effect : build the funcion of trail list

    #the real experiment for simon effect #load the library which is our need import pygame import sys i ...

  9. js微信禁用右上角的分享按钮,和vue中微信页面禁用右上角的分享按钮的问题

    1.隐藏微信网页右上角的按钮 document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { // 通过下面这个 ...

  10. sqli-labs5-10(全程sqlmap)

    sqlmap注入教程:https://www.cnblogs.com/ichunqiu/p/5805108.html 前五关直接可以用默认的sqlmap语法跑: python sqlmap.py -u ...