hdu 5884 Sort(二分+哈夫曼树(队列))

Sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1812    Accepted Submission(s): 455


Problem Description
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 (2N100000)  and  T (Ni=1ai<T<231) .
In the next line there are  N  integers  a1,a2,a3,...,aN(i,0ai1000) .
 

Output
For each test cases, output the smallest  k .
 

Sample Input
  
  
1 5 25 1 2 3 4 5
 

Sample Output
  
  
3
 

Source
 
题意:有n个数,两个数组合并的代价是两个数组个数之和,总代价不能超过T,求每次最少可以合并的个数是多少?
思路:很明显的二分然后判断,问题是如何判断?首先可以发现这里有个贪心的思想,就是每次都合并最小的k个数,最后的结果会是最优的。
其次可能不能每次都找到k个,为了维护二分的单调性,那么我们可以补0,不会影响最后的结果。(想象一下哈夫曼树多加一个权值为0的叶子节点)

如果直接用优先队列的话会TLE,我们这里还需要一个模拟优先队列的写法。用两个队列,一个存初始数组,一个 存中间结果,每次要取k个只要取两个之中小的一个,然后一个个取过去就好。  这里省去一个排序的时间,因为这两个队列一定是有序的,第一个自不必说,第二个因为后面合并的k个数一定比前面要大,故一定也是有序的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 100005
long long a[N];
int check(int k,long long t,int n)
{
    queue<long long>first;///存初始数组
    queue<long long>second;///存中间结果的数组,注意都是有序的,不需要特意排序
    long long sum=0;
    if((n-1)%(k-1))///k叉哈夫曼树要注意每次都要能找到k个数,不够补0
    {
        for(int i=1; i<=k-1-(n-1)%(k-1); i++)
            first.push(0);
    }
    for(int i=1; i<=n; i++)
        first.push(a[i]);
    while(!first.empty()||!second.empty())
    {
        long long ans=0;
        for(int i=1; i<=k; i++)
        {
            if(!first.empty()&&!second.empty())
            {
                if(first.front()<=second.front())
                {
                    ans+=first.front();
                    first.pop();
                }
                else
                {
                    ans+=second.front();
                    second.pop();
                }
            }
            else if(first.empty()&&!second.empty())
            {
                ans+=second.front();
                second.pop();
            }
            else if(!first.empty()&&second.empty())
            {
                ans+=first.front();
                first.pop();
            }
            else break;
        }
        sum+=ans;
        if(sum>t) return 0;
        if(first.empty()&&second.empty()) break;
        second.push(ans);
    }
    return 1;
}
int main()
{
    int T;
    int n;
    long long t;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %lld",&n,&t);
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        sort(a+1,a+1+n);
        int l=2,r=n,ans=n;
        while(l<r)///二分k
        {
            int mid=(l+r)>>1;
            if(check(mid,t,n)) ans=mid,r=mid;
            else l=mid+1;
        }
        printf("%d\n",l);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值