UVa 10954 Add All(优先队列)
题意 求把全部数加起来的最小代价 a+b的代价为(a+b)
越先运算的数 要被加的次数越多 所以每次相加的两个数都应该是剩下序列中最小的数 然后结果要放到序列中 也就是优先队列了
#include<cstdio>
#include<queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> >q;
typedef long long ll;
ll ans;
int main()
{
int n, t;
while(scanf("%d", &n), n)
{
for(int i = 0; i < n; ++i)
{
scanf("%d", &t);
q.push(t);
} ans = 0;
while(!q.empty())
{
int a = q.top(); q.pop();
int b = q.top(); q.pop();
ans += (a + b);
if(q.empty()) break;
q.push(a + b);
}
printf("%lld\n", ans);
}
return 0;
}
Yup!! The problem name reflects your task; just add a set of numbers. But you may feel yourselves condescended, to write a C/C++ program just to add a set of numbers. Such a problem will simply question your erudition. So, let’s add some flavor of ingenuity
to it.
Addition operation requires cost now, and the cost is the summation of those two to be added. So, to add 1 and 10, you need a cost of11. If you want to add 1, 2 and 3. There
are several ways –
1 + 2 = 3, cost = 3 3 + 3 = 6, cost = 6 Total = 9 |
1 + 3 = 4, cost = 4 2 + 4 = 6, cost = 6 Total = 10 |
2 + 3 = 5, cost = 5 1 + 5 = 6, cost = 6 Total = 11 |
I hope you have understood already your mission, to add a set of integers so that the cost is minimal.
Input
Each test case will start with a positive number, N (2 ≤ N ≤ 5000) followed by N positive integers (all are less than 100000). Input is terminated by a case where the value of N is zero.
This case should not be processed.
Output
For each case print the minimum total cost of addition in a single line.
Sample Input Output for Sample Input
3 1 2 3 4 1 2 3 4 0 |
9 19
|
UVa 10954 Add All(优先队列)的更多相关文章
- UVA - 10954 Add All (全部相加)(Huffman编码 + 优先队列)
题意:有n(n <= 5000)个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数.每次操作的开销等于删除的两个数之和,求最小总开销.所有数均小于10^5. 分析:按 ...
- 【NOIP合并果子】uva 10954 add all【贪心】——yhx
Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...
- UVA 10954 Add All 哈夫曼编码
题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...
- UVA 10954 Add All
题意: 给出n个数,要将n个数相加,每次相加所得的值为当次的计算量,完成所有的求和运算后,要求总的计算量最小. 分析: 直接一个优先队列,由小到大排序,每次前两个相加就好. 代码: #include ...
- UVA 10954 Add All 全部相加 (Huffman编码)
题意:给你n个数的集合,每次选两个删除,把它们的和放回集合,直到集合的数只剩下一个,每次操作的开销是那两个数的和,求最小开销. Huffman编码.Huffman编码对于着一颗二叉树,这里的数对应着单 ...
- UVa 10954 Add All 贪心
贪心 每一次取最小的两个数,注意相加的数也要算' #include<cstring> #include<iostream> #include<cstdio> # ...
- UVA.136 Ugly Numbers (优先队列)
UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...
- UVa 10954 (Huffman 优先队列) Add All
直接用一个优先队列去模拟Huffman树的建立过程. 每次取优先队列前两个数,然后累加其和,把这个和在放入到优先队列中去. #include <cstdio> #include <q ...
- 【uva 10954】Add All(算法效率--Huffman编码+优先队列)
题意:有N个数,每次选2个数合并为1个数,操作的开销就是这个新的数.直到只剩下1个数,问最小总开销. 解法:合并的操作可以转化为二叉树上的操作[建模],每次选两棵根树合并成一棵新树,新树的根权值等于两 ...
随机推荐
- Python中深拷贝与浅拷贝的区别
转自:http://blog.csdn.net/u014745194/article/details/70271868 定义: 在Python中对象的赋值其实就是对象的引用.当创建一个对象,把它赋值给 ...
- 阿里云机器学习tensorflow实践
1,前言 伴随人工智能和深度学习的应用越来越普及,越来越多的开发人员开始投入到智能算法的编程中.由于算法成熟且公开,软件编码这一块不存在难度:但模型训练和预测所需的时间与硬件设备的配置有极大关系,很多 ...
- HDU_1556_线段树区间更新
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【Linux】Tomcat安装及端口配置
安装环境 :Linux(CentOS 64位) 安装软件 : apache-tomcat-9.0.20.tar.gz(下载地址http://tomcat.apache.org/) 一:JDK安装配置 ...
- nodejs连接数据库
var express = require("express");var query = require("querystring");var mysql = ...
- 04JavaScript程序语句
JavaScript程序语句 2.6程序控制流程 2.6.1选择结构 if <逻辑表达式> 语句 else 语句 if <逻辑表达式> { 语句组 } else { 语句组} ...
- Android studio升级后原有项目无法正常编译运行问题
Android studio工具升级后Gradle版本问题 背景 升级AndroidStudio到最新版本后,原来可正常编译输出AndroidTest的项目无法正常编译通过. 原因 升级后的Andro ...
- X shell 6下载安装和简单使用
①前言:昨天已经上线了的智能家居项目出现了一个BUG,需要重新写个html发布到服务器上,由于公司大佬都在忙别的项目,时间比较紧张,这种小事就落到了我这个小喽啰身上.其实,写个html我还是可以接受的 ...
- fiddler培训
fiddler 在客户端和服务器中间做一个代理 ,只能截获http或HTTPS的请求 代理地址127.0.0.1 端口8888 反向代理,正向代理 浏览器上设置代理地址和端口 左边是session ...
- 框架之---Flask
Flask是一个轻量级的WEB框架,它和Django相比,就是一个胖子和一个骨架的区别. Flask是一个py文件就可以run的web框架,但是就因为是py文件就能run,所有,啥都没有,等把Flas ...