一、哈夫曼树:
①、
P1090 合并果子:
题目描述
在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。

每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过 n-1次合并之后, 就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。

因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为 1 ,并且已知果子的种类 数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。

例如有3 种果子,数目依次为 1 , 2 , 9 。可以先将 1 、 2 堆合并,新堆数目为 3 ,耗费体力为 3 。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为 12 ,耗费体力为 12 。所以多多总共耗费体力 =3+12=15 。可以证明 15 为最小的体力耗费值。

输入格式
共两行。
第一行是一个整数 n ,表示果子的种类数。

第二行包含 n 个整数,用空格分隔,第 i 个整数 是第 i 种果子的数目。

输出格式
一个整数,也就是最小的体力耗费值。输入数据保证这个值小于 2^31

输入输出样例
输入 #1 复制
3
1 2 9
输出 #1 复制
15
说明/提示
对于30%的数据,保证有n≤1000:

对于50%的数据,保证有n≤5000;

对于全部的数据,保证有n≤10000。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#define ll long long
#define llu unsigned ll
using namespace std;
int main(void)
{
    int n,x,ans=0;
    priority_queue<int>q;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        q.push(-x);
    }
    while(q.size()>1)
    {
        int now=0;
        now+=q.top();
        q.pop();
        now+=q.top();
        q.pop();
        ans-=now;
        q.push(now);
    }
    printf("%d\n",ans);
    return 0;

}

②、Sort HDU - 5884 :
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 logn,加上二分一个logn,应该是n logn logn
但是本题可以利用序列的单调性,用两个队列来维护,进而使每次哈夫曼树的时间复杂度降到
O(n),这样整个的时间复杂度为n logn

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#define ll long long
#define llu unsigned ll
using namespace std;
const int maxn=100100;
ll a[maxn],n,res;

bool check(int k)
{
    queue<ll>q1,q2;
    ll ans=0,temp=0;
    int tm=(n-1)%(k-1);
    if(tm)
        for(int i=1;i<=k-1-tm;i++)
            q1.push(0);
    for(int i=1;i<=n;i++)
        q1.push(a[i]);

    while(q1.size()+q2.size()>=2)
    {
        temp=0;
        for(int i=1;i<=k;i++)
        {
            if(q1.size()&&q2.size())
            {
                if(q1.front()<q2.front())
                    temp+=q1.front(),q1.pop();
                else temp+=q2.front(),q2.pop();
            }
            else if(q1.size())
                temp+=q1.front(),q1.pop();
            else if(q2.size())
                temp+=q2.front(),q2.pop();
        }
        ans+=temp;
        q2.push(temp);
    }
    return ans<=res;
}

int main(void)
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&res);
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);

        sort(a+1,a+n+1);

        int pos=2,l=2,r=n;
        int mid;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(check(mid)) pos=mid,r=mid-1;
            else l=mid+1;
        }

        printf("%d\n",pos);
    }
}

二、哈夫曼编码:
P2168 [NOI2015]荷马史诗:
追逐影子的人,自己就是影子 ——荷马

Allison 最近迷上了文学。她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的《荷马史诗》。但是由《奥德赛》和《伊利亚特》 组成的鸿篇巨制《荷马史诗》实在是太长了,Allison 想通过一种编码方式使得它变得短一些。

一部《荷马史诗》中有n种不同的单词,从1到n进行编号。其中第i种单 词出现的总次数为wi。Allison 想要用k进制串si来替换第i种单词,使得其满足如下要求:

对于任意的 1 ≤ i, j ≤ n , i ≠ j ,都有:si不是sj的前缀。

现在 Allison 想要知道,如何选择si,才能使替换以后得到的新的《荷马史诗》长度最小。在确保总长度最小的情况下,Allison 还想知道最长的si的最短长度是多少?

一个字符串被称为k进制字符串,当且仅当它的每个字符是 0 到 k − 1 之间(包括 0 和 k − 1 )的整数。

字符串 str1 被称为字符串 str2 的前缀,当且仅当:存在 1 ≤ t ≤ m ,使得str1 = str2[1…t]。其中,m是字符串str2的长度,str2[1…t] 表示str2的前t个字符组成的字符串。

输入格式
输入的第 1 行包含 2 个正整数 n, k ,中间用单个空格隔开,表示共有 n种单词,需要使用k进制字符串进行替换。

接下来n行,第 i + 1 行包含 1 个非负整数wi ,表示第 i 种单词的出现次数。

输出格式
输出包括 2 行。

第 1 行输出 1 个整数,为《荷马史诗》经过重新编码以后的最短长度。

第 2 行输出 1 个整数,为保证最短总长度的情况下,最长字符串 si 的最短长度。

输入输出样例
输入 #1 复制
4 2
1
1
2
2
输出 #1 复制
12
2
输入 #2 复制
6 3
1
1
3
3
9
9
输出 #2 复制
36
3
说明/提示
【样例说明 1】

用 X(k) 表示 X 是以 k 进制表示的字符串。

一种最优方案:令 00(2) 替换第 1 种单词, 01(2) 替换第 2 种单词, 10(2) 替换第 3 种单词,11(2) 替换第 4 种单词。在这种方案下,编码以后的最短长度为:

1 × 2 + 1 × 2 + 2 × 2 + 2 × 2 = 12

最长字符串si的长度为 2 。

一种非最优方案:令 000(2) 替换第 1 种单词,001(2) 替换第 2 种单词,01(2)替换第 3 种单词,1(2) 替换第 4 种单词。在这种方案下,编码以后的最短长度为:

1 × 3 + 1 × 3 + 2 × 2 + 2 × 1 = 12

最长字符串 si 的长度为 3 。与最优方案相比,文章的长度相同,但是最长字符串的长度更长一些。

【样例说明 2】

一种最优方案:令 000(3) 替换第 1 种单词,001(3) 替换第 2 种单词,01(3) 替换第 3 种单词, 02(3) 替换第 4 种单词, 1(3) 替换第 5 种单词, 2(3) 替换第 6 种单词。

n<=1e5
k<=9
wi<=1e11

本题要求在符合要求的前提下,最长编码的长度最短。只需要对于相同权值的节点,优先考虑当前深度最小(已合并次数最少)的进行合并即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#define ll long long
#define llu unsigned ll
using namespace std;
const int maxn=100100;
struct node
{
    ll wi;
    ll d;
    node(){}
    node(const ll &a,const ll &b)
    {
        wi=a,d=b;
    }
    friend bool operator<(const node&a,const node &b)
    {
        if(a.wi!=b.wi) return a.wi>b.wi;
        else return a.d>b.d;
    }
};
int main(void)
{
    int n,k;
    ll ans=0,maxx=0,res=0,x;
    scanf("%d%d",&n,&k);
    priority_queue<node>q;

    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&x);
        q.push(node(x,0));
    }

    int tm=(n-1)%(k-1);
    if(tm)
        for(int i=1;i<=k-1-tm;i++)
        q.push(node(0,0));

    while(q.size()>=2)
    {
        maxx=res=0;
        for(int i=1;i<=k;i++)
        {
            res+=q.top().wi;
            maxx=max(maxx,q.top().d);
            q.pop();
        }
        ans+=res;
        q.push(node(res,maxx+1));
    }

    printf("%lld\n%lld\n",ans,q.top().d);
    return 0;


}