「NOI2015」荷马史诗
传送门
Luogu
解题思路
\(k\) 叉 \(\text{Huffman}\) 树板子题,至于最长串最短,只要同样权值的优先考虑深度小的就好了。
细节注意事项
- 咕咕咕
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
typedef long long LL;
const int _ = 100010;
int n, k; struct node{ LL w; int h; };
inline bool operator < (const node& a, const node& b)
{ return a.w == b.w ? a.h > b.h : a.w > b.w; }
priority_queue < node > Q;
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(k);
for (rg int i = 1; i <= n; ++i) {
LL w; read(w), Q.push((node) { w, 1 });
}
while ((n - 1) % (k - 1) != 0) Q.push((node) { 0, 1 }), ++n;
LL ans = 0;
while (Q.size() != 1) {
int mx = 0; LL tmp = 0;
for (rg int i = 1; i <= k; ++i)
tmp += Q.top().w, mx = max(mx, Q.top().h), Q.pop();
ans += tmp, Q.push((node) { tmp, mx + 1 });
}
printf("%lld\n%d\n", ans, Q.top().h - 1);
return 0;
}
完结撒花 \(qwq\)
「NOI2015」荷马史诗的更多相关文章
- 【LOJ】 #2132. 「NOI2015」荷马史诗
题解 k叉哈夫曼树,但是没有了二叉那样的最后一定能合并成一个树根的优秀性质,我们就不断模拟操作看看到了哪一步能用的节点数< k,然后先拿这些节点数合并起来 然后就可以k个k个合并了,大小一样先拿 ...
- LOJ#2132. 「NOI2015」荷马史诗
$n \leq 100000$个数字,放进$k$叉树里,一个点只能放一个数,使所有数字乘以各自深度这个值之和最小的同时,最大深度的数字最小. 哈夫曼.这是我刚学OI那段时间看到的,感觉就是个很无聊的贪 ...
- 「NOI2015」荷马史诗 (k叉huffman树/k叉合并果子)
是个多叉huffman树,思想类比合并果子. 具体见 CrazyDave 的博客 CODE #include <bits/stdc++.h> using namespace std; ty ...
- 【NOI2015】荷马史诗[Huffman树+贪心]
#130. [NOI2015]荷马史诗 统计 描述 提交 自定义测试 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读 ...
- 【BZOJ4198】【NOI2015】荷马史诗(贪心,Huffman树)
[BZOJ4198][NOI2015]荷马史诗(贪心,Huffman树) 题面 BZOJ 洛谷 题解 合并果子都是不知道多久以前做过的了.现在才知道原来本质就是一棵哈夫曼树啊. 这题我们仔细研究一下题 ...
- UOJ#130 【NOI2015】荷马史诗 K叉哈夫曼树
[NOI2015]荷马史诗 链接:http://uoj.ac/problem/130 因为不能有前缀关系,所以单词均为叶子节点,就是K叉哈夫曼树.第一问直接求解,第二问即第二关键字为树的高度. #in ...
- 【bzoj4198】【Noi2015】荷马史诗
4198: [Noi2015]荷马史诗 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2200 Solved: 1169[Submit][Statu ...
- 【NOI2015】荷马史诗
追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛>和& ...
- UOJ130 【NOI2015】荷马史诗
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
随机推荐
- Vue——前端生成二维码
与后端生成二维码相比,前端生成二维码更具有灵活性,下面就介绍两种前端生成二维码的方式,两种方式相比之下,vue-qr比qrcode多了一个再中间添加logo的功能. 方式一:qrcode npm np ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 迭代数组
import numpy as np a = np.arange(6).reshape(2,3) print ('原始数组是:') print (a) print ('\n') print ('迭代输 ...
- LeetCode中等题(三)
题目一: 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m ...
- java 第三次课后作业
1.java字段初始化的规律 public class gouzao { public static void main(String[] args) { test te=new test(); Sy ...
- Web性能测试工具推荐
WEB性能测试工具主要分为三种: 一种是测试页面资源加载速度的: 一种是测试页面加载完毕后页面呈现.JS操作速度的: 一种是总体上对页面进行评价分析. ~~~如果谁有更好的工具也请一起分享下 1. ...
- 洛谷 P1339 [USACO09OCT]热浪Heat Wave(最短路)
嗯... 题目链接:https://www.luogu.org/problem/P1339 这道题是水的不能在水的裸最短路问题...这里用的dijkstra 但是自己进了一个坑—— 因为有些城市之间可 ...
- Website's Game source code
A Darkroom by doublespeakgames <!DOCTYPE html> <html itemscope itemtype="https://schem ...
- linux 添加与修改用户归属组
参考资源:https://cnzhx.net/blog/linux-add-user-to-group/ 一:已存在的用户 1.要以root进行登录 2.打开终端 3.修改分组 usermod -a ...
- Unity初步 基本拼图实现
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...
- FiBiNET-学习
Our main contributions are listed as follows: • Inspired by the success of SENET in the computer vis ...