Fence Repair

第一部分:题目

题目链接:http://poj.org/problem?id=3253

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 37499   Accepted: 12160

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

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 should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
第二部分:思路
见代码注释
第三部分:代码
/*
要花最少的钱就是要总和最小,那么就希望每次锯断木板的长度最小。倒过来想:最后
一次锯断的肯定是最短的即每次锯断后的都是当前的最小值和次小指。
得出:每次计算最小值和次小指的和加上总和直到剩最后一个。 
*/
#include<iostream>
#include<algorithm> 
using namespace std;
int main()
{
    int n;
    long long sum=0;//由于N最大取20000,L最大取50000.所以总和用long long型。 
    long long l[20000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>l[i];
    }
    if(n>1)//有不止一块木板时进行计算 
    {
        int index=1;//次小值位置 
        //先排序,便于找到最小值和次小值。
        //(如果数据偏差不是很大的话,可以减少运行时间:便于后续最小值和次小值
        //的查找 ) 
        //比如:1 2 3 4 5。但是遇到:10 20 20 21 22 23,效果就没了 
        sort(l,l+n);
        long long min1,min2;//当前最小值和次小值
        min1=l[0];
        min2=l[1];
        while(index<n)
        {
            min1+=min2;
            sum+=min1;
            //把和存储在当前最小值位置,次小值往后移。然后寻找当前的最小值和次小值 
            min2=l[++index];//这里注意index先++。 
       //PS:可以每次进行排序,然后确定一下最小值和次小值的位置,就是index,index+1位置。
if(min1>min2) { swap(min1,min2); } for(int i=index+1;i<n;i++) { if(l[i]<min1) { swap(min2,min1); swap(min1,l[i]); } else { if(l[i]<min2) { swap(min2,l[i]); } } } } } else//只有一块木板就直接输出就好了。 { sum=l[0]; } cout<<sum<<endl; return 0; }

 

posted @ 2016-04-27 16:20  喝醉的香锅锅  阅读(442)  评论(0编辑  收藏  举报