CF994B Knights of a Polygonal Table 第一道 贪心 set/multiset的用法
1 second
256 megabytes
standard input
standard output
Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than kk other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins.
Now each knight ponders: how many coins he can have if only he kills other knights?
You should answer this question for each knight.
The first line contains two integers nn and kk (1≤n≤105,0≤k≤min(n−1,10))(1≤n≤105,0≤k≤min(n−1,10)) — the number of knights and the number kkfrom the statement.
The second line contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤109)(1≤pi≤109) — powers of the knights. All pipi are distinct.
The third line contains nn integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤109)(0≤ci≤109) — the number of coins each knight has.
Print nn integers — the maximum number of coins each knight can have it only he kills other knights.
4 2
4 5 9 7
1 2 11 33
1 3 46 36
5 1
1 2 3 4 5
1 2 3 4 5
1 3 5 7 9
1 0
2
3
3
Consider the first example.
- The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has.
- The second knight can kill the first knight and add his coin to his own two.
- The third knight is the strongest, but he can't kill more than k=2k=2 other knights. It is optimal to kill the second and the fourth knights:2+11+33=462+11+33=46.
- The fourth knight should kill the first and the second knights: 33+1+2=3633+1+2=36.
In the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own.
In the third example there is only one knight, so he can't kill anyone.
题目给出n个knight,每个knight最多可以杀死k个其余的knight,第一行是每个knight的攻击力,第二行是每个knight的价值。
每个knight杀死其他knight之后可以增加被杀者的价值。
问每个knight最多可以拥有的价值。
按每个knight的攻击力排序,这样我们可以按攻击力从小到大遍历,计算时我们先加上所有的knight价值,用一个multiset保存下来(自动从小到大顺序保存可重复的所有值)。当multiset中的个数大于k时,我就减去第一个(最小的值),这样我们可以保证每个knight杀死了他所能杀死的所有knight中价值最大的k个。
附上multiset的用法:
3.1 构造、拷贝、析构
|
操作 |
效果 |
|
set c |
产生一个空的set/multiset,不含任何元素 |
|
set c(op) |
以op为排序准则,产生一个空的set/multiset |
|
set c1(c2) |
产生某个set/multiset的副本,所有元素都被拷贝 |
|
set c(beg,end) |
以区间[beg,end)内的所有元素产生一个set/multiset |
|
set c(beg,end, op) |
以op为排序准则,区间[beg,end)内的元素产生一个set/multiset |
|
c.~set() |
销毁所有元素,释放内存 |
|
set<Elem> |
产生一个set,以(operator <)为排序准则 |
|
set<Elem,0p> |
产生一个set,以op为排序准则 |
3.2 非变动性操作
|
操作 |
效果 |
|
c.size() |
返回当前的元素数量 |
|
c.empty () |
判断大小是否为零,等同于0 == size(),效率更高 |
|
c.max_size() |
返回能容纳的元素最大数量 |
|
c1 == c2 |
判断c1是否等于c2 |
|
c1 != c2 |
判断c1是否不等于c2(等同于!(c1==c2)) |
|
c1 < c2 |
判断c1是否小于c2 |
|
c1 > c2 |
判断c1是否大于c2 |
|
c1 <= c2 |
判断c1是否小于等于c2(等同于!(c2<c1)) |
|
c1 >= c2 |
判断c1是否大于等于c2 (等同于!(c1<c2)) |
3.3 特殊的搜寻函数
sets和multisets在元素快速搜寻方面做了优化设计,提供了特殊的搜寻函数,所以应优先使用这些搜寻函数,可获得对数复杂度,而非STL的线性复杂度。比如在1000个元素搜寻,对数复杂度平均十次,而线性复杂度平均需要500次。
|
操作 |
效果 |
|
count (elem) |
返回元素值为elem的个数 |
|
find(elem) |
返回元素值为elem的第一个元素,如果没有返回end() |
|
lower _bound(elem) |
返回元素值为elem的第一个可安插位置,也就是元素值 >= elem的第一个元素位置 |
|
upper _bound (elem) |
返回元素值为elem的最后一个可安插位置,也就是元素值 > elem 的第一个元素位置 |
|
equal_range (elem) |
返回elem可安插的第一个位置和最后一个位置,也就是元素值==elem的区间 |
3.4 赋值
|
操作 |
效果 |
|
c1 = c2 |
将c2的元素全部给c1 |
|
c1.swap(c2) |
将c1和c2 的元素互换 |
|
swap(c1,c2) |
同上,全局函数 |
3.5 迭代器相关函数
sets和multisets的迭代器是双向迭代器,对迭代器操作而言,所有的元素都被视为常数,可以确保你不会人为改变元素值,从而打乱既定顺序,所以无法调用变动性算法,如remove()。
|
操作 |
效果 |
|
c.begin() |
返回一个随机存取迭代器,指向第一个元素 |
|
c.end() |
返回一个随机存取迭代器,指向最后一个元素的下一个位置 |
|
c.rbegin() |
返回一个逆向迭代器,指向逆向迭代的第一个元素 |
|
c.rend() |
返回一个逆向迭代器,指向逆向迭代的最后一个元素的下一个位置 |
3.6 安插和删除元素
必须保证参数有效,迭代器必须指向有效位置,序列起点不能位于终点之后,不能从空容器删除元素。
|
操作 |
效果 |
|
c.insert(elem) |
插入一个elem副本,返回新元素位置,无论插入成功与否。 |
|
c.insert(pos, elem) |
安插一个elem元素副本,返回新元素位置,pos为收索起点,提升插入速度。 |
|
c.insert(beg,end) |
将区间[beg,end)所有的元素安插到c,无返回值。 |
|
c.erase(elem) |
删除与elem相等的所有元素,返回被移除的元素个数。 |
|
c.erase(pos) |
移除迭代器pos所指位置元素,无返回值。 |
|
c.erase(beg,end) |
移除区间[beg,end)所有元素,无返回值。 |
|
c.clear() |
移除所有元素,将容器清空 |
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl;
using namespace std;
const int maxn = 1e5 + ;
typedef long long ll;
struct node {
ll power, value, index;
};
bool cmp( node p, node q ) {
return p.power < q.power;
}
node a[maxn];
ll num[maxn];
int main(){
std::ios::sync_with_stdio(false);
ll n, k;
while( cin >> n >> k ) {
memset( num, , sizeof(num) );
for( ll i = ; i < n; i ++ ) {
cin >> a[i].power;
a[i].index = i;
}
for( ll i = ; i < n; i ++ ) {
cin >> a[i].value;
}
sort( a, a + n, cmp );
ll sum = ;
multiset<ll> s;
for( ll i = ; i < n; i ++ ) {
num[a[i].index] = sum + a[i].value;
sum += a[i].value;
s.insert(a[i].value);
while( s.size() > k ) {
sum -= *s.begin();
s.erase(s.begin());
}
}
for( ll i = ; i < n; i ++ ) {
if( i == n - ) {
cout << num[i] << endl;
} else {
cout << num[i] << " ";
}
}
}
return ;
}
CF994B Knights of a Polygonal Table 第一道 贪心 set/multiset的用法的更多相关文章
- [CF994B] Knights of a Polygonal Table - 贪心,堆
有 n 个骑士想决战.每个骑士都有能力值(互不相同),且身上带有一些金币.如果骑士 A 的能力值大于骑士 B ,那么骑士 A 就可以杀死骑士 B ,并获得骑士 B 身上的所有金币.但就算是骑士也不会残 ...
- Knights of a Polygonal Table CodeForces - 994B (贪心)
大意:n个骑士, 每个骑士有战力p, 钱c, 每个骑士可以抢战力比他低的钱, 每个骑士最多抢k次, 对每个骑士求出最大钱数 按战力排序后, 堆维护动态前k大即可 #include <iostre ...
- [C++]Knights of a Polygonal Table(骑士的多角桌)
[程序结果:用例未完全通过,本博文仅为暂存代码之目的] /* B. Knights of a Polygonal Table url:http://codeforces.com/problemset/ ...
- Codeforces 994B. Knights of a Polygonal Table
解题思路 将骑士按力量从小到大排序,到第i个骑士的时候,前面的i-1个骑士他都可以击败,找出金币最多的k个. 用multiset存金币最多的k个骑士的金币数,如果多余k个,则删除金币数最小的,直到只有 ...
- CodeForces 994B Knights of a Polygonal Table(STL、贪心)
http://codeforces.com/problemset/problem/994/B 题意: 给出n和m,有n个骑士,每个骑士的战力为ai,这个骑士有bi的钱,如果一个骑士的战力比另一个骑士的 ...
- poj 2942 Knights of the Round Table - Tarjan
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- POJ 2942 Knights of the Round Table
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
随机推荐
- 设置Myeclipse的jvm内存参数
Myeclipse经常会遇到内存溢出和Gc开销过大的情况,这时候就需要修改Myeclipse的Jvm内存参数 修改如下:(使用Extjs做公司大项目时候,不要让项目Builders的Javascrip ...
- 拉格朗日对偶性(Lagrange duality)
目录 拉格朗日对偶性(Lagrange duality) 1. 从原始问题到对偶问题 2. 弱对偶与强对偶 3. KKT条件 Reference: 拉格朗日对偶性(Lagrange duality) ...
- ContentProvider 使用详解
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- 制造资源计划(Manufacturing Resource Planning,Mrp II)
制造资源计划(Manufacturing Resource Planning,Mrp II) 概括: 以物料需求计划(MRP)为核心的企业生产管理计划系统,MRP II 是以工业工 ...
- [nghttp2]压测工具,源码编译并进行deb打包过程
编译环境:deepin 15.11桌面版 nghttp2下载地址:https://github.com/nghttp2/nghttp2 环境要求 emm只能在类Linux环境才能完整编译,想在Wind ...
- 素数筛法(Eratosthenes筛法)
介绍 Eratosthenes筛法,又名埃氏筛法,对于求1~n区间内的素数,时间复杂度为n log n,对于10^6^ 以内的数比较合适,再超出此范围的就不建议用该方法了. 筛法的思想特别简单: 对于 ...
- myeclipse源码相关操作
做web开发经常要看别人的jar里的源码才能搞懂别人的想法,但是源码有的时候需要单独下载很麻烦,甚至有的新的jar根本就是没有源码的,那么我们能不能自己制作源码呢. 从jar中提取源码 说白了,提取源 ...
- Git原理入门简析
为了获得更好的阅读体验,建议访问原地址:传送门 前言: 之前听过公司大佬分享过 Git 原理之后就想来自己总结一下,最近一忙起来就拖得久了,本来想塞更多的干货,但是不喜欢拖太久,所以先出一版足够入门的 ...
- Mysql优化-mysql分区
背景:由于我负责i西科教务处系统,i西科用户量达到20000人左右,那么假设每人每星期10门讲课,数据库记录信息将是20万条,如果不将课程表进行分区或分表,就会造成爆表的情况,如此看来,分区是必须要做 ...
- Linux - 通过expect工具实现脚本的自动交互
目录 1 安装expect工具 2 expect的常用命令 3 作用原理简介 3.1 示例脚本 3.2 脚本功能解读 4 其他脚本使用示例 4.1 直接通过expect执行多条命令 4.2 通过she ...