1. 程式人生 > >sgu-203 Hyperhuffman(哈夫曼編碼)

sgu-203 Hyperhuffman(哈夫曼編碼)

move include node ins time expect 一個 set tween

Hyperhuffman

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 Pitimes 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

解題思路:
  本題給出多組數據每組數據包括測試數據數量,每個測試包括一個整數代n表字符數量(即哈夫曼樹葉子結點數),n個整數代表每個字符出現的次數(即哈夫曼樹葉子結點權值),要求輸出其哈夫曼編碼的長度。

樣例解析:
  1    ->測試數據組數

  3    ->字符數量(葉子結點數)
  1 1 4   ->每個字符出現次數(哈夫曼樹葉子結點權值)

#include <bits/stdc++.h>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long> > value;    //優先隊列數值小的先出隊
int main()
{
    int t;
    while(cin >> t)     //輸入測試數據組數
    {
        while(t--)
        {
            int n;  //n記錄字符數量
            long long ans = 0LL;
            while(!value.empty())   //清空隊列
            {
                value.pop();
            }
            scanf("%d", &n);
            for(int i = 0; i < n; i++)
            {
                long long temp; //輸入出現次數
                scanf("%lld", &temp);
                value.push(temp);   //出現次數入隊
            }
            while(value.size() > 1)
            {
                long long x1 = value.top();
                value.pop();
                long long x2 = value.top();
                value.pop();
                //每次出隊兩個最小的數據合並後入隊
                ans += x1 + x2; //記錄長度
                value.push(x1 + x2);
            }
            printf("%lld\n", ans);  //輸出長度
            if(t)
                printf("\n");
        }
    }
    return 0;
}

sgu-203 Hyperhuffman(哈夫曼編碼)