【队列,树】poj 3253

Fence Repair
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 26380
Accepted: 8554

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into theN 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 theN-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 theN 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 makeN-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,求最小费用


提示:

3

5 8 5为例:

先从无限长的木板上锯下长度为 21 的木板,花费 21

再从长度为21的木板上锯下长度为5的木板,花费5

再从长度为16的木板上锯下长度为8的木板,花费8

总花费 = 21+5+8 =34


思路:

利用Huffman思想,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可

本题虽然利用了Huffman思想,但是直接用HuffmanTree做会超时,可以用优先队列做

 

因为朴素的HuffmanTree思想是:

1)先把输入的所有元素升序排序,再选取最小的两个元素,把他们的和值累加到总费用

2)把这两个最小元素出队,他们的和值入队,重新排列所有元素,重复(1),直至队列中元素个数<=1,则累计的费用就是最小费用

 

HuffmanTree超时的原因是每次都要重新排序,极度浪费时间,即使是用快排。


一个优化的处理是:

1)只在输入全部数据后,进行一次升序排序  (以后不再排序)

2)队列指针p指向队列第1个元素,然后取出队首的前2个元素,把他们的和值累计到总费用,再把和值sum作为一个新元素插入到队列适当的位置

     由于原队首的前2个元素已被取出,因此这两个位置被废弃,我们可以在插入操作时,利用后一个元素位置,先把队列指针p+1,使他指向第2个废弃元素的位置,然后把sum从第3个位置开始向后逐一与各个元素比较,若大于该元素,则该元素前移一位,否则sum插入当前正在比较元素(队列中大于等于sum的第一个元素)的前一个位置

3)以当前p的位置作为新队列的队首,重复上述操作

 

另一种处理方法是利用STL的优先队列,priority_queue,非常方便简单高效,虽然priority_queue的基本理论思想还是上述的优化思想,但是STL可以直接用相关的功能函数实现这些操作,相对简单,详细参见我的程序。

注意priority_queueqsort的比较规则的返回值的意义刚好相反


参考博文: http://blog.csdn.net/lyy289065406/article/details/6647423


代码1:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=20020;

int s[maxn];

int main()
{
    int i,j,k,n;
    long long ans=0,sum=0;
    cin >> n;
    for(i=0;i<n;i++)
        cin >> s[i];
    sort(s,s+n);
    sum = ans = 0;
    for(i=0;i<n-1;i++)
    {
        sum=s[i]+s[i+1];
        ans+=sum;
        for(j=i+2;j<n;j++)
        {
            if(s[j]<sum)
            {
                s[j-1]=s[j];
                if(j==n-1)
                {
                    s[j]=sum;
                    break;
                }
            }
            else
            {
                 s[j-1]=sum;
                 break;
            }
        }
    }
    cout << ans <<endl;
    return 0;
}
//AC 563MS

代码2:优先队列

#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

struct cmp
{
    bool operator()(int &a,int &b)
    {
        return a > b;
    }
};

priority_queue<int,vector<int>,cmp> q;

int main()
{
    int i,j,k,a,n;
    long long t,sum=0;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> a;
        q.push(a);
    }
    t = 0;sum = 0;
    while(q.size()>1)
    {
        t=q.top();
        //cout << q.top()<<' ';
        q.pop();
        t += q.top();
        //cout << q.top()<<' ';
        q.pop();
        sum += t;
        q.push(t);
    }
    while(!q.empty())
        q.pop();
    cout << sum <<endl;
    return 0;
}
//priority_queue
//AC  C++94MS  G++141MS

体会:很难想到用哈弗曼树的思想,STL用起来很方便


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值