题意:如果要切断一个长度为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树 + 优先队列)的更多相关文章

  1. [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25274   Accepted: 8131 Des ...

  2. poj 3253 Fence Repair (贪心,优先队列)

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

  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 (优先队列)

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

  5. POJ 3253 Fence Repair(修篱笆)

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

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

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

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

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

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

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

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

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

随机推荐

  1. Randomized QuickSelect

    In this blog, we give a solution for Quick Select. Here, we have an improvement. The idea is to rand ...

  2. Valid Anagram 解答

    Question Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...

  3. SHDP--Working With HBase (二)之HBase JDBC驱动Phoenix与SpringJDBCTemplate的集成

    Phoenix:Phoenix将SQL查询语句转换成多个scan操作,并编排执行最终生成标准的JDBC结果集.   Spring将数据库访问的样式代码提取到JDBC模板类中,JDBC模板还承担了资源管 ...

  4. Nginx学习之四-Nginx进程同步方式-自旋锁(spinlock)

    自旋锁简介 Nginx框架使用了三种消息传递方式:共享内存.套接字.信号. Nginx主要使用了三种同步方式:原子操作.信号量.文件锁. 基于原子操作,nginx实现了一个自旋锁.自旋锁是一种非睡眠锁 ...

  5. 左右HttpClient上传的方法来解决中国的乱码

    二手HttpClient人们都知道通过addTextBody方法来加入要上传的文本信息,可是,假设要上传中文的话.或还有中文名称的文件会出现乱码的问题,解决的方法事实上非常easy: 第一步:设置Mu ...

  6. Ubuntu亮度无法调节或调节无法保存的问题

    装了搜狗输入法之后,系统设置里面的很多软件都没有了.以前屏幕太亮在电源里面可以调节,现在不行了.没办法,只能找其他的办法了. 在网上查了很多资料,经自己的实验,找到了一个成功的方法. 首先进入 /sy ...

  7. U盘变小恢复工具——亲测完美可用

    大白菜U盘,装系统后,U盘损坏,格盘后8G只剩345M,用usbboot恢复到了2G容量.离8G还差很远.用U盘变小恢复工具后,完美恢复到原来大小.在此记录一下,以待下次遇到相似情况使用. 原文地址 ...

  8. OCMOCM

    14年,OCM考试费12000 15年,考试费19800 对于我来说,1,2年之后是否换工作还是个未知数 在本单位考这个貌似没什么用处,工资也不会突然就涨很多 跳槽的话,专门做数据库感觉压力还挺大 年 ...

  9. <转>科普CPU Cache line

    转载于http://coolshell.cn/articles/10249.html CPU cache一直是理解计算机体系架构的重要知识点,也是并发编程设计中的技术难点,而且相关参考资料如同过江之鲫 ...

  10. MySQL安装配置过程

    1.下载压缩包,解压: 2: 修改  my-default.ini 文件 将一下代码前# 去掉修改成自己的地址 # These are commonly set, remove the # and s ...