After last year’s success, Samuel W. E. R. Craft’s fame continues to grow and now he has funds for all kinds of projects that cross his mind. His newest idea involves creating arrays of canvasses with color patterns having no repeated colors.
    Samuel bought a set of white canvasses of varying sizes. Since painting them manually would take too much time, he devised a huge machine to automate the painting process.
    The painting process works as follows:

  1. Assemble all canvasses in a line in the machine’s conveyor belt, disposed in some chosen order.
  2. Pick a color C and a number F (which should be less than the number of color C canvasses).
  3. Going from left to right, all canvasses with color C are painted. The first F color C canvasses are painted with a new color X and the remaining color C canvasses are painted with a new color Y . Colors X and Y are selected by the machine, are distinct, and are different from any color used previously. The amount of ink spent in this step is equal to the sum of the sizes of the painted canvasses.
  4. Repeat 2) and 3) until all canvasses have distinct colors.

Consider for example that Samuel bought four canvasses of sizes 3, 5, 5 and 7. The following picture shows 2 different options for painting them.

Given the sizes of the canvasses Samuel bought, can you help Samuel finding the minimum amount of ink the machine needs to spend in order to have all canvasses with different colors?
Input
The first line consists of a single integer T, the number of test cases. Each test case is composed by two lines. The first line consists of a single integer N representing the number of canvasses. The next line contains N space separated integers representing the sizes of the canvasses.
Constraints:
1 ≤ T ≤ 100 Number of test cases.
1 ≤ Ni ≤ 100 000 Number of canvasses in the ith test case.
1 ≤ s ≤ 100 000 Size of each canvas.
1 ≤∑i=1TNi\sum_{i=1}^{T}N_i∑i=1T​Ni​ ≤ 100 000 Number of canvasses over all test cases in one test file.
Output
The output contains T lines, one for each test case: the minimum amount of ink the machine needs in order to have all canvasses with different colors.
Sample Input
2
3
7 4 7
4
5 3 7 5
Sample Output
29
40

问题链接:UVA13017 LA7274 Canvas Painting
问题简述:(略)
问题分析:哈夫曼编码问题,数据表示上可以用优先队列,也可以用集合来实现。这里使用集合来实现,但是需要注意的是,要使用multiset,保证数据重复时能够正确计算。
程序说明:LA7274的测试数据有点怪,需要考虑有关多个测试样例(指有多个t)的情况。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA13017 LA7274 Canvas Painting */#include <bits/stdc++.h>using namespace std;int main()
{int t, n, v;while (~scanf("%d", &t)) {while (t--) {multiset<long long> s;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &v);s.insert(v);}long long ans = 0;for (int i = 1; i < n; i ++) {long long a, b;a = *s.begin();s.erase(s.begin());b = *s.begin();s.erase(s.begin());s.insert(a + b);ans += a + b;}printf("%lld\n", ans);}}return 0;
}

