HDU 5884 Sort(K叉哈夫曼树例题)

Sort

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6398 Accepted Submission(s): 1588
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
Source
2016 ACM/ICPC Asia Regional Qingdao Online
Recommend
wange2014 | We have carefully selected several similar problems for you: 6837 6836 6835 6834 6833

K叉哈夫曼树,本质思路还是贪心,可以通过优先队列等的形式实现,但是时间复杂度时O(NlogN) 通过两个数组直接实现O(2N) 最后当a数组为空 b数组只剩一个数时 算法结束,这个便是哈夫曼树的根

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define Maxn 100005
#define LL long long
using namespace std;
int n,a[Maxn],b[Maxn],Val;

inline int  hufuman(int k) {// k cha Hufuman
    LL sum = 0;
    int cnt = 0;
    if((n - 1) % (k - 1) != 0) cnt = (n - 1) % (k - 1) + 1; // 这样可以在第一次的时候减少cnt次循环
    // 相当于直接补了cnt个0
    int ai = 0,bi = 0,m = 0;
    while(n - ai + m - bi > 1) {
        if(cnt == 0) cnt = k;
        int num = 0;
        while(cnt--) {
            if(n == ai) num += b[++bi];
            else if(m == bi) num += a[++ai];
            else if(a[ai + 1] < b[bi + 1]) num += a[++ai];
            else num += b[++bi];
        }
        sum += num;
        b[++m] = num;
        cnt = 0;
    }
    return sum <= Val;
}


int main(int argc,char* argv[]) {
    int T; scanf("%d",&T);
    while(T--) {
        scanf("%d %d",&n,&Val);
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);
        sort(a + 1,a + n + 1);
        int l = 2,Mid,r = n,Ans = 0;
        while(l <= r) {
            Mid = l + r >> 1;
            if(hufuman(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
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值