HDU - 5884 Sort (二分答案+模拟优先队列+k叉哈夫曼树)

HDU - 5884 Sort (二分答案+模拟优先队列+k叉哈夫曼树)

Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice.
Alice will give Bob N sorted sequences, and the i-th sequence includes ai elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than k sequences in one time. The cost of a merging operation is the sum of the length of these sequences. Unfortunately, Alice allows this program to use no more than T cost. So Bob wants to know the smallest k to make the program complete in time.
Input
The first line of input contains an integer t0, the number of test cases. t0 test cases follow.
For each test case, the first line consists two integers N (2≤N≤100000) and T (∑Ni=1ai<T<231).
In the next line there are N integers a1,a2,a3,…,aN(∀i,0≤ai≤1000).
Output
For each test cases, output the smallest k.
Sample Input
1
5 25
1 2 3 4 5
Sample Output
3

题目大意:

给你n个数,让你把这n个数合成一个数,每次可以合并k个,合并的花费为这k个数的和。
给你个T,问合并n个数为一个数且花费不超过T的最小的k为多少。

解题思路:

这显然是一个k叉哈夫曼树的问题。每次合并k个。看最少花费。
我们需要解决几个问题。

  • 补几个0? 完全k叉哈夫曼树的节点数为n个 那么(n-1)%(k-1)==0 为什么呢?因为除了第一层你需要k个其他层都是需要k-1个 所以 n-1必须是k-1的倍数。那么补0的个数就很好算了。(num就是要补的0的个数
int tmp = (n-1)%(k-1);
int num = 0;
if(tmp!=0) num = k-1-tmp;
  • 怎么样实现k叉哈夫曼树? 首先想到的肯定是用优先队列,每次弹出k个加和然后再放回去。但是在这个题中却不行,用优先队列会超时,会超时! 我看别人用了两个队列模拟了一个优先队列。让两个队列中都是有序的,然后取k个的时候,取两个队首较小的那个然后放进其中一个队列中。要实现这个的前提是两个队列里都是有序的!
	queue<ll> q1,q2;
	ll sum = 0;
	int tmp = (n-1)%(k-1);
	int num = 0;
	if(tmp!=0){
		num = k-1-tmp; 
		for(int i=0;i<num;i++) q1.push(0);
	}

	for(int i=0;i<n;i++) q1.push(a[i]);
	while(q1.size()+q2.size()>1){
		ll ss = 0;
		///每一层有k个 
		for(int i=0;i<k;i++){
			if(q1.size()>0&&q2.size()>0){
				ll t1 = q1.front();
				ll t2 = q2.front();
				if(t1<t2){
					ss+=t1;
					q1.pop();
				}else{
					ss+=t2;
					q2.pop();
				}
			}
			else if(q1.size()==0){
				ss+=q2.front();
				q2.pop(); 
			}else if(q2.size()==0){
				ss+=q1.front();
				q1.pop();
			}else{
				break;	
			}
		}
		sum += ss; 
		q2.push(ss);
	}

需要注意的几个问题:

  • 我们在二分找答案的时候,l = 2,因为最小最小就是二叉哈夫曼树。
  • 虽然题目说给的序列是sorted,但是如果不sort一遍就wa。。。我也不知道为啥。

AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5+10;
ll a[maxn];
int n;
ll t;

bool solve(int k){
	queue<ll> q1,q2;
	ll sum = 0;
	int tmp = (n-1)%(k-1);
	int num = 0;
	if(tmp!=0){
		num = k-1-tmp; 
		for(int i=0;i<num;i++) q1.push(0);
	}

	for(int i=0;i<n;i++) q1.push(a[i]);
	while(q1.size()+q2.size()>1){
		ll ss = 0;
		///每一层有k个 
		for(int i=0;i<k;i++){
			if(q1.size()>0&&q2.size()>0){
				ll t1 = q1.front();
				ll t2 = q2.front();
				if(t1<t2){
					ss+=t1;
					q1.pop();
				}else{
					ss+=t2;
					q2.pop();
				}
			}
			else if(q1.size()==0){
				ss+=q2.front();
				q2.pop(); 
			}else if(q2.size()==0){
				ss+=q1.front();
				q1.pop();
			}else{
				break;	
			}
		}
		sum += ss; 
		q2.push(ss);
	}
	return sum<=t;
}
int main(){
	int T;
	cin>>T;
	while(T--){
		scanf("%d%lld",&n,&t);
		for(int i=0;i<n;i++)
			scanf("%lld",&a[i]);
		sort(a,a+n);
		int l = 2;//最少是2叉 
		int r = n;
		int ans,mid;
		while(l<=r){
			mid = (l+r)/2;
			if(solve(mid)) {
				ans = mid;
				r = mid -1;
			}
			else l = mid+1;
			
		}
		printf("%d\n",ans);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值