AYITACM2016 Province Fourth week c-fence Repair (priority queue, Max heap)

Source: Internet
Author: User

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1≤ n ≤20,000) Planks of wood, each having some intege R length Li (1≤ li ≤50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of The lengths Li). FJ is ignoring the ' kerf ', the extra length lost to sawdust when a sawcut is made; You should ignore it, too.

FJ sadly realizes that he doesn ' t own a saw with which-cut the wood, so he mosies over to Farmer Don's Farm with this L Ong board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn ' t lend FJ a saw but instead offers to charge Farmer John for each of the N -1 cuts in the plank. The charge to cut a piece of wood are exactly equal to its length. Cutting a plank of length costs cents.

Farmer Don and lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which would result in different charges since the resulting Intermediate planks is of different lengths.

Input

Line 1:one Integer NThe number of planks
Lines 2.. N+1:each line contains a single integer describing the length of a needed plank

Output

Line 1:one integer:the Minimum amount of money he must spend to make N-1 cuts

Sample Input

3858

Sample Output

34

Hint

He wants to cut a board of length into pieces of lengths 8, 5, and 8.
The original board measures 8+5+8=21. The first cut would cost, and should is used to cut the board into pieces measuring and 8. The second cut would cost, and should is used to cut the 8 and 5. This would cost 21+13=34. If the are cut into and 5 instead, the second cut would cost + for a total of PNS (which is more than 34).




Analysis:

Constructs the Huffman tree for the input data, where the value of the non-leaf node is added together and is required.

Huffman Tree Construction Process: Each take the number of the two minimum, add up to get a non-leaf node value, the next time you fetch the number of the previous two minimum number can not be taken, and then add the value of the non-leaf node to be taken.

For example, 3, 4, 5, First take 3, 4 add 7, then take 5, 7 (3,4 can not be taken again), add to 12, 7+12 = 19, that is, the request.

The priority queue is a good implementation of this operation.

#include <stdio.h> #include <string> #define INF 0x3f3f3f3f#include<algorithm> #include <queue >using namespace Std;int Main () {    int i,j,n,a[50010];    Priority_queue<int,vector<int>,greater<int> >q;//Large Top pile//    priority_queue<int,vector< int>,less<int> >q;//Small Top heap    scanf ("%d", &n);//Priority queue comes with sort, big top heap default from small to large row, small top heap from smallest to largest for    (i=0; i<n; i++    {        scanf ("%d", &a[i]);        Q.push (A[i]);    }    Long long s=0;    while (!q.empty ())    {        int n1=q.top ();//Find two smallest as child        q.pop ();        int n2=q.top ();        Q.pop ();        S+=N1+N2;  Add the value of the parent node        if (Q.empty ()) break            ;        Q.push (N1+N2);//Put the parent node in the queue, re-assemble    }    printf ("%i64d\n", s);    return 0;}



AYITACM2016 Province Fourth week c-fence Repair (priority queue, Max heap)

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.