2016 年青岛网络赛---Sort(k叉哈夫曼)

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5884

 

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
 
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:   5891  5890  5889  5887  5886 
 
题意:to组数据,每次输入N,T,然后输入N个数,进行合并操作,将其中k个数合并为一个数后,代价为这k个数的和,并将这个和放入剩余的数列中,一直合并下去......最后合并为一个数,要求总的代价不超过T,求最小的k值;
 
思路:k叉哈夫曼树,很明显k值在2~N之间,而且k越大总的代价越小,那么利用这个性质我们可以对k值进行二分查找,我开始时想的用优先队列做,但超时了......我们可以对数组先从小到大排序,然后利用一个队列装合并得到的数,每次取数组和队列中较小的数,注意用一个变量pos记录数组取完数后的下一个位置,队列中取完数后要删除这个数,为什么可以这样呢? 因为每次合并得到的数一定小于等于上次合并得到的数,所以最小数一是 数组pos位置和队列首中的较小者。另外,这些数的个数不一定满足k个k个的合并,所以要先合并不足的几个数,什么时候不满足呢,(N-1)%(k-1)!=0 时;
 
代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cmath>
#include <string.h>
using namespace std;
int N;
long long T;
long long a[100005];

int calc(int k)
{
    queue<long long>q;
    int pos=0;
    long long sum=0;
    if((N-1)%(k-1)!=0&&N>k) ///如果不能k个k个合并到底,则先合并筹不足k个的;
    {
        pos=(N-1)%(k-1)+1;
        for(int i=0;i<pos;i++)  sum+=a[i];
        q.push(sum);
    }
    while(1)
    {
        long long sum2=0;
        for(int i=0; i<k; i++)
        {
            if(!q.empty())
            {
                if(pos<N&&q.front()>a[pos])
                {
                    sum2+=a[pos];
                    sum+=a[pos];
                    pos++;
                }
                else
                {
                    sum2+=q.front();
                    sum+=q.front();
                    q.pop();
                }
            }
            else if(pos<N)
            {
                sum2+=a[pos];
                sum+=a[pos];
                pos++;
            }
            else goto endw;
        }
        if(sum>T) return 0;
        if(pos<N||!q.empty())
        q.push(sum2);
    }
    endw:;
    if(sum<=T) return 1;
    else    return 0;
}

int main()
{
    int to;
    scanf("%d",&to);
    while(to--)
    {
        scanf("%d%lld",&N,&T);
        for(int i=0; i<N; i++)
            scanf("%lld",&a[i]);
        sort(a,a+N);
        int l=2,r=N,mid;
        while(l<=r)
        {
            mid=(l+r)>>1;
            int f=calc(mid);
            if(f==0) l=mid+1;
            else     r=mid-1;
        }
        printf("%d\n",l);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/chen9510/p/5879618.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值