• zoj 2399, 哈弗曼编码


    Hyperhuffman

    Time Limit: 5 Seconds      Memory Limit: 32768 KB

    You might have heard about Huffman encoding - that is the coding system that minimizes the expected length of the text if the codes for characters are required to consist of an integral number of bits.

    Let us recall codes assignment process in Huffman encoding. First the Huffman tree is constructed. Let the alphabet consist of N characters, i-th of which occurs Pi times in the input text. Initially all characters are considered to be active nodes of the future tree, i-th being marked with Pi. On each step take two active nodes with smallest marks, create the new node, mark it with the sum of the considered nodes and make them the children of the new node. Then remove the two nodes that now have parent from the set of active nodes and make the new node active. This process is repeated until only one active node exists, it is made the root of the tree.

    Note that the characters of the alphabet are represented by the leaves of the tree. For each leaf node the length of its code in the Huffman encoding is the length of the path from the root to the node. The code itself can be constrcuted the following way: for each internal node consider two edges from it to its children. Assign 0 to one of them and 1 to another. The code of the character is then the sequence of 0s and 1s passed on the way from the root to the leaf node representing this character.

    In this problem you are asked to detect the length of the text after it being encoded with Huffman method. Since the length of the code for the character depends only on the number of occurences of this character, the text itself is not given - only the number of occurences of each character. Characters are given from most rare to most frequent.

    Note that the alphabet used for the text is quite huge - it may contain up to 500 000 characters.

    This problem contains multiple test cases!

    The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

    The output format consists of N output blocks. There is a blank line between output blocks.

    Input

    The first line of the input file contains N - the number of different characters used in the text (2 <= N <= 500 000). The second line contains N integer numbers Pi - the number of occurences of each character (1 <= Pi <= 109, Pi <= Pi+1 for all valid i).

    Output

    Output the length of the text after encoding it using Huffman method, in bits.

    Sample Input

    1

    3
    1 1 4

    Sample Output

    8


    Author: Andrew Stankevich
    Source: Andrew Stankevich's Contest #2

    哈夫曼编码,双队列模拟即可。

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<fstream>
    
    #define MAX_INT 0x7fffffff
    #define LL long long
    #define ULL unsigned long long
    #define MAX(x,y) ((x) > (y) ? (x) : (y))
    #define MIN(x,y) ((x) > (y) ? (y) : (x))
    
    using namespace std;
    
    #define N 555555
    int p[N],q[N];
    int hp,hq,ep,eq;
    LL ans;
    int n;
    
    struct MM{
        int l,r;
        LL f;
    }t[2*N];
    
    void ini(){
        hp=hq=1;
        ep=n+1;   eq=1;
    
        for(int i=1; i<=n; i++) p[i]=i;
    }
    
    inline int have(){
        int n1;
        if(hp<ep && hq<eq){
            if(t[p[hp]].f < t[q[hq]].f) n1=p[hp++];
            else n1=q[hq++];
        }
        else if(hp<ep) n1=p[hp++];
        else n1=q[hq++];
        return n1;
    }
    
    void build(){
        int n1,n2,i;
        for(i=1; i<=n; i++){
            t[i].l=t[i].r=-1;
            t[i].f=p[i];
        }
    
        ini();
    
        while(hp<ep || hq<eq){
            n1=have();
            n2=have();
            t[i].f=t[n1].f + t[n2].f;
            t[i].l = n1;    t[i].r=n2;
            q[eq++]=i++;
        }
    }
    
    void pre(int x, int cur){
        if(t[x].l!=-1){
            pre(t[x].l,cur+1);
            pre(t[x].r,cur+1);
        }
        else ans+=cur*t[x].f;
    }
    
    int main(){
        // fstream fin("C:\Users\Administrator\Desktop\in.txt",ios::in);
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int cas;
        scanf(" %d",&cas);
        while(cas--){
            scanf(" %d",&n);
            int i;
            for(i=1; i<=n ;i++) scanf(" %d",&p[i]);
            build();
            ans=0;
            pre(2*n-1,0);
            cout<<ans<<endl;
            if(cas) cout<<endl;
        }
    
        // fin.close();
        return 0;
    }
    
  • 相关阅读:
    MVVM CopyValuesTo接触属性上下级关联
    怎样控制WPF GroupBox.HeaderTemplate中的控件
    wpf动态创建DataGrid
    mvvm 绑定textbox焦点丢失问题
    C# 导出CSV文件
    使用C#选择文件夹、打开文件夹、选择文件或者如何使用C#选择文件夹
    EF 强制从数据库刷新集合
    WPF及Silverlight中将DataGrid数据导出
    VS 2005中winForm开发(C#)—图片上传到数据库与显示(sql server 2005)
    数据导出为csv文件时 数值型数据为科学计数法 时间被截取的解决方法
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3412978.html
Copyright © 2020-2023  润新知