HDU-5884 Sort(好题)

                                                                                                   Sort
 
                                                                       Time Limit: 3000/1000 MS (Java/Others)

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 (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个数,合并k个数的花费为这k个数加和,求最小k.

思路:可以想到每次合并最小的k个数,然后产生新的一个数,直到没有数了为止.  可以想到用优先队列实现,但是有一个问题,

合并到最后可能不够k个了,难道就是把这剩余的合并就完事了吗?    我们来想,每次我们合并都相当于产生了一个新的节点,整个合并过程相当于从树叶子到树根,最后最后产生根节点,每个节点被加的次数为它所处的深度.   回到刚才问题,假如最后只剩下了不到k个节点,相当于根的孩子不足k个,这明显不如让树的最底层节点不足k个,因为前者会使树变深,花费变大,所以我们应该先合并多余的,剩下的每次合并k个即可(优先队列开在外面比里面省时).

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const int maxn = 1e5+5;

int n,all;
int a[maxn],sum[maxn];
priority_queue< int,vector<int>,greater<int> > q;

bool judge(int k)
{
    while(!q.empty()) q.pop();
    int m = (n-1)%(k-1),s = 0;
    if(m> 0)
    {
        m++;
        q.push(sum[m]);
        s+= sum[m];
    }
    
    for(int i = m+1;i<= n;i++)
        q.push(a[i]);
    
    while(q.size()> 1)
    {
        int tmp = 0,x = k;
        while(x--)
        {
            tmp+= q.top();
            q.pop();
        }
        
        s+= tmp;
        if(s> all) return 0;
        q.push(tmp);
    }
    if(s> all) return 0;
    return 1;
}

int main()
{
    int t;
    cin>>t;
    
    while(t--)
    {
        scanf("%d %d",&n,&all);
        for(int i = 1;i<= n;i++)
            scanf("%d",&a[i]);
        
        sort(a+1,a+n+1);
        for(int i = 1;i<= n;i++) sum[i] = sum[i-1]+a[i];
        int l = 2,r = n,ans = n;
        while(l<= r)
        {
            int mid = (l+r)>>1;
            
            if(judge(mid))
                r = mid-1,ans = mid;
            else
                l = mid+1;
        }
        
        printf("%d\n",ans);
    }
    
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值