POJ—3253—Fence Repair—【贪心】【优先队列】

Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 26474 Accepted: 8588

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



方法一:贪心——Huffman编码的思想

贪心策略:每次找最小min_one的和次小min_two,然后合并起来得min,也就是说,要得到min_one和min_two,就要锯min,money就要加上min。。。然后把min_one和min_two删掉,用min代替,重复上述步骤。。。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define LL __int64

LL plank[20010];

int main()
{
    int N;
    scanf("%d",&N);
    for(int i=0;i<N;i++)
        scanf("%I64d",plank+i);
    LL ans=0;     //总的费用。。。。。。

    while(N>1)      //
    {
        int min_one=0,min_two=1;    //最小的min_one,初始化为第1个,次小的min_two,初始化为第2个
        if(plank[min_one]>plank[min_two])
            swap(min_one,min_two);  //每次开始,确保plank[min_one]<=plank[min_two]

        for(int i=2;i<N;i++)    //找到最小的min_one,和次小的min_two  //i一定要从2开始。。。
            if(plank[i]<plank[min_one])
            {
                min_two=min_one;
                min_one=i;
            }
            else if(plank[i]<plank[min_two])
                min_two=i;

//        printf("min_one=%d\nmin_two=%d\n\n",min_one,min_two);
        LL temp=plank[min_one]+plank[min_two];  //把最小的两个合并
        ans+=temp;

        if(min_one==N-1)
            swap(min_one,min_two);  //如果最小的那个正好是数组最后一个,那么交换
        plank[min_one]=temp;        //plank[min_one]存放最小的那个
        plank[min_two]=plank[N-1];  //上面如果最小的那个正好是数组最后一个,交换后就是自己等于自己,如果上面不是最后一个,把最后一个赋给plank[min_two],因为下面N--,不赋值的话就会少了。。。
        N--;
    }
    printf("%I64d\n",ans);
    return 0;
}


法二:优先队列

想法和上面是一样的,但是用了STL实现,比较方便,O(nlogn)的复杂度也比上面O(n^2)快很多。。。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
#define LL __int64

LL planks[20010];
int main()
{
    priority_queue <int ,vector<int>,greater<int> > que;    //声明一个从小到大取出数值的优先队列
    LL ans=0;   //需要的money
    int N;
    scanf("%d",&N);
    for(int i=0;i<N;i++)
    {
        scanf("%I64d",planks+i);
        que.push(planks[i]);
    }

    while(que.size()>1) //循环到只剩下一块木板
    {
        LL min_one ,min_two;
        min_one=que.top();
        que.pop();
        min_two=que.top();
        que.pop();
        ans+=min_one+min_two;   //合并
        que.push(min_one+min_two);
    }
    printf("%I64d\n",ans);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值