题目链接:http://poj.org/problem?id=3253

题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割前的长度。

有个陷阱就是需要用long long 去储存

Sample Input

3
8
5
8

Sample Output

34

就是将一块长为21的木板先切成13和8,花费21。然后将13切成5和8,花费13,总花费21 + 13 = 34。

分析:如果考虑从一块木板分割成小木板,方法数会有很多种,其实可以转化成从小木板合成大木板。

其实就是构造一棵最优二叉树(哈夫曼树),然后求出这棵树带权路径长度,过程如下:

题目可以转化为Huffman树构造问题 :

给定 N planks of woods,

1.   在 N planks 中每次找出两块长度最短的木板,然后把它们合并,加入到集合A中,

2.  在集合中找出两块长度最短的木板,合并加入到集合A中,重复过程,直到集合A中只剩下一个元素

显然,通过每次选取两块长度最短的木板,合并,最终必定可以合并出长度为 Sum(Li)的木板,并且可以保证总的耗费最少

所以采用优先队列,每次都取出两块最短的木板,然后结果加上这两块木板长度,再入队(这两块木板长度之和),直到木板之和为一块

#include<bits/stdc++.h>
const long long maxn = 2e4+;
using namespace std;
int main()
{
priority_queue<long long,vector<long long>, greater<long long> > q;
long long n;
scanf("%lld", &n);
if(n == )
{
long long l;
scanf("%lld", &l);
printf("%d\n", l);
return ;
}
if(n == )
{
long long l1, l2;
scanf("%lld %lld", &l1, &l2);
printf("%d\n",l1+l2);
return ;
}
while(n--)
{
long long t;
scanf("%lld", &t);
q.push(t);
}
long long res = ;
do
{
long long t1 = q.top();q.pop();
long long t2 = q.top();q.pop();
// printf("%d %d\n", t1, t2);
q.push(t1+t2);
res += t1+t2;
}while(q.size()!= );
printf("%lld\n",res);
return ;
}

poj 3253 Fence Repair (优先队列,哈弗曼)的更多相关文章

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

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

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

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

  3. poj 3253 Fence Repair 优先队列

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

  4. POJ - 3253 Fence Repair 优先队列+贪心

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

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

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

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

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

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

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

  8. poj 3253 Fence Repair(优先队列+huffman树)

    一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...

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

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

随机推荐

  1. _bzoj1001 [BeiJing2006]狼抓兔子【平面图】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 顺便推荐一个ppt,里面有对平面图的介绍:浅析最大最小定理在信息学竞赛中的应用. 这里 ...

  2. bzoj2581 [USACO 2012 Jan Gold] Cow Run【And-Or Tree】

    传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=110 传送门2:http://www.lydsy.com/JudgeOn ...

  3. *关于TCP长连接,NAT超时,心跳包

    参考: http://www.jianshu.com/p/584707554ed7 1.TCP长连接 TCP连接建立后只要不明确关闭,逻辑上连接一直存在. TCP是有保活定时器的,可以打开保活定时器来 ...

  4. 转】在Ubuntu中安装Cassandra

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/ 感谢! Posted: Mar 22, 2014 Tags: cas ...

  5. 在reset css后两个input之间还是出现默认间隔的问题。

    <div class="search_box fl"> <input type="text" class="search_text& ...

  6. jq星星评分

    html代码 <div class="make_mark"> <h5>请为这次服务打分</h5> <div class="mar ...

  7. 使用Jquery1.9 版本 来实现全选

    在使用Jquery实现全选以及反选的时候, 使用attr()实现的时候,在浏览器第一次运行可以全选,但是第二次再全选,不管用.  通过查找资料,用 prop()方法代替attr()方法就行了. 注意: ...

  8. Android学习笔记(十二) 线程

    Android中的线程和Java中的线程使用方法类似,参考(四)Java基础知识回顾 MainThread与WorkerThread UI相关的线程都运行在主线程(MainThread/UIThrea ...

  9. Vue.js——打包之后资源路径产生问题

    https://blog.csdn.net/qq_30632003/article/details/79353035 https://www.cnblogs.com/diantao/p/7776523 ...

  10. Oracle11g 审计介绍

    审计是记录数据库上方方面面操作.事件等信息,是数据安全管理的重要手段. 开启审计,虽然不同级别的审计会有不同,但是对数据库的性能是有影响的,并且占用存储空间. --1.创建审计数据专用表空间 crea ...