poj3253 Fence Repair "Huffman Tree + Priority Queue"

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).



This is the code that was written at the time of the game, although the answer is correct, but there is a problem

Is it not to cut a length of 21? I think of a beginning is 21, first cut into 8 and 13, 21, and then 13 and 8 to cut 5, need 13 13+21=34; and then I put the voxel in the array

From large to small arranged, so that 21 plus less, the flower of gold is less, from the big to the small arrangement,


But the wrong n times, after the game I looked at other people's code, is Huffman tree + priority queue, each take two the smallest number, sticky into a long, every time this


But I don't know where my code is wrong.

#include <cstdio> #include <algorithm>using namespace Std;int cmp (__int64 X,__int64 y) {    return x>y;} __int64 A[20100];int Main () {    int n,i;    while (~SCANF ("%d", &n))    {        __int64 s=0;        for (I=0;i<n;++i)                {            scanf ("%i64d", a+i);            S+=a[i];        }                        Sort (a,a+n,cmp);        __int64 sum=0;        for (I=0;i<n;++i)        {            if (s!=a[n-1])            {                sum+=s;                S-=a[i];            }                    }        printf ("%i64d\n", sum);}    }



/* Test instructions: Chop a piece of wood, the current block is how long it takes how many gold coins, to spend the least amount of gold coins to cut into the length of the wood you want *///Huffman tree + priority queue POJ 3253//per implementation of the minimum two numbers added #include <cstdio> #include <algorithm> #include <queue>using namespace Std;int main () {int N,i;__int64 a,b;__int64 m;scanf ("%d", &n ); {priority_queue<__int64,vector<__int64>,greater<__int64> >q;for (i=0;i<n;++i) {scanf ("%I64d") , &m); Q.push (m);} __int64 Sum=0;while (Q.size () >1) {a=q.top (); Q.pop (); b= q.top (); Q.pop (); Sum+=a+b;q.push (a+b);} printf ("%i64d\n", Sum);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

poj3253 Fence Repair "Huffman Tree + Priority Queue"

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.