UVA13017 LA7274 Canvas Painting【哈夫曼编码】相关推荐

  1. 可逼近信道容量编码技术之霍夫曼编码的实现

    可逼近信道容量编码技术之霍夫曼编码的实现 简介 在当今信息爆炸时代,如何采用有效的数据压缩技术来节省数据文件的存储空间和计算机网络的传送时间已越来越引起人们的重视.哈夫曼编码正是一种应用广泛且非常有效 ...

  2. 数据结构与算法(6-5)二叉树的应用--哈夫曼树与哈夫曼编码

    目录 哈夫曼编码(最优二叉树) 一.优势:缩短电文长度 二.思想: 三.过程: 四.图解实现过程: 五.总代码 哈夫曼编码(最优二叉树) 一.优势:缩短电文长度 二.思想: 获取每个字符出现的频率,用 ...

  3. 哈夫曼树的生成及哈夫曼编码

    首先构造哈夫曼树结构体,初始化哈夫曼树的四个无符号整型域,输入文本,统计各个字符的权值,然后构建哈夫曼树,从根到叶子逆向求哈夫曼树的编码. #include"stdio.h" #i ...

  4. 哈夫曼树哈夫曼编码(已知A,B,C,D,E,F,G的概率分别为:17,25,50,67,40,60,30.画出其哈夫曼树和每个字符对应的哈夫曼编码)

    [也可以哈夫曼树与最优不等长编码] 已知A,B,C,D,E,F,G的概率分别为:17,25,50,67,40,60,30.画出其哈夫曼树和每个字符对应的哈夫曼编码.

  5. java中哈夫曼编码所用的函数_数据结构(java语言描述)哈夫曼编码

    原理:哈夫曼编码是根据将已给出的权值作为叶子结点,生成一颗哈夫曼树,然后使得权重最小. 首先生成已给权重的所有的叶子结点,然后取所有节点中最小和次小的结点作为左右孩子生成一个哈夫曼树,计算出父节点的权 ...

  6. 递归求解并生成哈夫曼编码的代码实现

    一开始我用的三叉链表来生成哈夫曼编码,这一点都不递归.后来我想起了一度被递归统治地恐惧,我发现哈夫曼树不仅编码可以简单的用递归来求,树的WPL也可以. 改善后的递归版本如下,虽然WPL也可以通过递归来 ...

  7. 蓝桥哈夫曼树C语言,实验四 哈夫曼树及哈夫曼编码

    实验目的## 掌握哈夫曼树的概念.哈夫曼编码及其应用. 掌握生成哈夫曼树的算法. 会用哈夫曼树对传输报文进行编码. 掌握二叉树的二叉链表存储方式及相应操作的实现. ##实验内容## 用哈夫曼编码进行通 ...

  8. 20172311-哈夫曼编码测试

    20172311-哈夫曼编码测试 哈夫曼编码与哈夫曼树 哈夫曼树 哈夫曼树又称最优二叉树,是一种带权路径长度最短的二叉树.所谓树的带权路径长度,就是树中所有的叶结点的权值乘上其到根结点的路径长度(若根 ...

  9. 【Android 内存优化】Android 原生 API 图片压缩原理 ( 哈夫曼编码开关 | 哈夫曼编码原理 | libjpeg-turbo 函数库 )

    文章目录 一. 哈夫曼编码开关 二. 哈夫曼编码原理 三. libjpeg-turbo 函数库 四. libjpeg-turbo 函数库下载 [Android 内存优化]图片文件压缩 ( Androi ...

最新文章

  1. nsTimer的简单用法
  2. Redis系列教程(三):如何解决Redis缓存雪崩、缓存穿透、缓存并发等5大难题
  3. python基础(15)之 继承
  4. 误删/var/lib/dpkg/info,文件解决方案(是否完全解决,不确定)
  5. wlop一张多少钱_粤港两地车牌办理条件丨办一张粤港两地车牌要多少钱
  6. C#多线程编程实例 线程与窗体交互
  7. 切换输入法默认语言为英文
  8. Hyper-V提供创建三种类型的虚拟网络
  9. ICCV-2021 Oral | AdaFocus:利用空间冗余性实现高效视频识别
  10. CentOS 7.6环境设置Redis开机自启动
  11. Linux使用ragel进行文本快速解析(下)
  12. IDEA配置方法注释模板
  13. 黑鲨重装计算机安装无法继续,黑鲨装机大师一键重装系统失败
  14. 单片机课设———基于51单片机的智能风扇控制器(汇编语言)
  15. mip php,zblog php mip主题开发官方指南
  16. 2021年焊工(初级)新版试题及焊工(初级)考试技巧
  17. 你为你的机会准备了什么
  18. KSO --在vue中卸载eslin
  19. 路由器自身拥有IP地址吗?
  20. Crypto++库实现AES和RSA加密解密

热门文章

  1. 2020-01-14 英文资料How to Set Up Intel® Ethernet Flow Director
  2. 配置 Hadoop 时遇到的一些问题
  3. 浅析GDAL库C#版本支持中文路径问题
  4. 使用GDAL打开裸数据(RAW)
  5. cache相关命中率的运算_Linux cache命中率查看
  6. Spark RDD的运行机制 工作节点分布关系
  7. JavaScript(五)对象
  8. 深入了解vue中slot和slot-scope
  9. 51nod 1770 数数字
  10. tensorflow精进之路(二十一)——使用slim模型对图像识别与检测(上)(Inception_ResNet_v2模型)