题目链接: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. Luogu P1195/P1892 口袋的天空/BOI团伙 【最小生成树/并查集】By cellur925

    其实这俩题挺水的,团伙拿下了一血,但是感觉还是写一下博客比较好x. 一.团伙 题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是 ...

  2. 乐搏讲自动化测试-Python适用公司类型(6)

    相信小伙伴们都知道,随着软件测试行业的发展和进步自动化测试已经成为必然.在竞争日益激烈的市场环境中也是你升职加薪的利器. 所以,小编决定从今天起!将要系统.连续.高质量的持续更新「整套自动化测试」文章 ...

  3. POJ 2234 Nim博弈

    思路: nim博弈裸题 xor一下 //By SiriusRen #include <cstdio> using namespace std; int n,tmp,xx; int main ...

  4. _bzoj1051 [HAOI2006]受欢迎的牛【强联通】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1051 保存tarjan模版. 求强联通分量,缩点. #include <cstdio& ...

  5. DFS Codeforces Round #299 (Div. 2) B. Tavas and SaDDas

    题目传送门 /* DFS:按照长度来DFS,最后排序 */ #include <cstdio> #include <algorithm> #include <cstrin ...

  6. ORA-14074: partition bound must collate higher than that of the last partition

    There is a error happen in crotab: CREATE parttion report ORA-14074:ORA-14074: partition bound must ...

  7. 174 Dungeon Game 地下城游戏

    一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格布局.我们英勇的骑士(K)最初被安置在左上角的房间里,并且必须通过地下城对抗来拯救公主.骑士具有以正整数 ...

  8. 解决asp.net 以及MVC中上传文件大小限制的问题

    ﹤system.web﹥ ﹤httpruntime requestlengthdiskthreshold="256" maxrequestlength="2097151& ...

  9. 工作记录:JS正则表达式 angularjs ng-if ng-show ng-switch

    用了一下JS 正则表达式判断密码,很简单 学习了angularjs的ng-if ng-show ng-switch的区别并使用 https://www.cnblogs.com/54td/p/59743 ...

  10. dubbo系列--集群容错

    作为一个程序员,咱们在开发的时候不仅仅是完成某个功能,更要考虑其异常情况程序如何设计,比如说:dubbo的消费端调用服务方异常的情况,要不要处理?如何处理? dubbo提供了多种集群容错机制,默认是f ...