Topic 1107: Moving Fruit (Huffman tree)

Source: Internet
Author: User
Tags ming

Topic 1107: Moving Fruit

time limit:1 seconds

Memory limit:32 MB

Special question: No

submitted:6189

Resolution:2178

Title Description:

In an orchard, Xiaoming had all the fruit hit down, and according to the different types of fruit into a number of piles, Xiao Ming decided to make all the fruits of a pile. Each time, Xiao Ming can combine the two piles of fruit and consume the same amount of energy as the weight of the two piles of fruit. Of course, after N‐1 merger, it becomes a pile. The total energy consumed by xiaoming during the merging of fruits equals the physical strength of each merger.

Assuming that each fruit weighs 1, and that the number of fruits and the number of each fruit is known, your task is to design a combination of the order, so that xiaoming consumes the least amount of energy, and output this minimum energy consumption value. For example, there are 3 kinds of fruit, the number is 1,2,9. You can merge the heap first, the number of new heaps is 3, the energy consumption is 3. The new heap is then combined with the original third heap to get a new heap, which consumes 12 of the energy. So Xiao Ming spent a total of physical =3+12=15, can prove that 15 is the minimum physical consumption value.

Input:

Each set of data input consists of two rows, the first line is an integer n (1<=n<=10000), which indicates the number of species of fruit, and if n equals 0 means that the input ends and is not processed. The second line contains n integers, separated by spaces, and the first integer (1<=ai<=1000) is the number of fruit of type I.

Output:

For each set of inputs, output an integer and wrap the value, which is the minimum physical cost. The input data guarantees that this value is less than 2^31.

Sample input:
39 1 20
Sample output:
15
#include <iostream>#include<stdio.h>#include<queue>using namespaceStd;priority_queue<int,vector<int>,greater<int> >Q;intMain () {intN;  while(SCANF ("%d", &n)!=eof&&n!=0)    {         while(Q.empty () = =false) Q.pop ();  for(intI=0; i<n; i++)        {            intx; scanf ("%d",&x);        Q.push (x); }        intans=0;  while(Q.size () >1)        {            intA=Q.top ();            Q.pop (); intb=Q.top ();            Q.pop (); Ans+=a+b; Q.push (A+b); } printf ("%d\n", ans); }    return 0;}

Topic 1107: Moving Fruit (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.