POJ 3253 Fence Repair (priority queue)

Source: Internet
Author: User

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 NThe 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 Amou NT 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

At the beginning of their own writing when the time, found that the time-out, and then see Seniors to the puzzle to know the choice of priority queue, you can not know what the ghost, and then checked the next;

such as data, the priority queue is 8 8 5

Take the first two additions, and the queue for 8 sum=13, the second time sum=13+13+8=34;

The code is:

#include <iostream>#include<cstdio>typedefLong Longll; #include<vector>#include<queue>using namespacestd;intMain () {intN//number of planks to be cut     while(cin>>N) {priority_queue<ll,vector<ll>,greater<int> >Queue;//define priority queue, from big to small (team head small, Team tail big)         while(n--) {ll t; CIN>>T;       Queue.push (t); //Enter the required board length (fees) into the team} ll min=0;//Minimum cost         while(Queue.size () >1)//jumps out when the queue is less than or equal to an element{ll a=queue.top ();//get the value of the first element of the teamcout<<a<<Endl; Queue.pop ();//out Teamll B=queue.top ();//two times to take the first team, that is, to get the minimum two valuescout<<b<<Endl;            Queue.pop (); Queue.push (A+B);//Queuemin=min+a+b; } cout<<min<<Endl;  while(! Queue.empty ())//Empty QueueQueue.pop (); }    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.