C + + K-binary Huffman Tree

Source: Internet
Author: User

Original site: http://blog.csdn.net/Quack_quack/article/details/46958413

Title: Give n number w[], representing the number of times n letters appear, give K. The K-binary string si is required to replace the letter I, and replaced after the replacement of the article has no ambiguity (the ambiguity here refers to any of the 1≤i,j≤n, I≠j, there are: Si is not the prefix of SJ), to replace the shortest post length (length Len=sigma (w[i]* Strlen (SI))) and the minimum value of the largest SI in this case.
Data range: n<=100000,k<=9
Analysis:
There are many restrictions on the subject requirements, which require no ambiguity after substitution, and require the maximum value of the scheme, as well as the limit of k-system = =. Did not learn Huffman tree can think of is too powerful, learned (Noi this level certainly also learned) can think of and then set the model there are ideas.
I was weak, so I set the model of Huffman tree directly.
Huffman tree is generally two fork tree, the method of achievement is to choose two weights each time (that is, the number of occurrences) the smallest point, delete the 2 points, add a weight is the sum of the two points in the new point. Also the father of the 2 points to make this deleted becomes that new point.
When encoding the left branch and the right branch one is 11 is 0, from the root node to the leaf node through the edge of the 1/0 sequence is the leaf node corresponding coding.
However, the problem is K-fork tree, the method is similar to the above, but each time you choose K weight of the smallest point of time easy to let the last merger when the point of less than K. Assume that there are initially n points, and finally there are 1 points, and each merge deletes K points and puts them in 1 points. So easy to get: (n-1) is a multiple of (k-1). if (n-1)% (k-1)! =0, then the virtual points (k-1-(n-1)% (k-1)) are placed again, and their weights are 0, and they are also involved in finding the minimum k points.
However, the problem also requires that the maximum value of SI is minimal, so we let the dot represent a two-tuple (VAL,DEP) that represents the weight of the point and the depth of the point in the tree. When the minimum k points, the Val as the first comparison condition, if the Val value is equal, the DEP is small in front, so that at each merger, the depth of the small point will be the priority of merging, to ensure that the root to the leaves of the longest chain length as small as possible.
So, you can get the algorithm for this problem:
1) handle These n weights, add virtual points, the Val values of these points have been told above, DEP value is 0,ans=0;
2) each time before removing the small point of K, to ask for their Val sum, the maximum value of the DEP D, then put the new point should be (sum,d+1), put it into the original container and ordered, and Ans+=sum (Draw a Huffman tree, Consider the principle that the process of the length of the article can be so realized;
3) outputs the DEP value of ans and this point when there is only one point in the container.
This correctness can be guaranteed, but note the selection of the container, can not directly array simulation, will time out, can be used for heap optimization, I use the priority queue, and heap almost, so that the order of maintenance points into O (log n). Ask before K large number also do not have what advanced data structure, consider K is not big, priority queue one one pops up, bounce k times can.
The final time complexity is O (NLOGN).

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
using namespace Std;
typedef long Long LL;
LL Sum=0,mark;
int n,k;
typedef pair<ll,ll> INT;
struct cmp{
BOOL Operator () (const int A,const int b) const{
if (A.first!=b.first) return a.first>b.first;
Return a.second>b.second;
}
};
priority_queue< int,vector<int>,cmp >a;
int main ()
{
scanf ("%d%d", &n,&k);
for (int i = 1;i <= n;i + +) {

Int x;
x.second=1;
scanf ("%lld", &x.first);
A.push (x);

}
LL top=0;
if ((n-1)% (k-1)! = 0) Top + = k-1-(n-1)% (k-1);
for (int i=1;i<=top;i++) {
Int x;
x.first=0;
x.second=1;
A.push (x);
}
Top+=n;

while (top!=1) {

LL num=0;
mark=0;
Int x;
for (LL i=1;i<=k;i++) {

x = A.top ();
Num+=x.first;
Mark=max (Mark,x.second);
A.pop ();

}
Sum+=num;
X.first=num;
x.second=mark+1;
A.push (x);
Top-=k-1;
printf ("dot:%d,%d\n", P.first,p.second);
}
printf ("%lld\n%lld", Sum,mark);
return 0;
}

C + + K-binary Huffman Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.