203. Hyperhuffman

\(O(nlogn)\)可能会超时,最优二叉树有\(O(n)\)的做法,当年合并果子全机房就我最快,哈哈。。

开两个队列,一个存放未合并的节点,一个存放合并之后的子树,每次取最小时只需考虑这两个队列中的最小值即可,可以证明在队列内的元素单调。

notice: 输入数据已经排好序了, Characters are given from most rare to most frequent 。

 1 #include <bits/stdc++.h>
 2 #define rep(_i, _j) for(int _i = 1; _i <= _j; ++_i)
 3 typedef long long LL;
 4 const LL inf = 0x3f3f3f3f3f3f3f3f;
 5 typedef double DB;
 6 using namespace std;
 7
 8 const int maxn = 500000 + 10;
 9 int n;
10 LL que[2][maxn];
11 int head[2], tail[2];
12
13 #define val(k) (head[k] > tail[k] ? inf : que[k][head[k]])
14
15 LL min_val() {
16     int d = val(1) < val(0);
17     return que[d][head[d]++];
18 }
19
20 int main() {
21 #ifndef ONLINE_JUDGE
22     freopen("data.in", "r", stdin); freopen("data.out", "w", stdout);
23 #endif
24
25     scanf("%d", &n);
26     head[0] = head[1] = 0, tail[0] = tail[1] = -1;
27     for(int i = 1; i <= n; ++i) {
28         scanf("%lld", &que[0][++tail[0]]);
29     }
30     LL ans = 0;
31     for(int i = 1; i < n; ++i) {
32         LL t = min_val() + min_val();
33         que[1][++tail[1]] = t;
34         ans += t;
35     }
36     printf("%lld\n", ans);
37
38     return 0;
39 }

203. Hyperhuffman

时间: 2024-08-22 21:18:32

203. Hyperhuffman的相关文章

SQL Server2012使用导入和导出向导时,用sql语句作为数据源,出现数据源类型会变成202或者203

用MS SqlServer2012进行数据导出时,使用的查询语句导出,但是出现了错误: "发现 xx个未知的列类型转换您只能保存此包" 点击列查看详细错误信息时,可以看到: [源信息]源位置: 192.168.1.97源提供程序: SQLNCLI11表: [查询]列: ItemSize列类型: 203SSIS 类型: (类型未知...)映射文件(到 SSIS 类型): C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Mapping

[2016-04-14][POJ][203][Building a Space Station]

时间:2016-04-14 21:43:30 星期四 题目编号:[2016-04-14][POJ][203][Building a Space Station] 题目大意:给定n个球体,每个球体可能重合,可能包含,可能分离,问把每个球体连接起来(重合和包含看做已经连接),至少需要多长的路 分析:最小生成树,边权为 max(0,disij?ri?rj)max(0,disij?ri?rj),即重合和内含,边权为0 #include<cstdio> #include<cstring> #

leetCode 203. Remove Linked List Elements 链表

203. Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 题目大意: 删除链表中全部的目标元素. 代码如下:

&lt;swustoj&gt;?id=203 Jack&#39;s problem

链接http://acm.swust.edu.cn/problem/203/ #include <stdio.h> int main() { double x1,x2,x3,y1,y2,y3; double s; while(~scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)) { s=(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2)*1

C - Hyperhuffman(哈弗曼值)

C - Hyperhuffman Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description You might have heard about Huffman encoding - that is the coding system that minimizes the expected length of the tex

Acadia Lab 203 + Lab 231

在做完 Lab 6 之后,惊觉选做实验缺口很大,于是遍历了一遍夏任务,找到了一条最省力的路线. 做完 Lab 6 的连线不用拆,可以接下来做以下两个实验: Lab 203 网络时钟 核心代码如下: void write_data(int dat) { int i=0,temp; RS_H; EN_L; temp=dat & 0xf0; for (i=0; i < 4; i++) { if (temp&0x80) digitalWrite(DB[i],HIGH); else digit

“未能加载文件或程序集file:///E:/MoneySet.dll或它的某一个依赖项,试图加载格式不正确的程序,行203,位置5. 文件:MReportSet.resx”,

http://bbs.csdn.net/topics/390334265 "未能加载文件或程序集file:///E:/MoneySet.dll或它的某一个依赖项,试图加载格式不正确的程序,行203,位置5. 文件:MReportSet.resx",

java 给定一个日期期间 返回形如Mar 2015 3/20-3/31的数据

最近一个项目中有个前台对于表头要求: 给定一个日期期间返回形如 Mar 2015 3/20-3/31Apr 2015 4/1-4/30 这样的月年数据,简单的写了下代码,暂时没想到更好的办法 例如传进来的参数是 "2015-03-20"-"2016-04-08" 这样的形式 分三段处理: 第一段:起始日期的起始日期的月底 第二段:开始和结束月之间的所有月数据 第三段:结束日期的月初到结束日期 1 public List<String> loadMonth

配置203.195.139.19

crontab -e * * * * * /root/start.sh */20 * * * * /usr/sbin/ntpdate 0.centos.pool.ntp.org >>/root/ntp.log vi /root/start.sh #!/bin/bash #判断进程是否存在,如果不存在就启动它 PIDS=`ps -ef |grep vpnbridge |grep -v grep | awk '{print $2}'` if [ "$PIDS" != "