CodeForces 994B Knights of a Polygonal Table(STL、贪心)
http://codeforces.com/problemset/problem/994/B


题意:
给出n和m,有n个骑士,每个骑士的战力为ai,这个骑士有bi的钱,如果一个骑士的战力比另一个骑士的战力高,那么,他就可以夺取这个骑士的钱,但是每个骑士最多夺取m个人,问,这些骑士最多可以获得
多少钱。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
#define Bug cout<<"---------------------"<<endl
const int maxn=1e5+;
using namespace std; struct node
{
int val;
int num;
LL ans;
int pos;
}PE[maxn]; bool cmp1(node a,node b)
{
return a.val<b.val;
} bool cmp2(node a,node b)
{
return a.pos<b.pos;
} int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&PE[i].val);
PE[i].pos=i;
}
for(int i=;i<=n;i++)
{
scanf("%d",&PE[i].num);
PE[i].ans=;
}
sort(PE+,PE++n,cmp1);//按战力排序
priority_queue<int,vector<int> ,greater<int> > qe;//存放m个大的硬币数
LL sum=;
for(int i=;i<=n;i++)
{
if(qe.size()<m)
{
qe.push(PE[i].num);
sum+=PE[i].num;
}
else if(!qe.empty())
{
int t=qe.top();
if(t<PE[i].num)
{
qe.pop();
sum=sum-t+PE[i].num;
qe.push(PE[i].num);
}
}
PE[i+].ans+=sum;//注意是i+1
}
sort(PE+,PE++n,cmp2);//按位置排序
for(int i=;i<=n;i++)
{
printf("%lld ",PE[i].ans+PE[i].num);//注意是LL
}
return ;
}
CodeForces 994B Knights of a Polygonal Table(STL、贪心)的更多相关文章
- Codeforces 994B. Knights of a Polygonal Table
解题思路 将骑士按力量从小到大排序,到第i个骑士的时候,前面的i-1个骑士他都可以击败,找出金币最多的k个. 用multiset存金币最多的k个骑士的金币数,如果多余k个,则删除金币数最小的,直到只有 ...
- [C++]Knights of a Polygonal Table(骑士的多角桌)
[程序结果:用例未完全通过,本博文仅为暂存代码之目的] /* B. Knights of a Polygonal Table url:http://codeforces.com/problemset/ ...
- 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 ...
- Knights of a Polygonal Table CodeForces - 994B (贪心)
大意:n个骑士, 每个骑士有战力p, 钱c, 每个骑士可以抢战力比他低的钱, 每个骑士最多抢k次, 对每个骑士求出最大钱数 按战力排序后, 堆维护动态前k大即可 #include <iostre ...
- [CF994B] Knights of a Polygonal Table - 贪心,堆
有 n 个骑士想决战.每个骑士都有能力值(互不相同),且身上带有一些金币.如果骑士 A 的能力值大于骑士 B ,那么骑士 A 就可以杀死骑士 B ,并获得骑士 B 身上的所有金币.但就算是骑士也不会残 ...
- 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 ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
随机推荐
- Database--事务
参考 https://segmentfault.com/a/1190000012669504?utm_source=tag-newest https://blog.csdn.net/qq_371559 ...
- centos6.7搭建局域网ntp服务器
修改/etc/ntp.conf文件 restrict xxx nomodify notrap nopeer noquery #xxx 此处配置本地IP地址restrict 12 ...
- java课程课后作业190530之找水王
从题目中我们可以看出,水王有着相当严苛的条件才可以成为,那就是必须拥有一半的评论量才可以当上水王.当然这就是破题的关键,最简单的算法当然是用O(N平方)的复杂度的那种算法,但显然,我们需要的不是这种. ...
- DispatcherServlet (1)
阅读DispatcherServlet源码和<Spring解密>第二十三章 总结 之前对SpringMVC处理请求模糊不清,本周阅读了DispatcherServlet源码,对这一部分有了 ...
- HTML笔记01
HTML语法规范 <!DOCTYPE html>//HTML5规范 用于注释<!-- HTML文件主要包含头部分和体部分 <!-title> 指定网站标题 指定浏览器打开 ...
- StringBuffer类、StringBuilder类详解
StringBuffer是一个字符串缓冲区,是一个容器,而且长度可变,可以直接操作多个数据类型, 最终会通过toString()方法变成字符串. 容器的功能有: 1.存储 public StringB ...
- UVA - 10118 Free Candies(免费糖果)(dp---记忆化搜索)
题意:桌上有4堆糖果,每堆有N(N<=40)颗.佳佳有一个最多可以装5颗糖的小篮子.他每次选择一堆糖果,把最顶上的一颗拿到篮子里.如果篮子里有两颗颜色相同的糖果,佳佳就把它们从篮子里拿出来放到自 ...
- 51nod 1284:2 3 5 7的倍数 容斥原理
1284 2 3 5 7的倍数 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 给出一个数N,求1至N中,有多少个数不是2 3 5 7的倍数. 例如N ...
- bzoj4316小C的独立集(dfs树/仙人掌+DP)
本题有两种写法,dfs树上DP和仙人掌DP. 先考虑dfs树DP. 什么是dfs树?其实是对于一棵仙人掌,dfs后形成生成树,找出非树边(即返祖边),然后dfs后每条返祖边+其所覆盖的链构成了一个环( ...
- SpringCloud----服务注册中心Eureka
Eureka是Netflix开源的一个RESTful服务,主要用于服务的注册发现.Eureka由两个组件组成:Eureka服务器和Eureka客户端.Eureka服务器用作服务注册服务器.Eureka ...