Fence Repair
http://poj.org/problem?id=3253
Time limit:2000ms
Memory limit:65536k
Description
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds, "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 him doesn ' t own a saw with which to cut the wood and 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 was exactly equal to its length. Cutting a plank of length costs cents.
Farmer Don then lets farmer John decide the order and locations to cut the plank. Help farmer John Determine the minimum amount of the $ he can spend to create the N planks. FJ knows and can cut the board in various different orders which'll 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 $ he must spend to make N-1 cuts
Sample Input
3
8
5
8
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 "a" 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 is would cost 21+13=34. If the second cut would the $ for a total of 5 instead (which are more than 34).
Source
Usaco 2006 November Gold
Water over ~
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Complete code:
/*32ms,480kb*/
#include <cstdio>
#include <queue>
using namespace std;
typedef long long LL;
Priority_queue<ll, Vector<ll>, greater<ll> > pq;///ascending
int main (void)
{
int n,len;
ll ans;
while (~SCANF ("%d", &n))
{
if (!pq.empty ()) Pq.pop ();
for (int i = 0; i < N; ++i)
scanf ("%d", &len), Pq.push (len);
Ans = 0;
while (Pq.size () > 1)
{
ll a = Pq.top ();
Pq.pop ();
ll B = Pq.top ();
Pq.pop ();
Ans + A + b;
Pq.push (A + b);
}
printf ("%lld\n", ans);
}
return 0;
}