UVA - 10954 Add All (全部相加)(Huffman编码 + 优先队列)
题意:有n(n <= 5000)个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数。每次操作的开销等于删除的两个数之和,求最小总开销。所有数均小于10^5。
分析:按此操作,最终变成1个数,需要n-1次操作,要想总开销最小,就使每次取出的两数之和最小,优先队列。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 500 + 10;
const int MAXT = 10000 + 10;
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int main(){
int n;
while(scanf("%d", &n) == 1){
if(!n) return 0;
while(!q.empty()) q.pop();
for(int i = 0; i < n; ++i){
int x;
scanf("%d", &x);
q.push(x);
}
int ans = 0;
while(q.size() > 1){
int a = q.top(); q.pop();
int b = q.top(); q.pop();
ans += a + b;
q.push(a + b);
}
printf("%d\n", ans);
}
return 0;
}
UVA - 10954 Add All (全部相加)(Huffman编码 + 优先队列)的更多相关文章
- UVA 10954 Add All 全部相加 (Huffman编码)
题意:给你n个数的集合,每次选两个删除,把它们的和放回集合,直到集合的数只剩下一个,每次操作的开销是那两个数的和,求最小开销. Huffman编码.Huffman编码对于着一颗二叉树,这里的数对应着单 ...
- UVA 10954 Add All 哈夫曼编码
题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...
- 【uva 10954】Add All(算法效率--Huffman编码+优先队列)
题意:有N个数,每次选2个数合并为1个数,操作的开销就是这个新的数.直到只剩下1个数,问最小总开销. 解法:合并的操作可以转化为二叉树上的操作[建模],每次选两棵根树合并成一棵新树,新树的根权值等于两 ...
- 【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(优先队列)
题意 求把全部数加起来的最小代价 a+b的代价为(a+b) 越先运算的数 要被加的次数越多 所以每次相加的两个数都应该是剩下序列中最小的数 然后结果要放到序列中 也就是优先队列了 #inc ...
- UVA 10954 Add All
题意: 给出n个数,要将n个数相加,每次相加所得的值为当次的计算量,完成所有的求和运算后,要求总的计算量最小. 分析: 直接一个优先队列,由小到大排序,每次前两个相加就好. 代码: #include ...
- UVa 10954 Add All 贪心
贪心 每一次取最小的两个数,注意相加的数也要算' #include<cstring> #include<iostream> #include<cstdio> # ...
- UVa 10954 全部相加(Huffman编码)
https://vjudge.net/problem/UVA-10954 题意:有n个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数.每次操作的开销等于删除的两个数之和, ...
- UVa 10954,Add All
Huffman编码简化版,优先队列搞定. 1A 调试的时候发现一个问题..木有想明白...问题代码里给出,哪位大神给解释下. #include <iostream> #include &l ...
随机推荐
- Python可视化 | Seaborn包—heatmap()
seaborn.heatmap()的参数 seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None, robust=Fals ...
- django中添加日志功能
官方文档 猛戳这里 在settings中配置以下代码 #LOGGING_DIR 日志文件存放目录 LOGGING_DIR = "logs" # 日志存放路径 if not os.p ...
- SQL Server DATEADD() 函数 一步步使用教程
SQL Server DATEADD() 函数 DATEADD() 函数在日期中添加或减去指定的时间间隔. DATEADD(datepart,number,date)date 参数是合法的日期表达式. ...
- gitlab导入备份数据
1.将南阳的gitlab 迁入到本地80虚拟机 由于本地ip地址没有固定,所以,是本地去拉取南阳的代码,虽然,之后固定了ip,但,由于只用一次这样的操作,所以,还是一直在做拉取而不是推送的工作 2.具 ...
- 【Unity】鼠标指向某物体,在其上显示物体的名字等等等等信息
之前一直用NGUI HUD Text插件做这个功能,感觉一个小功能就导一个插件进来简直丧心病狂.然后就自己写了一个~ Camera cam;//用于发射射线的相机 Camera UIcam;//UI层 ...
- python2学习------基础语法5(文件操作)
1.文件内容读取 a.txt teestttttttttttttt teestttttttttttttt teestttttttttttttt teestttttttttttttt teesttttt ...
- greenplum 存储过程 函数
参考:https://docs.pivotal.io/search?q=function
- P1063 计算谱半径
P1063 计算谱半径 转跳点:
- 51nod 1378:夹克老爷的愤怒 很好玩的一道树状dp
1378 夹克老爷的愤怒 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 取消关注 夹克老爷逢三抽一之后,由于采用了新师爷的策略,乡民们叫苦不堪,开始组织 ...
- Minikube安装
参考 https://blog.csdn.net/liumiaocn/article/details/52041726?locationNum=4&fps=1 中文社区API http://d ...