《数据结构》专题6--堆、哈夫曼树

A - 数据结构实验之二叉树六:哈夫曼编码

Description

字符的编码方式有多种,除了大家熟悉的ASCII编码,哈夫曼编码(Huffman Coding)也是一种编码方式,它是可变字长编码。该方法完全依据字符出现概率来构造出平均长度最短的编码,称之为最优编码。哈夫曼编码常被用于数据文件压缩中,其压缩率通常在20%~90%之间。你的任务是对从键盘输入的一个字符串求出它的ASCII编码长度和哈夫曼编码长度的比值。

Input

 输入数据有多组,每组数据一行,表示要编码的字符串。

Output

 对应字符的ASCII编码长度la,huffman编码长度lh和la/lh的值(保留一位小数),数据之间以空格间隔。

Sample

Input 

AAAAABCD
THE_CAT_IN_THE_HAT

Output 

64 13 4.9
144 51 2.8

Hint

 
#include<bits/stdc++.h>
using namespace std;

const int N = 1e5+7;
char s[N];
int cnt[N];
int main()
{
    ios::sync_with_stdio(false);
    int maxn, len, sum;
    while(cin >> s){
        priority_queue <int ,vector<int>,greater<int> >q;// 我偷懒我有罪
        memset(cnt, 0, sizeof(cnt));// cnt[] 的重置hin重要
        len = strlen(s);
        maxn = 0;
        sum = 0;
        for(int i = 0; i < len; i++){
            cnt[s[i]]++;
            if(s[i] > maxn)  maxn = s[i];
        }
        for(int i = 0; i <= maxn; i++){
            if(cnt[i] != 0)  q.push(cnt[i]);
        }
        while(!q.empty()){
            int a = q.top();
            q.pop();
            if(!q.empty()){
                int b = q.top();
                q.pop();
                sum = sum + a + b;
                q.push(a+b);
            }
        }
        printf("%d %d %.1f\n", len*8, sum, len*8.0/sum);
    }// ASCLL码值每位占8个字节
    return 0;
}

B - 树-堆结构练习——合并果子之哈夫曼树

Description

 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。

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

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

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

Input

 第一行是一个整数n(1<=n<=10000),表示果子的种类数。第二行包含n个整数,用空格分隔,第i个ai(1<=ai<=20000)是第i个果子的数目。

Output

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

Sample

Input 

3
1 2 9

Output 

15

Hint

// 优先队列
#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll s;
int main()
{
    ll n, i, m;
    priority_queue<ll ,vector<ll>,greater<ll> >q;
    while(cin >> n){
        ll a, b, z;
        s = 0;
        while(n--){
            cin >> m;
            q.push(m);
        }
        while(!q.empty()){
            a = q.top();
            q.pop();
            if(!q.empty()){
                b = q.top();
                q.pop();
                z = a + b;
                s += z;
                q.push(z);
            }// 一个是while 一个是if!!
        }
        cout << s << endl;
    }
    return 0;
}

C - Fence Repair

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn\'t own a saw with which to cut the wood, so he mosies over to Farmer Don\'s Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn\'t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

 Line 1: One integer N, the number of planks 

Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

 Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample

Input 

3
8
5
8

Output 

34

Hint

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 1e5 + 100;
const int M = 2e2 + 100;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
#define PI 3.14159265358979323846

int main()
{
    priority_queue<int, vector<int>, greater<int> >q;
    int n;
    LL sum = 0;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        int x;
        scanf("%d", &x);
        q.push(x);
    }
    while(!q.empty()){
        int a = q.top();
        q.pop();
        if(!q.empty()){
            int b = q.top();
            q.pop();
            sum = sum + a + b;
            q.push(a + b);
        }
    }
    printf("%lld", sum);
    return 0;
}

**私心加上一个堆排序的:

数据结构实验之排序四:寻找大富翁

Description

2015胡润全球财富榜调查显示,个人资产在1000万以上的高净值人群达到200万人,假设给出N个人的个人资产值,请你快速找出排前M位的大富翁。

Input

首先输入两个正整数N( N ≤ 10^6)和M(M ≤ 10),其中N为总人数,M为需要找出的大富翁数目,接下来给出N个人的个人资产,以万元为单位,个人资产数字为正整数,数字间以空格分隔。

保证所有的数据大小均在int范围内。

Output

一行数据,按降序输出资产排前M位的大富翁的个人资产值,数字间以空格分隔,行末不得有多余空格。

Sample

Input 

6 3
12 6 56 23 188 60

Output 

188 60 56

Hint

请用堆排序完成。

// 堆排序
#include<bits/stdc++.h>
using namespace std;

int heap[20], sz;
int res[20], cnt;
// 堆的两个操作:上升、下降
// 
void up(int u){
    while(u / 2 && heap[u/2] > heap[u]){
        swap(heap[u/2], heap[u]);
        u /= 2;
    }
}
void down(int u){
    int t = u;
    if(u*2 <= sz && heap[u*2] < heap[t]) 
        t = u * 2;
    if(u*2+1 <= sz && heap[u*2+1] < heap[t])
        t = u * 2 + 1;
    if(t != u){
        swap(heap[t], heap[u]);
        down(t);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // 上面三行是为了提高c++代码运行效率
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        int num;
        cin >> num;
        if(sz < m){
            heap[sz] = m;
            sz++;
            up(sz);
        }
        else{
            if(heap[1] < num){
                heap[1] = num;
                down(1);
            }
        }
    }// 本题只要排好前m个就行
    while(m--){
        res[++cnt] = heap[1];
        heap[1] = heap[sz];
        sz--;
        down(1);
    }
    for(int i = cnt; i >= 1; i--)
        cout << res[i] << " ";
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值