POJ -3253 Fence Repair

Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 35076   Accepted: 11332

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 integer 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 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 of the N-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 create the N planks. FJ knows that he can cut the board in various 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

3
8
5
8

Sample Output

34

分析
我们还是先从决策的角度来看看这个问题,首先如果我们完全顺着题目的要求来,那么我们现在就有了一块总长度为所有木板长度和的大木板,我们要进行n-1次切割,那么我们的决策无疑就是从那里进行切割,具体来说这包括两个问题,当我们有多块木板时我们选择哪块木板进行切割,当我们选定了一块木板时,我们在这块木板的那里进行切割,那么我们根据这两种决策,就要制定两个策略,我们的首要策略当然是“使我们的成本最小”,首先我们来看我们要选择那一块木板进行切割,因为我们一次切割的产生的成本就是要待切割板子的长度,那么如果我们每次选择最小的板子(出去已经切好的)进行切割,这样我们每次都选择最小,的就会产生最小的总成本,那我们再看第二个问题,我们要从一个板子的那个部位进行切割,当然我们还是要考虑使得切割的成本最小,但是我们发现,不管从该板子的哪个部位进行切割,我们这次决策的成本都是这个板子的长度,但是我们在该板子的不同部位进行切割所产生的结果却会在后面的决策中产生不同的成本,所以决定我们当前决策的依据并不在我们进行决策之前,而在之后,这就提醒我们我们应该逆向来思考这个问题,如果我们一旦把问题倒过来思考,那么我们的依据就会出现在决策之前。

如果把问题倒过来看,那么我们的问题,就变成了合并两个板子,那我们就会发现这就是 合并果子啊!

你可能感兴趣的:(算法,动态规划,ACM,优先队列,贪心)