农夫约翰修篱笆-堆排序

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

题意为农夫约翰想要修篱笆,先给了篱笆要分为几块,然后每块分为给定的长度,一单位的长度就等于一个金币,如何切割才能花费最少,比如8,5,8,总和为21,把21分为13和8,在把13分为5和8,共需21+13=34个金币。

每次找最小的两段长度,这样才能保证它们的和最小,可以先建立最小堆,每次删除顶部元素,即最小的元素,每删够两次,把这两次的和再加入最小堆的末端,再次进行堆排序,每删够两次据一次,直到据的次数等于要分为的块数减去一,代码如下。

#include<stdio.h>
int h[21000],n;
void swaap(int x,int y)
{
    int t;
    t=h[x];
    h[x]=h[y];
    h[y]=t;
    return;
}
void siftdown(int i)
{
    int t,flag=0;
    while(i*2<=n&&flag==0)
    {
        if(h[i]>h[i*2])
            t=i*2;
        else
            t=i;
        if(i*2+1<=n)
        {
            if(h[t]>h[i*2+1])
                t=i*2+1;
        }
        if(t!=i)
        {
            swaap(t,i);
            i=t;
        }
        else
            flag=1;
    }
    return;
}
void creat()
{
    int i;
    for(i=n/2;i>=1;i--)
    {
        siftdown(i);
    }
    return;
}
int deletemax()
{
    int t;
    t=h[1];
    h[1]=h[n];
    n--;
    siftdown(1);
    return t;
}
int main()
{
    int num,i,v=0,v1=0,v2=0,mm;
    scanf("%d",&num);
    for(i=1;i<=num;i++)
        scanf("%d",&h[i]);
    n=num;
    long long sum=0;
    creat();
    for(i=1;;i++)
    {
        if(v==2)
        {
            h[++n]=v1;
            v1=0;
            v=0;
            siftdown(1);
            v2++;
            //printf("%d**\n",h[n]);
        }
        else
        {
            mm=deletemax();
            sum=sum+mm;
            v1=v1+mm;
            //printf("%d&&\n",sum);
            v++;
        }
        if(v2==num-1)
            break;
    }
    printf("%lld\n",sum);
    //for(i=3;i<=num+2;i++)
        //printf("%d ",deletemax());
    return 0;
}
发布了30 篇原创文章 · 获赞 1 · 访问量 242

猜你喜欢

转载自blog.csdn.net/weixin_46204099/article/details/105128539
今日推荐