Fence Repair
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 29658 |
|
Accepted: 9643 |
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).
Source
Usaco 2006 November Gold This topic is easy to associate with Huffman tree, but this topic can be used in priority queue to write. So let's introduce the priority queue here. priority_queue< element types, container types, comparison operators >; There are two kinds of 1.greater<>2.less<> operators. The operator's header file needs to be #include<functional>, or it will compile incorrectly.
#include <iostream> #include <queue> #include <cstdio> #include <algorithm> #include <vector >using namespace Std;int Main () {Priority_queue<int,vector<int>,greater<int> >q;int n,k;while (~ scanf ("%d", &n)) {for (int i=0;i<n;i++) {scanf ("%d", &k); Q.push (k);} int Temp=0;long Long Ans=0;while (q.size () >1) {temp+=q.top (); Q.pop (); Temp+=q.top (); Q.pop (); Ans+=temp;q.push (temp ); temp=0;} printf ("%lld\n", ans);} return 0;}
POJ-3253 Priority Queue STL