哈夫曼树  倒过来思考 ~

最深的叶子 值为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#调用C、C++结构体数组的方法总结

    一个客户要使用C#调用我们用C++开发的一个动态链接库,本来我没有C#的开发经验,就随便写了一个例程.以为很简单就可以搞定,没想到客户开发的过程中遇到了不少问题,最困难的就是用C#调用C++接口中的自 ...

  2. mplayer-for-windows change color scheme in win 7

    Q: When I play movie on Windows7, always comes this message: The color scheme has been changed The f ...

  3. wage

    #include<iostream> using namespace std; int main() { double wage1,wage2,time; cout<<&quo ...

  4. mysql数据库创建database(实例),和用户,并授权

    前言:mysql创建用户的方法分成三种:INSERT USER表的方法.CREATE USER的方法.GRANT的方法. 一.账号名称的构成方式 账号的组成方式:用户名+主机(所以可以出现重复的用户名 ...

  5. 批处理cmd背景颜色

    Pause  Echo 其他提示语 & pause > nul     pause暂停 COLOR 设置默认的控制台前景和背景颜色. COLOR [attr] attr 指定控制台输出的 ...

  6. IP进制站群原理

    百度搜索:“inurl:0×00”,会发现全是以八进制.十六进制形式显示的域名(如下图),当点击后,浏览器会自动将这些域名转换为十进制的ip.这种方式在黑帽圈目前挺火爆的,用于做长尾词排名,可以带来可 ...

  7. 反编译APK终结教程

    现在来教大家如何由网上下载的Android应用反编译为源码.如果你感兴趣,就来看一看吧.前提是你的电脑得已经配置好了java环境,如果没有配置好的话,下面我会附带一提,如果你还是不懂的话,那就上网搜一 ...

  8. Game Tutorials

    SDL: http://www.sdltutorials.com/tutorials        http://lazyfoo.net/ http://panda3d.noie.name/ http ...

  9. JNI 学习笔记

    JNI是Java Native Interface的缩写,JNI是一种机制,有了它就可以在java程序中调用其他native代码,或者使native代码调用java层的代码.也 就是说,有了JNI我们 ...

  10. [转]Linux下修改/设置环境变量JAVA_HOME

    1. 永久修改,对所有用户有效  # vi /etc/profile //按键盘[Shift + g], 在profile文件最后添加下面的内容: export JAVA_HOME = /home/m ...