题目链接:

  http://poj.org/problem?id=3253

题目大意:

  有一根木棍,需要截成n节,每节都有固定的长度,一根长度为x的木棒结成两段,需要花费为x,问截成需要的状态需要最小的花费?

解题思路:

  哈夫曼数,把每节需要的木棒长度看做树上的节点,把截木棍的过程倒过来,变成把n截木棍接起来,这两个过程的花费是一样的。根据哈夫曼的性质,可知先把最短的两个木棍连起来后,放到剩下的n-2根木棍中,再选取两个最短的连接起来,再放回去,直到全部的木根都连在一起就ok了。

代码:

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std; int main ()
{
priority_queue <int> Q;//优先队列,默认大的先出队,所以我们把进队的数取负
int n;
long long sum, num, m;
scanf ("%d", &n);
while (n --)
{
scanf ("%d", &m);
Q.push(-m);//进队
}
sum = ;
while (Q.size() > )//队列里面的元素个数要大于1
{
num = ; num += Q.top();//出队两个最短木棍
Q.pop(); num += Q.top();
Q.pop(); sum += num;//连接起来
Q.push(num);//进队
}
printf ("%lld\n", -sum);
return ;
}

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

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

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

  2. POJ 3253 Fence Repair (哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19660   Accepted: 6236 Des ...

  3. poj 3253 Fence Repair (优先队列,哈弗曼)

    题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...

  4. poj3253 Fence Repair【哈夫曼树+优先队列】

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

  5. POJ 3253 Fence Repair(修篱笆)

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

  6. poj 3253 Fence Repair 优先队列

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

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

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

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

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

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

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

随机推荐

  1. Callable线程

    写一个Callable线程 import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; im ...

  2. Python导入模块的几种姿势

    中文翻译:http://codingpy.com/article/python-import-101/ 英文原文:http://www.blog.pythonlibrary.org/2016/03/0 ...

  3. idea、jdk、eclispe中空main方法的线程数量不一样,why?

    測试代码: public class Test {     public static void main(String[] args) {         System.out.println(Th ...

  4. Meteor软件包管理

    Meteor 提供数千种开发应用程序,您可以使用社区包. 添加软件包 您可以查看Meteor官方包服务器: 点击这里. 只搜索你需要的包,并在命令提示符窗口中添加它. 例如,想使用 http 包添加到 ...

  5. 再说javascript 的__proto__ 和prototype 属性

    过了一段时间,没写 原生的 javascript 的了,感觉天天在用框架写代码,框架写代码完全限定死了你所需要思考的东西,只是在处理一些业务逻辑,真正的代码 都感觉不会写了. 突然发现,框架用的不熟悉 ...

  6. hibernate查询之Criteria实现分页方法(GROOVY语法)

    public int searchTest(String name, Integer pageIndex, List<Test> resultList){ def criteria = T ...

  7. vux tabbar 组件

    1.App.vue <!-- 入口文件 --> <template> <div id="app"> <!-- 视图层 --> < ...

  8. 全文检索(二)-基于lucene4.10的增删改查

    今天 用lucene完毕了 一个简单的web应用.提取了早期编写的一个測试类. 首先简单介绍下lucene几个经常使用包; lucene 包的组成结构:对于外部应用来说索引模块(index)和检索模块 ...

  9. pat(A)1041. Be Unique(哈希)

    1.链接:点击打开链接 2.代码: #include<cstdio> #include<iostream> #include<cstring> using name ...

  10. vue入门教程 (vueJS2.X)

    vue入门教程vueJS2.X 写在前面 看完此教程可以达到:能看懂并能修改简单的vue项目. 看的过程中,请把所有例子都放到html文件中跑一遍. Vue.js 是什么 Vue.js(读音 /vju ...