[C++]Knights of a Polygonal Table(骑士的多角桌)
+ 问题描述
+ Link: https://codeforces.com/contest/994/problem/B
+ 描述
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.
+ 大意
n个骑士, 每个骑士有战力p, 钱c, 每个骑士可以抢战力比他低的钱, 每个骑士最多抢k次, 对每个骑士求出最大钱数。
+ 输入
The first line contains two integers nn and kk (1≤n≤10^5,0≤k≤min(n−1,10))(1≤n≤10^5,0≤k≤min(n−1,10)) — the number of knights and the number kk from the statement.
The second line contains nn integers p1,p2,…,pn (1≤pi≤10^9)(1≤pi≤10^9) — powers of the knights. All pipi are distinct.
The third line contains nn integers c1,c2,…,cn (0≤ci≤10^9)(0≤ci≤10^9) — 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.
+ 样例

+ 网友思路
1 贪心 +(优先队列)
2 线段树
n ...
+ 代码
【程序结果:用例未完全通过,本博文仅为暂存代码之目的】
/*
B. Knights of a Polygonal Table
url:http://codeforces.com/problemset/problem/994/B
思路:
step0.对骑士进行编号
step1.按照权力值进行顺序排序
step2.分别计算骑士所能杀死的其他骑士及其对应的金币值
step3.按照编号重新恢复原始排序.
*/
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std; typedef struct Knight{
int id;
int power;
int coins;
int expected_coins;
}Knight; bool compare_id(Knight a, Knight b){
if(a.id>b.id)
return false;
else return true;
} bool compare_power(Knight a, Knight b){
if(a.power>b.power)
return false;
else return true;
} int main(){
int n,k,id = 0;
Knight *persons;
scanf("%d %d", &n, &k);
persons = (Knight*)malloc(n*sizeof(Knight));
//printf("\n%d %d", k, n);
for(int i=0;i<n;i++){
persons[i].id = ++id;
scanf("%d", &(persons[i].power));
}
for(int i=0;i<n;i++){
scanf("%d", &(persons[i].coins));
} sort(persons, persons+n, compare_power);//顺序排序:<algorithm> for(int i=0;i<n;i++){//分别计算各骑士能够杀死的人和可以获得的金币最大值
persons[i].expected_coins = persons[i].coins;
for(int j=1;j<=k;j++){
if(i - j >= 0){
persons[i].expected_coins += persons[i - j].coins;
}
}
} sort(persons, persons+n, compare_id);//依照id恢复原始排序 for(int i=0;i<n;i++){
printf("%d%s", persons[i].expected_coins, ((i + 1) != n ?" ":"\n"));//test
//printf("%d %d %d\n", persons[i].power, persons[i].coins, persons[i].expected_coins);//test
} return 0;
}
[C++]Knights of a Polygonal Table(骑士的多角桌)的更多相关文章
- 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 ...
- Codeforces 994B. Knights of a Polygonal Table
解题思路 将骑士按力量从小到大排序,到第i个骑士的时候,前面的i-1个骑士他都可以击败,找出金币最多的k个. 用multiset存金币最多的k个骑士的金币数,如果多余k个,则删除金币数最小的,直到只有 ...
- [CF994B] Knights of a Polygonal Table - 贪心,堆
有 n 个骑士想决战.每个骑士都有能力值(互不相同),且身上带有一些金币.如果骑士 A 的能力值大于骑士 B ,那么骑士 A 就可以杀死骑士 B ,并获得骑士 B 身上的所有金币.但就算是骑士也不会残 ...
- CodeForces 994B Knights of a Polygonal Table(STL、贪心)
http://codeforces.com/problemset/problem/994/B 题意: 给出n和m,有n个骑士,每个骑士的战力为ai,这个骑士有bi的钱,如果一个骑士的战力比另一个骑士的 ...
- POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- 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 ...
随机推荐
- Jmeter接口自动化
基本思路: 在excel中维护测试用例,包括访问协议,服务器名或IP,路径,请求的方法,端口号,提交参数,测试结果等,使用CSV Data Set Config读取请求信息并写入测试结果,后期只要维护 ...
- 第二十六节,滑动窗口和 Bounding Box 预测
上节,我们学习了如何通过卷积网络实现滑动窗口对象检测算法,但效率很低.这节我们讲讲如何在卷积层上应用这个算法. 为了构建滑动窗口的卷积应用,首先要知道如何把神经网络的全连接层转化成卷积层.我们先讲解这 ...
- javase的网络编程(InetAddress,UDP,TCP,URL,Socket,DatagramSocket)
通过一段时间对java网络编程相关内容的学习,写下这篇随笔,对这一部分的知识进行梳理和总结. 网络编程 一.网络编程三要素: IP地址:网络会给每个联网的主机分配一个数字的编码地址,该地址就是IP地址 ...
- 关于继承的基本知识,方法重写,final和abstract的使用, 动态绑定和静态绑定的知识
一.继承: 涉及关键字: extends(继承) super final abstract 特点: 1.类与类之间可以用 XX是XX来描述 , 那么他们之间就存在继承关系. 2.Java中不支持多继 ...
- Idea + Git + GitLab 使用
首先去下载Git,https://git-scm.com/ 安装好之后,打开Idea--->Settings,,,设置Git路径,然后点击Test按钮 然后是GitLab,一般企业内部开发都会有 ...
- JS生成随机数并排序
JS生成[10,100]之间的十个随机数,并排序 function getRandom(start,end){ var m=end-start+1 return Math.floor(Math.ran ...
- (贪心和优先队列) POJ1862 Stripies
Stripies Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21506 Accepted: 9478 Descrip ...
- 用Jdbc连接数据库后实现增删改查功能
增删改用的都是executeUpdate()方法: 查用的是executeQuery()方法 package cn.lideng.dbc; import java.lang.management.Ma ...
- linux简单优化
1.简单优化 #关闭firewalld,selinux,NetworkManager systemctl(管理服务的命令) stop(关服务) firewalld (服务名称,d是demo的意思) s ...
- CSS样式链接和文字常用属性
行内: <div style="color:red;"></div> 内嵌<style>div{background-color:red;}&l ...