Sort HDU - 5884 哈夫曼权值O(n)
http://acm.hdu.edu.cn/showproblem.php?pid=5884
原来求一次哈夫曼可以有O(n)的做法。
具体是,用两个队列,一个保存原数组,一个保存k个节点合并的数值,然后每次选k个的时候,用two point在两个队列中选k个出来即可。
然后又把新的节点放去第二个队列那里去。
所以,首先需要排序(这样就和求 一次huffman编码的时间复杂度一样了nlogn)
然后这样明显第二个队列是单调递增的。
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = + ;
int a[maxn];
int n, T;
LL que[][maxn], head[], tail[];
LL en = 1e14;
bool check(int val) {
head[] = tail[] = head[] = tail[] = ;
int res = n % (val - );
if (res == ) {
res = val - ;
}
LL ans = ;
if (res == ) {
for (int i = ; i <= n; ++i) que[][tail[]++] = a[i];
} else {
int sum = ;
for (int i = ; i <= res; ++i) sum += a[i];
for (int i = res + ; i <= n; ++i) que[][tail[]++] = a[i];
ans = sum;
que[][tail[]++] = sum;
}
while (true) {
if (ans > T) return false;
LL sum = ;
int want = val;
while (want--) {
LL mi1 = 1e14, mi2 = 1e14;
if (head[] < tail[]) mi1 = que[][head[]];
if (head[] < tail[]) mi2 = que[][head[]];
if (mi1 < mi2) sum += mi1, head[]++;
else sum += mi2, head[]++;
if (mi1 == en && mi2 == en) return true; // 不可以取了
}
que[][tail[]++] = sum;
ans += sum;
}
return true;
}
void work() {
scanf("%d%d", &n, &T);
for (int i = ; i <= n; ++i) scanf("%d", a + i);
sort(a + , a + + n);
int be = , en = n;
// cout << check(2) << endl;
while (be <= en) {
int mid = (be + en) >> ;
if (check(mid)) en = mid - ;
else be = mid + ;
}
printf("%d\n", be);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
Sort HDU - 5884 哈夫曼权值O(n)的更多相关文章
- 2016 年青岛网络赛---Sort(k叉哈夫曼)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5884 Problem Description Recently, Bob has just learn ...
- HDU 1053 & HDU 2527 哈夫曼编码
http://acm.hdu.edu.cn/showproblem.php?pid=1053 #include <iostream> #include <cstdio> #in ...
- hdu 1565&hdu 1569(网络流--最小点权值覆盖)
方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2527哈夫曼树(二叉树的运用)
#include<stdio.h> #include<string.h> #define N 100 #define INF 2000000000 int b[N]; c ...
- HDU 5249:KPI(权值线段树)
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...
- HDU 1533 KM算法(权值最小的最佳匹配)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU - 5592 ZYB's Premutation (权值线段树)
题意:给出序列前k项中的逆序对数,构造出这个序列. 分析:使用权值线段树来确定序列元素. 逆序对的数量肯定是递增的,从最后一个元素开始逆向统计,则\(a[i] - a[i-1]\)即位置i之前比位置i ...
- hdu 5592 ZYB's Premutation (权值线段树)
最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Mem ...
- Hdu P1394 Minimum Inversion Number | 权值线段树
题目链接 题目翻译: 约定数字序列a1,a2,...,an的反转数是满足i<j和ai>aj的数对(ai,aj)的数量. 对于给定的数字序列a1,a2,...,an,如果我们将第1到m个数字 ...
随机推荐
- HDU1042(N!:设4为基数)
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submi ...
- SP:目录
ylbtech-SP:目录 商谱开发框架 1.返回顶部 1. 1.WebUIWeChatUI MobileUI 2.Core-Model-Repository-Security -Service3.D ...
- JAVA基础知识(12)-----同步
好处:解决了线程安全问题.弊端:相对降低性能,因为判断锁需要消耗资源,产生了死锁.定义同步是有前提的:1,必须要有两个或者两个以上的线程,才需要同步.2,多个线程必须保证使用的是同一个锁. 同步的第二 ...
- LAMP 1.2 Apache编译安装问题解决
这个错误安装 yum install -y gcc error: mod_deflate has been requested but can not be built due to prerequi ...
- eclipse中使用Maven插件报错:-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
步骤: 1.添加M2_HOME的环境变量 2.Preference->Java->Installed JREs->Edit 选择一个jdk 3.添加 -Dmaven.multiMod ...
- 6、perl创建模块(Exporter)及路径 引用 嵌套 查询模块
参考博客:http://www.cnblogs.com/xudongliang/tag/perl/ 1.perl 模块的创建以及制定perl 模块的路径 (1)创建一个Myfun.pm模块. #/us ...
- 阶段2-新手上路\项目-移动物体监控系统\Sprint4-嵌入式web服务器开发\第3课-CGI程序开发
实现CGI程序显示一幅图片最核心的功能 把上一节课编写好的led.c程序拷贝过来,并重新命名为image.c 把led的某些部分删除,后如下 那么如何显示一幅图片呢,百度(搜索在html里面去插入图片 ...
- Java虚拟机内存配置
在做java开发时尤其是大型软件开发时经常会遇到内存溢出的问题,比如说OutOfMemoryError等.这是个让开发人员很痛苦.也很纠结的问题,因为我们有时不知道什么样的操作导致了这种问题的发生.所 ...
- 在虚拟机环境(CentOS7系统)下将kubernetes中部署服务成功,但在虚拟机外部无法访问到服务
在CentOS7环境下,kubernetes单机版环境,成功部署一个服务,在虚拟机中访问服务没问题,下面这样: curl http://172.27.73.26:8888/eureka-server/ ...
- HTTP协议格式及基础
HTTP请求数据: HTTP请求信息由3部分组成: ① 请求方法 URI 协议/版本 ② 请求头(Request Header) ③ 请求正文 HTTP 请求 数据 例子举例: GET/sam ...