POJ3253

本文介绍了一个关于如何最小化切割成本的问题,通过合理安排切割顺序来减少总费用。农夫约翰需要将一块长板切割成若干小木板,每次切割的成本等于当前木板的长度。文章探讨了如何通过算法找到最佳切割顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


USACO 2006 November Gold


描述
农夫约翰想在牧场周围修一小段篱笆。他措施围栏发现他需要N(1≤N≤20000)木板,各有一些整数长度的李(1≤李≤50000)单位。然后他买了一块长板,刚好能看到N块木板(即长度是李的长度之和)。福建是忽略了“切”的额外长度失木屑当开槽了;你应该忽略它,太。
福建悲哀地意识到他没有看到用来切木材,所以他mosies到Farmer Don农场这个长板并礼貌地问他是否可以借一看见。
Farmer Don,一个衣柜的资本主义,不借给FJ看见而是提供收取农民约翰为每个在木板的N-1的削减。切一块木头的费用等于它的长度。切割一块长度为21的木板要花21美分。
农民唐然后让农民约翰决定秩序和地点削减木板。帮助农民约翰确定他能用来创造n块木板的最少的钱。FJ知道他可以剪切板在各种不同的命令,会导致不同的费用,由于中间木板是不同的长度。
输入
第1行:一个整数n,木板的个数
第2行…n + 1:每行包含一个整数,描述所需的板的长度。
输出
第1行:一整数:他必须花费最少的钱来做n-1的削减。
样本输入……

提示
他想把长度为21的木板切成长度为8, 5和8的长度。
原来的董事会措施8 + 5 + 8 = 21。第一次切割将花费21,并且应该被用来将板切成13和8的碎片。第二次切割将花费13,并且应该被用来切割13到8和5。这将花费21 + 13 = 34。如果21个被切割成16和5,那么第二个切割将花费16,总共37(超过34)。

#include 
using namespace std;
int find_small(int arr[], int len, int &a, int &b){//a是最小的,b是第二小的  
    a=b=99999;  
    int a_index, b_index, i;   
    a_index = b_index = 0;  
    for(i=0; i>n;  
    for(int i=0; i>arr[i];  
    }  
    cnt = n;  
    int w=0;  
    if(cnt==1){  
        w = arr[0];  
    }  
    while(cnt!=1){  
        --cnt;  
        int a,b,hole;  
        hole = find_small(arr,n,a,b);  
        w = w+a+b;  
        arr[hole] = a+b;  
    }  
    cout<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值