poj 3253 Fence Repair (水哈夫曼树)
题目链接:
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 (水哈夫曼树)的更多相关文章
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
- POJ 3253 Fence Repair (哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19660 Accepted: 6236 Des ...
- poj 3253 Fence Repair (优先队列,哈弗曼)
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...
- poj3253 Fence Repair【哈夫曼树+优先队列】
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
- Poj 3253 Fence Repair(哈夫曼树)
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
随机推荐
- HttpUtils 用于进行网络请求的工具类
原文:http://www.open-open.com/code/view/1437537162631 import java.io.BufferedReader; import java.io.By ...
- datasnap中间件如何控制长连接的客户端连接?
ActiveConnections: TClientDataSet; ... 有客户端连接上来的时候 procedure TForm8.DSServer1Connect(DSConnectEventO ...
- 通过通过url routing解决UIViewController跳转依赖
XYRouter https://github.com/uxyheaven/XYRouter XYRouter是一个通过url routing来解决UIViewController跳转依赖的类. * ...
- 一个很小的C++写的MVC的例子
#include<iostream> #include<vector> //get namespace related stuff using std::cin; using ...
- 【iOS系列】-autorelease的作用
内存管理原则(配对原则):只要出现了new,alloc,retain方法,就要配对出现release,autorelease 1:对象存入到自动释放池中,当这个池子被销毁的时候他会对池子中所有的对 ...
- js可视区域图片懒加载
可视区域图片懒加载 实现原理,页面滚动时获取需要懒加载的图片,判断此图片是否在可视区域内,是则设置图片data-src地址为src地址,加载图片. html下载地址 <!DOCTYPE html ...
- 【Android】获取控件的宽和高
有时候我们须要在Activity的时候获取控件的宽和高来做一些操作,以下介绍三种获取宽和高的方式: 1. onWindowFocusChanged @Override public void onWi ...
- Codeforces 794F. Leha and security system 线段树
F. Leha and security system Bankopolis, the city you already know, finally got a new bank opened! ...
- github相关
1 某次release的源码 某次release的源码在release列表中,不在branch中,tag和release是在一起的.所以,下载某个release的源码应该去release中找,而不应该 ...
- Django's CSRF mechanism
Forbidden (403) CSRF verification failed. Request aborted. You are seeing this message because this ...