poj 3253 Fence Repair

来源:互联网 发布:java ...参数 编辑:程序博客网 时间:2024/06/05 00:36
Fence Repair
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 39773 Accepted: 12970

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

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 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).

Source

USACO 2006 November Gold

提示

题意:

农夫要修理牧场的一段栅栏,他测量了栅栏,发现需要n(1<=n<=20000)块木头,每块木头长l(1<=l<=50000),于是他购买了一条很长的、能锯成N块的木头。但是农夫自己没有锯子,请人锯木的酬金跟这段木头的长度成正比。为简单起见,不妨就设酬金等于所锯木头的长度。例如,要将长度为20的木头锯成长度为8、5和7的三段,第一次锯木头花费20,将木头锯成12和8;第二次锯木头花费12,将长度为12的木头锯成7和5,总花费为32。如果第一次将木头锯成15和5,则第二次锯木头花费15,总花费为35(大于32)。请编写程序帮助农夫计算将木头锯成N块的最少花费。

思路:

队列,每次将最小的两块相加入队,入一次队排一次序,但TLE在等着你。

C++有现成的库函数——优先队列,这里不在多讲。

如果不使用库函数,那就先排序,之后还是每次将最小的两块相加,但需要找自己合适的位置入队,也就是插入排序了,时间复杂度比较高。

示例程序

C++库函数:

Source CodeProblem: 3253Code Length: 569BMemory: 808KTime: 32MSLanguage: G++Result: Accepted#include <cstdio>#include <algorithm>#include <queue>using namespace std;int main(){    long long i,k,x,y,n;//会有超过int的情况    priority_queue<int,vector<int>, greater<int> >q;    scanf("%lld",&n);    for(i=0;n>i;i++)    {        scanf("%lld",&k);        q.push(k);    }    k=0;//k将被用来进行加和并输出,相当于sum    while(!q.empty())    {        x=q.top();        q.pop();        if(q.empty())        {            break;        }        else        {            y=q.top();            q.pop();            k=k+x+y;            q.push(x+y);        }    }    printf("%lld",k);    return 0;}
C++的插入排序:
Source CodeProblem: 3253Code Length: 669BMemory: 512KTime: 1188MSLanguage: G++Result: Accepted#include <cstdio>#include <algorithm>using namespace std;int main(){    long long a[20000],i,i1,sum,y,n,cmin=0;//会有超过int的情况    scanf("%lld",&n);    for(i=0;n>i;i++)    {        scanf("%lld",&a[i]);    }    sort(a,a+n);//排一下序    for(i=0;n-1>i;i++)    {        sum=a[i]+a[i+1];        cmin=cmin+sum;        for(i1=i+2;n>i1;i1++)        {            if(sum>a[i1])            {                a[i1-1]=a[i1];                if(i1==n-1)                {                    a[i1]=sum;                }            }            else            {                a[i1-1]=sum;                break;            }        }    }    printf("%lld",cmin);    return 0;}

0 0