Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 26167   Accepted: 8459

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

#include <iostream>
using namespace std;
#define maxn 20010
long long int n;//目标板数
long long int len;//堆长
long long int p[maxn];//堆
void swap(long long int &a,long long int &b)
{
long long int temp;
temp=a;
a=b;
b=temp;
}
void heap_insert(long long int k)//将k插入到小根堆中。并维护堆性质
{
long long int t=++len;//将k插入到堆尾
p[t]=k;
while(t>1)//自上而下将k调整到合适的位置
{
if(p[t/2]>p[t])//若t的值大于其父节点的值,则交换。继续向上调整
{
swap(p[t/2],p[t]);
t=t/2;
}
else
break;
}
} void heap_pop()
{
long long int t=1;//将堆尾元素移到堆首
p[t]=p[len--];
while(2*t<=len)//调整堆首元素到合适位置,自上而下调整
{
long long int k=2*t;
if(k<len&&p[k]>p[k+1])//计算左右儿子中较小的节点序号k
k=k+1;
if(p[t]>p[k])
{
swap(p[t],p[k]);
t=k;
}
else
break;
}
}
int main()
{
cin>>n; long long int i;
for(i=1;i<=n;i++)
cin>>p[i]; for(i=1;i<=n;i++)
heap_insert(p[i]);//将n块木板的长度增加小根堆 long long int ans;//最小费用
ans=0; while(len>1)//构造哈夫曼树
{
long long int a,b;
a=p[1];//取堆首节点(权值a)。并维护其性质
heap_pop(); b=p[1];
heap_pop(); ans=ans+a+b;//将a和b累计计入最小费用中
heap_insert(a+b);//合并成一个权值插入到小根堆
}
cout <<ans<<endl;
return 0;
}

POJ 3253 Fence Repair(哈夫曼树)的更多相关文章

  1. Poj 3253 Fence Repair(哈夫曼树)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  2. poj 3253 Fence Repair (哈夫曼树 优先队列)

    题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...

  3. BZOJ 3253 Fence Repair 哈夫曼树 水题

    http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...

  4. POJ 3253 Fence Repair(哈夫曼编码)

    题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...

  5. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

  6. POJ 3253 Fence Repair(简单哈弗曼树_水过)

    题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...

  7. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  8. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  9. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  10. poj 3253 Fence Repair(优先队列+哈夫曼树)

    题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...

随机推荐

  1. mysql的三大范式

    关系数据库的几种设计范式介绍: 第一范式:确保每列的原子性(强调的是列的原子性,即列不能够再分成其他几列). 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足第一范式 ...

  2. NPM安装报错:WARN PACKAGE.JSON, NO REPOSITORY FIELDS

    今天在安装npm包时遇到了这个错误,出现如下提示: npm WARN package.json xxx@0.0.0 No repository field. npm WARN package.json ...

  3. 转:MyBatis学习总结(Mybatis总结精华文章)

    http://www.cnblogs.com/xdp-gacl/tag/MyBatis%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/ 当前标签: MyBatis学习总结   ...

  4. Java程序猿的JavaScript学习笔记(12——jQuery-扩展选择器)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  5. MFC对话框贴图基础上控件Stasic变成透明的

    对应WM_CTLCOLOR函数 加入下面代码: HBRUSH CMFCApplication2Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) ...

  6. Python全栈之路--Django ORM详解

    ORM:(在django中,根据代码中的类自动生成数据库的表也叫--code first) ORM:Object Relational Mapping(关系对象映射) 我们写的类表示数据库中的表 我们 ...

  7. linux程序设计——取消一个线程(第十二章)

    12.7    取消一个线程 有时,想让一个线程能够要求还有一个线程终止,就像给它发送一个信号一样. 线程有方法能够做到这一点,与与信号处理一样.线程能够被要求终止时改变其行为. pthread_ca ...

  8. linux在shell中获取时间 date巧用

    获得当天的日期 date +%Y-%m-%d 输出: 2011-07-28 date1=$(date --date='1 days ago +%Y%m%d')    #前一天的日期 date1=$(d ...

  9. Asp.Mvc将生成的视图保存为字符串

    public static class ViewExtensions { /// <summary> /// 在控制器内获取指定视图生成后的HTML /// </summary> ...

  10. html5 的a标签是可以拨电话的,通过其Href属性来实现

    <a href="tel:18700000000">点击给我打电话吧!</a> 注: 1.<a href="tel:18750000000& ...