Poj3253 fence repair STL priority queue

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question link: http://poj.org/problem? Id = 3253

Description

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 intoNPlanks (I. e., whose length is the sum of the lengthsLi). Fj is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; You shoshould ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to farmer don's farm with this long 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 ofN-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then 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 createNPlanks. fj knows that he can cut the board in varous different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, The 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 21 into pieces of lengths 8, 5, and 8.
The original Board measures 8 + 5 + 8 = 21. the first cut will cost 21, and shoshould be used to cut the Board into pieces measuring 13 and 8. the second cut will cost 13, and shoshould be used to cut the 13 into 8 and 5. this wocould cost 21 + 13 = 34. if the 21 was cut into 16 and 5 instead, the second cut wocould cost 16 for a total of 37 (which is more than 34 ).

Source

Usaco 2006 November gold

General question:

A farmer wants to crop a board into several small boards with a given length. Each saw is charged for the length of the current wood board.

The minimum cost is calculated based on the length of each sub-board and the number n of sub-boards.

 

Tip:

To

3

5 8 5 For example:

First saw a 21-length wooden board from an infinitely long wooden board, and spent 21

Then, we saw 5 boards from a wooden board with a length of 21 and spent 5

Then, we saw 8 boards from a 16-length board and spent 8

Total cost = 21 + 5 + 8 = 34

 

Solution:

Using the Huffman idea, to minimize the total cost, you can only add the two boards with the minimum length each time, and then add the "and" to the total cost.

Although this question uses the Huffman idea, it will time out to directly use the huffmantree. You can use the priority queue to do it.


The Code is as follows:


/* STL priority queue */# include <cstdio> # include <queue> # include <vector> # include <iostream> using namespace STD; int main () {int N; // number of boards to be cut _ int64 temp, A, B, mincost; while (~ Scanf ("% d", & N) {// defines a priority queue from small to large. You can change greater to less, that is, from large to small priority_queue <int, vector <int>, greater <int> q; while (! Q. empty () // clear the queue Q. pop (); For (INT I = 1; I <= N; I ++) {scanf ("% i64d", & temp); q. push (temp); // enter the required length (cost) and join the team} mincost = 0; // The minimum fee is initially zero while (Q. size ()> 1) // when the queue is smaller than or equal to one element, it jumps out of {A = Q. top (); // obtain the value of the first element of the team and enable it to exit Q. pop (); B = Q. top (); // take the first line twice, that is, obtain the minimum two values Q. pop (); q. push (a + B); // put the two smallest elements into the queue mincost + = a + B;} printf ("% i64d \ n", mincost );} return 0 ;}


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.