POJ 3253 Fence Repair (priority queue)

Source: Internet
Author: User

POJ 3253 Fence Repair (priority queue)

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1≤ n ≤20,000) Planks of wood, each having some integer LengthLi (1≤ li ≤50,000) units. He then purchases a single long board just long enough to saw into theN 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 theN- 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 theN 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.

InputLine 1:one integer N, the number of planks Lines 2. N+1:each line contains a single integer describing the length of a needed plankOutputLine 1:one integer:the Minimum amount of money he must spend to make N-1 cutsSample 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).

Test instructions: There is a farmer to make a plank into a few pieces of a given length of small planks, each cost is the length of the current saw this plank of a given number of small planks n, the length of each requirement of the small plank, to find the minimum cost

< Span style= "color: #008000;" > ans=ans+13,   13 is the countdown to the first step of sawing Wood's behavior of plank length,  then 13 8 after entering the queue automatically with a small priority is 8 13,   in the penultimate step of the saw wood behavior (in the title data is the first step, because saw two times), 8+13=21,      ans=ans+21,    so the ultimate ans min get

If you have questions about the definition of a priority queue, see http://www.cnblogs.com/WHLdbk/articles/5693348.html

Here is the code implementation

1 #include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <queue>7#include <vector>8 using namespacestd;9 intMain ()Ten { Onepriority_queue<int, vector<int>,greater<int> >q;//greater<int> A small priority, team Head team tail Big A     intN,i,j,k,a,b;__int64 Num,ans; -      while(cin>>N) -     { theans=0; -          for(i=0; i<n;i++) -         { -Cin>>num; +Q.push (num);//take a small priority into the team -         } +          while(Q.size () >1)//when the element in the queue jumps out of the loop for a moment, it is impossible for one because it is 22 out of the team A         { atA=q.top ();//team head elements out of the team -Q.pop ();//team head Element Elimination -B=q.top ();//team head elements out of the team -Q.pop ();//team head Element Elimination -Q.push (A+B);//a+b into the team, automatically according to the team head small to the tail of the big order to shoot their own position -ans+=a+b; in         } -cout<<ans<<Endl; to          while(!q.empty ()) + Q.pop (); -     } the     return 0; *}

POJ 3253 Fence Repair (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.