poj 3253 Fence Repair(模拟huffman树 + 优先队列)
题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价。
思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列。
注意priority_queue的用法,原型:
priority_queue<Type> q;
priority_queue<Type,deque<Type>,Comp> q;
其中Type是类型,Comp是比较结构体,比较函数是它的括号重载,比如对int型从小到大排序的Comp结构体如下:
struct Comp
{
bool operator()(const LL& a,const LL&b) const
{
return a>b;
}
};
还有要注意的是,不能直接while(!q.empty()),因为每次选取两个数,如果队列只剩下一个数时又要取两个数,此时程序会爆掉,应该用一个while(1)循环,在取出一个数时立刻判断if(q.empty()) break;
不能直接模拟huffman树,直接huffman会超时。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000000
#define inf 1e12
ll n;
struct Comp{
bool operator()(const ll &a,const ll &b)const{
return a>b;
}
};
int main()
{
while(scanf("%I64d",&n)==){
priority_queue<ll,deque<ll>,Comp> q;
for(int i=;i<=n;i++){
ll x;
scanf("%I64d",&x);
q.push(x);
}
ll ans=;
while(){
ll L=q.top(); q.pop();
if(q.empty()) break;
ll R=q.top(); q.pop();
ans+=L;
ans+=R;
q.push(L+R);
}
printf("%I64d\n",ans);
}
return ;
}
poj 3253 Fence Repair(模拟huffman树 + 优先队列)的更多相关文章
- [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
- poj 3253 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 优先队列 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 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- poj 3253 Fence Repair(优先队列+huffman树)
一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- poj 3253 Fence Repair (优先队列,哈弗曼)
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...
随机推荐
- spring-boot+nginx+tomcat+ssl配置笔记
如果你的tomcat应用需要采用ssl来加强安全性,一种做法是把tomcat配置为支持ssl,另一种做法是用nginx反向代理tomcat,然后把nginx配置为https访问,并且nginx与tom ...
- python多线程简单例子
python多线程简单例子 作者:vpoet mail:vpoet_sir@163.com import thread def childthread(threadid): print "I ...
- Python的if判断与while循环
1.if判断 Python 编程中 if 语句用于控制程序的执行,基本形式为: if 判断条件: 执行语句 else: 执行语句 Python中使用缩进代替c语言中的大括号,来告诉程序所执行的内容. ...
- UIView 弹出动画
// 展开动画 - (void)beginAnimations { CGContextRef context = UIGraphicsGetCurrentContext(); [UIView begi ...
- DM6437 C64X+ EDMA 疑惑总结记录
总结一下DM6437中的EDMA的使用出现的问题,方便以后再开发定位问题. 1.EDMA Link 和 Chain的区别 link实现了DMA的自动重加载(非静态模式),需要两个param chain ...
- indesign 注意事项
画册 42 * 28.5加出血 42.6 * 29.1用纸 889 * 1194 注意事项:indd文件打印需转曲线 快捷键:ctrl+shift+O ctrl+shift+G (2)应用图片需单独创 ...
- Oracle SQL函数之转换函数
chartorowid(c1) [功能]转换varchar2类型为rowid值 [参数]c1,字符串,长度为18的字符串,字符串必须符合rowid格式 [返回]返回rowid值 [示例] SQL> ...
- Sybase配置中文语言支持及字符集
在windows平台上,Sybase ASE15.0.7安装完成后默认语言是:英语(us_english),默认字符集为:cp850.cp850是一个西欧字符集,虽然也能使用cp850字符集保存汉字, ...
- java学习之Date的使用
Date使用,主要要注意将日期格式化,否则返回的是系统默认的格式.请自己查阅API文档. import java.util.*; import java.text.*; public class Te ...
- web前端的学习误区
web前端的学习误区 网页制作是计算机专业同学在大学期间都会接触到的一门课程,而学习网页制作所用的第一个集成开发环境(IDE)想必大多是Dreamweaver,这种所见即所得的“吊炸天”IDE为我们 ...