哈夫曼树  倒过来思考 ~

最深的叶子 值为1  所以最深的先出队列

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; struct node
{
long long d, v;
node(int i, int j)
{
d = i, v = j;
}
node() {}
bool operator < (node a) const
{
return d < a.d || (d == a.d && v > a.v);
}
}; priority_queue <node> q; int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
q.push(node(x, 0));
}
long long a, b, ans = 0, val = 1;
while((int)q.size() >= 2)
{
int cur = q.top().d;
a = q.top().v, q.pop();
b = q.top().v, q.pop();
if (!a)
a = val;
if (!b)
b = val;
val = max(val, max(a, b));
node t;
t.d = cur - 1, t.v = a + b;
q.push(t);
}
ans = q.top().v, q.pop();
printf("%lld\n", ans);
}
return 0;
}

UVALive 6533的更多相关文章

  1. The 2013 South America/Brazil Regional Contest 题解

    A: UVALive 6525 cid=61196#problem/A" style="color:blue; text-decoration:none">Atta ...

  2. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  3. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  4. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  5. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  6. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  9. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

随机推荐

  1. C#高效率导出Excel

    首先,需要引用excel的库: Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Exce ...

  2. (转)集成架构:对比 Web API 与面向服务的架构和企业应用程序集成

    摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不同,以及如何将它们融入到一个不断演变的集成架构中.文中还将讨论 API ...

  3. CSS Sprite 雪碧图制作

    CSS Sprite 雪碧图,简单来说就是: 为了提高网页的性能,减少加载次数,将一些不会经常随网站内容变化的小图标,集中放在一张大图上,将该图应作为background-image 嵌入页面中,在需 ...

  4. POD数据了解

    Plain old data (普通旧的数据); POD 是Plain Old Data的簡寫,是指一些系統的int, char, float.指標.array之類的資料型別,這應該蠻好想像的,就是C ...

  5. activiti系列导读

    此用于管理activiti系列标签文章,activiti的分析是建立在目前最新的版本5.21之上. 官方指导手册链接:http://www.activiti.org/userguide/index.h ...

  6. iOS屏幕尺寸和分辨率

    iOS平台家族成员主要包括iPhone.iPod Touch和iPad,但是各类设备的分辨率各不相同,目前存在的尺寸主要有: iOS设备的尺寸多种多样,此外,屏幕的分辨率也有多种,总结如下表所示: 其 ...

  7. 【转】JavaScript里的this指针

    用自然语言的角度理解JavaScript中的this关键字 <script type="text/javascript"> function ftn03(){ var ...

  8. How to running Job from a Form

    For Example we wanna run a Job with name  "FAN_TableList_CSV". So you must create a button ...

  9. 用户登录密码RSA加密后传输的实现,非明文密码传输

    在用户登录页面,用户输入密码后,在传送到服务器端时,为防止在密码传送过程中,被如360这种东东给拦截到, 需要在传送前对密码进行加密,然后再传送! 利用RSA加密,在客户端使用公钥对密码进行加密,在服 ...

  10. 微软更换考试中心后报名攻略以及MCT半价

    微软从Prometric更换为Pearson的考试中心,比起以前的预约流程更加便捷. Pearson VUE为微软公司提供MCP考试服务 Pearson VUE为微软公司提供MCP考试服务 http: ...