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个数字 ...
随机推荐
- 洛谷【P3952】NOIP2017提高组Day1T2时间复杂度
我对模拟的理解:http://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.luogu.org/problemnew/show/P395 ...
- spring IOC 注解@Autowired
自动装配:按照类型来找 会在xml中找类型一样的, 比如 setMessage(SetName setName)上面有@Autowired,会到xml中找<bean id="setna ...
- mysqladmin命令用法
mysqladmin 工具的使用格式: mysqladmin [option] command [command option] command ...... option 选项: -c numbe ...
- JAVA 1.7并发之Fork/Join框架
在之前的博文里有说过executor框架,其实Fork/Join就是继承executor的升级版啦 executor用于创建一个线程池,但是需要手动的添加任务,如果需要将大型任务分治,显然比较麻烦 而 ...
- 发布倒计时!JDK11为我们带来哪些新特性?
今年7月底,JDK11已经进入了Rampdown Phase Two阶段,这标志着该版本所有特性已经被冻结,不会有新的JEP会加入版本中. 这一阶段将会修复P1–P2级BUG,之后,JDK11预定于今 ...
- PG degraded实验
1. 创建一个文件,并把该文件作为对象放到集群中: [root@node1 ~]# echo "this is test! " >>test.txt [root@nod ...
- VUE之使用百度地图API
利用vue创建点餐系统,在点餐系统中需要知道商家地址信息,这时就需要借用百度地图API. 步骤一:申请百度地图密钥: 步骤二:在index.html中添加百度地图JavaScript API接口: & ...
- python+selenium简单实现拖动元素实例
from selenium import webdriver#引入ActionChains类from selenium.webdriver.common.action_chains impo ...
- 面试题: redis面试题 有用 redis详细
redis面试题目总结 2017-08-25 09:31:02 0个评论 来源:Java仗剑走天涯 收藏 我要投稿 (1)什么是redis? Redis 是一个基于内存的高 ...
- 10、RNA-seq for DE analysis training(Mapping to assign reads to genes)
1.Goal of mapping 1)We want to assign reads to genes they were derived from 2)The result of the mapp ...