zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    门面模式简述
    转:日志组件logback的介绍及配置使用方法
    spring boot项目中使用sfl4j+logbak配置
    druid相关资料
    spring boot +druid数据库连接池配置
    设计模式之Strategy模式
    转:高效代码审查的八条准则和十个经验
    SpringMVC如何解决POST请求中文乱码问题,GET的又如何处理呢?
    【其它】关于本博客的一些说明
    [THUWC2020] 自爆记
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3412978.html
Copyright © 2011-2022 走看看