Blue Bridge Cup Java Basics Practice Huffuman Tree

Source: Internet
Author: User

The problem Description Huffman tree is widely used in coding. Here, we only care about the construction of the Huffman tree.
Give a column number { Pi}={ P0, P1,..., PN-1}, the process of constructing the Huffman tree with this number of columns is as follows:
1. Find { Pi}, set the minimum number of two PAAnd PBWill PAAnd PBFrom Pi}, and then add them to the { PiIn The cost of this process is recorded as PA+ PB
2. Repeat step 1 until { PiOnly one number is left.
The total cost of constructing the Huffman tree is obtained by summing up all the expenses during the above operation.
Task: For a given sequence of numbers, now ask you to find out the total cost of constructing the Huffman tree with this series.

For example, for the sequence { Pi}={5, 3, 8, 2, the 9},huffman tree is constructed as follows:
1. Find the smallest 2 numbers in {5, 3, 8, 9, two}, respectively, 2 and 3, from { PiDelete them and add 5 to the {5, 8, 9, 5} and the cost is 5.
2. Find the smallest 5 numbers in {5, 8, 9, two}, respectively 5 and 5, from { PiDelete them and add 10 to the {8, 9, 10} and the cost is 10.
3. Find the smallest two numbers in {8, 9, 10}, respectively 8 and 9, from { PiDelete them and add 17 to the {10, 17} and the cost is 17.
4. Find the minimum two numbers in {10, 17}, respectively 10 and 17, from { PiDelete them and add 27 to the {27} at a cost of 27.
5. Now, only one number 27 is left in the sequence, and the construction process ends with a total cost of 5+10+17+27=59. Input format the first line of input contains a positive integer NN<=100).
Next is NA positive integer that represents P0, P1,..., PN-1, no more than 1000 per number. The output format output uses these numbers to construct the total cost of the Huffman tree. Sample Input 5
5 3 8 2 9 Sample output 59
Import Java.util.scanner;import Java.util.priorityqueue;public class Main {static priorityqueue<integer> q;  public static void Main (string[] args) {Scanner sc = new Scanner (system.in); q = new priorityqueue<integer> (); int n = Sc.nextint (); for (int i = 0; i < n; i++) {Q.offer (Sc.nextint ());} int ans = 0;int tmp1, tmp2;int ans1 = 0;while (!q.isempty () && q.size () >= 2) {ans = 0;TMP1 = Q.poll (); tmp2 = q . Poll (); ans = tmp1 + tmp2;q.offer (ans); ans1 + = ans;} System.out.println (ANS1);}}

  

Blue Bridge cup Java Basics Practice Huffuman tree

Related Article

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.