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).
** 分析:**
首先,切割的方法可以参见二叉树来描述,该二叉树的每一个叶子节点就对应了切割出的一块块木板,叶子节点的深度就对应了为了得到对应木板所需的切割次数,开销的合计就是各叶子节点的模板的长度*节点的深度的总和。
则可以得到最短的板和次短的板的节点应该是兄弟节点。
将Li按照大小顺序排列,则最短的板应该是L1,次短的板应该是L2,也就意味着他们是从一块长度为L1+L2的板切割得来的。由于切割顺序是自由的,不妨当做是最后被切割。则在此次切割之前有(L1+L2),L3,L4···,LN,这样的N-1块木板存在。之后的思路同上。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
long long int ans=0;
int N;
scanf("%d",&N);
int a[20009];
for(int i=0; i<N; i++)
scanf("%d",&a[i]);
///N的值这里需要注意的是在不断地减少的,因为每一次的两个木板和并后,木板的个数就减少一个
while(N>1)
{
int min1=0,min2=1;
if(a[min1]>a[min2]) swap(min1,min2);///首先我们将第一个当做最小的,第二值当做第二小
for(int i=2; i<N; i++)
{
///其余的值分别于剩下的值比较,找出整个数组里面的最小和次小
{
if(a[i]<a[min1])
min2=min1;
min1=i;
}
else if(a[i]<a[min2])
{
min2=i;
}
}
int t=a[min1]+a[min2];///两个木板的长度即为这次的开销
ans+=t;
if(min1==N-1) swap(min1,min2);///因为要把最后一个去掉,所以min1的下标不能是最后一个
///把之前数组的值刷新
a[min1]=t;
a[min2]=a[N-1];
N--;
}
printf("%lld\n",ans);
return 0;
}
由于只需要从板的集合中取出最小的两块,并且把两块板长度之和加入到结合中即可,因此可以用优先队列高效的实现。
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
int main()
{
int n,num;
long long int ans=0;
priority_queue<int ,vector<int>,greater<int> >que;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&num);
que.push(num);
}
int num1,num2;
while(que.size()>1)
{
num1=que.top();
que.pop();
num2=que.top();
que.pop();
ans+=num1+num2;
que.push(num1+num2);
}
printf("%lld\n",ans);
return 0;
}