bzoj4198 荷马史诗
关于Huffman树:
大概就是那样子吧。
是这样的:对于最多只能有k个叉的树,我们想要使得∑val(i) * deep(i)最大
那么我们补0后建立小根堆即可。
最典型例题:合并果子。
然后是这个:
/**************************************************************
Problem: 4198
Language: C++
Result: Accepted
Time:1032 ms
Memory:3896 kb
****************************************************************/ #include <cstdio>
#include <queue>
const int N = ;
typedef long long LL;
inline void max(LL &a, LL b) {
if(a < b) a = b;
return;
}
struct Node {
LL val, p;
bool operator < (const Node &x) const {
if(val != x.val) {
return val > x.val;
}
return p > x.p;
}
}; std::priority_queue<Node> Q; Node mk(LL v, LL p) {
Node ans;
ans.val = v;
ans.p = p;
return ans;
} int main() {
LL n, k, v;
scanf("%lld%lld", &n, &k);
for(int i = ; i <= n; i++) {
scanf("%lld", &v);
Q.push(mk(v, ));
}
while((n - ) % (k - ) != ) {
Q.push(mk(, ));
n++;
} LL ans = ; for(int i = ; i <= (n - ); i += (k - )) {
LL p = ;
v = ;
for(int j = ; j <= k; j++) {
Node nd = Q.top();
Q.pop();
max(p, nd.p);
v += nd.val;
}
ans += v;
Q.push(mk(v, p + ));
}
LL p = Q.top().p;
printf("%lld %lld", ans, p);
return ;
}
AC代码
之前写的太菜了......
对于这种要让叶权值 * 深度最小的最多k叉树,通解就是每层放k - 1个,然后多的一个向下延伸。
从下往上着构造,要添加0补全。
然后,可以用蚯蚓的套路优化常数(有排序的复杂度)。
如果给定的是有序的或者能够桶排,就能做到O(n + k)。
#include <cstdio>
#include <algorithm>
#include <queue> typedef long long LL;
const int N = ; struct Node {
LL val, deep;
inline bool operator <(const Node &w) const {
if(val == w.val) {
return deep < w.deep;
}
return val < w.val;
}
Node(LL a = , LL b = ) {
val = a;
deep = b;
}
}a[N]; int top, head = ; std::queue<Node> Q; inline Node getmin() {
if(Q.empty()) {
return a[head++];
}
if(head > top || Q.front() < a[head]) {
Node t = Q.front();
Q.pop();
return t;
}
return a[head++];
} int main() {
LL n, k;
scanf("%lld%lld", &n, &k);
for(int i = ; i <= n; i++) {
scanf("%lld", &a[i].val);
a[i].deep = ;
} while((n - ) % (k - )) {
n++;
a[n].deep = ;
}
std::sort(a + , a + n + );
LL ans = , d = , lm = (n - ) / (k - );
top = n;
for(int i = ; i <= lm; i++) {
Node s;
for(int j = ; j <= k; j++) {
Node t = getmin();
s.val += t.val;
s.deep = std::max(s.deep, t.deep);
}
s.deep++;
Q.push(s);
ans += s.val;
d = std::max(d, s.deep);
} printf("%lld\n%lld\n", ans, d - );
return ;
}
新版AC代码
bzoj4198 荷马史诗的更多相关文章
- bzoj4198 荷马史诗 哈夫曼编码
逐影子的人,自己就是影子. --荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛>和&l ...
- 【BZOJ4198】【NOI2015】荷马史诗(贪心,Huffman树)
[BZOJ4198][NOI2015]荷马史诗(贪心,Huffman树) 题面 BZOJ 洛谷 题解 合并果子都是不知道多久以前做过的了.现在才知道原来本质就是一棵哈夫曼树啊. 这题我们仔细研究一下题 ...
- 【bzoj4198】【Noi2015】荷马史诗
4198: [Noi2015]荷马史诗 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2200 Solved: 1169[Submit][Statu ...
- 【BZOJ4198】[Noi2015]荷马史诗 贪心+堆
[BZOJ4198][Noi2015]荷马史诗 Description 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅 ...
- [UOJ#130][BZOJ4198][Noi2015]荷马史诗
[UOJ#130][BZOJ4198][Noi2015]荷马史诗 试题描述 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静 ...
- [BZOJ4198][Noi2015]荷马史诗
4198: [Noi2015]荷马史诗 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 700 Solved: 365[Submit][Status] ...
- [BZOJ4198] [Noi2015] 荷马史诗 (贪心)
Description 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是 ...
- BZOJ4198 & 洛谷2168 & UOJ130:[NOI2015]荷马史诗——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4198 https://www.luogu.org/problemnew/show/P2168 ht ...
- BZOJ4198: [Noi2015]荷马史诗(哈夫曼树)
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1824 Solved: 983[Submit][Status][Discuss] Descripti ...
随机推荐
- 查看端口占用cmd命令
查看端口被占用的进程: 在任务管理器中结束进程:
- day 7-12 数据库的基本操作和存储引擎
一. 储备知识 数据库服务器:一台高性能计算机 数据库管理系统:mysql(mssql等),是一个软件 数据库:db1(student_db),是一个文件夹 表:studen_info 是一个文件 记 ...
- Python基础知识2-内置数据结构(上)
分类 数值型 用浮点型的时候注意别和"=="一起使用. 数字的处理函数 注意round()函数的特殊:四舍六入五取偶 类型判断 列表list 列表list定义 初始化 列表索引访 ...
- Unable to handle kernel paging request at virtual address
1.Unable to handle kernel paging request at virtual address 00000000 =====>越出内核地址空间范围,原因是由于使用空NUL ...
- jquery和js的几种页面加载函数的方法以及执行顺序
参考博客:http://www.cnblogs.com/itslives-com/p/4646790.html https://www.cnblogs.com/james641/p/783837 ...
- Ajax之Jquery封装使用举例2(Json和JsonArray处理)
本例主要使用ajax进行异步数据请求,并针对返回数据为json和jsonarray类型的数据处理. 本例中只有前端的代码,后端代码不是本文重点,故省略. 后端接口返回数据为: Json: {" ...
- layui loading
layer.msg('加载中', { icon: 16 ,shade: 0.4}); layer.load(2);风格二 setTimeout(function(){ layer.closeAll(' ...
- static类型的变量
c语言中变量的储存类型有以下四种 auto 如果没有定义储存类型 默认就是这个类型 比如 int a = 10; 储存类型就是 auto:编译器会跟你定义的位置,以及用途,自动帮你决定使用那 ...
- How to flash Havoc on enchilada
update fastboot and adb fastboot oem unlock adb debug enchilada reboot to fastboot fastboot devices ...
- eclipse导入项目时,仅项目名出现红叉
今天导入项目,项目名是红叉,百度了解决办法: 1.导入项目之前,请确认工作空间编码已设置为utf-8:window->Preferences->General->Wrokspace- ...