Hafuman

Hafuman树多用于合并代价,以及模拟哈夫曼编码。
构造哈夫曼树可以用优先队列和数组来实现。

149. 荷马史诗
时间复杂度O(nlogn)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define se second
#define fi first
typedef long long ll;
#define mp make_pair
typedef pair<ll,int>PII;
priority_queue<PII,vector<PII>,greater<PII> >p;
int main()
{
    int n,k;scanf("%d%d",&n,&k);
    for(int i = 1; i <= n ; i++){
        ll x;scanf("%lld",&x);
        p.push(mp(x,0));
    }
    while((n-1)%(k-1)){
        p.push(mp(0ll,0));n++;
    }
    ll ans=0;
    while(p.size()>1){
        ll sum=0;int dep=0;
        for(int i = 0 ; i < k ; i++){
            PII x=p.top();p.pop();
            dep=max(x.se,dep);
            sum+=x.fi;
        }
        ans+=sum;p.push(mp(sum,dep+1));
    }printf("%lld\n%lld\n",ans,p.top().se);
    return 0;
}

时间复杂度O(N)

const int maxn = 1e5+10;
int a[maxn],b[maxn];
int solve(){

    int ai=0,bi=0,blen=0,cost=0;
    bool f=true;
    while(n-ai+blen-bi>1){
        int num=0;
        if(f){
            if((N-k)%(k-1) == 0)
                num=k;
            else 
                num=(N-k)%(k-1)+1;
            f=false;
        }
        else num=k;
        int sum=0;
        while(num--){
            if(ai==n){
                sum+=b[bi];
                bi++;
            }
            else if(bi == blen){
                sum += a[ai];
                ai++;
            }
            else if(a[ai]<b[bi]){
                sum+=a[ai];
                ai++;
            }
            else{
                sum+=b[bi];
                bi++;
            }
        }
        cost += sum;
        b[blen++]=sum;
    }
    return cost;
}

int main()
{
    scanf("%d",&n);
    for(int i = 0 ; i < n ; i++){
        scanf("%d",&a[i]);
    }sort(a,a+n);
    int sum = solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为AI语言模型,我不能调用第三方库来实现哈夫曼编码。不过,下面的代码可以帮助你理解哈夫曼编码的实现过程: ```python from queue import PriorityQueue #优先队列用于实现哈夫曼树的构建 class Node: def __init__(self, value): self.left = None self.right = None self.value = value self.frequency = 1 def __lt__(self, other): return self.frequency < other.frequency #用于比较节点的频率 def build_huffman_tree(data): q = PriorityQueue() freq_dict = {} #建立字符和频率的映射 for term in data: if term in freq_dict: freq_dict[term] += 1 else: freq_dict[term] = 1 for key in freq_dict: q.put(Node(key)) while q.qsize() > 1: node1 = q.get() node2 = q.get() new_node = Node(None) new_node.left = node1 new_node.right = node2 new_node.frequency = node1.frequency + node2.frequency q.put(new_node) return q.get() #返回根节点 def encode_data(root, data): encoding_dict = {} #建立字符和编码的映射 get_encoding(root, encoding_dict, "") encoded_data = "" for term in data: encoded_data += encoding_dict[term] return encoded_data def get_encoding(node, encoding_dict, code): if node is None: return if node.value is not None: encoding_dict[node.value] = code get_encoding(node.left, encoding_dict, code + "0") get_encoding(node.right, encoding_dict, code + "1") #递归获得字符对应的编码 def decode_data(root, encoded_data): original_data = "" current_node = root for bit in encoded_data: if bit == "0": current_node = current_node.left else: current_node = current_node.right if current_node.value is not None: original_data += current_node.value current_node = root return original_data #根据编码还原原始数据 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值