POJ 3253 Fence Repair(哈夫曼编码)】的更多相关文章

题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个要求的小木板的长度,及小木板的个数n,求最小费用 以 3 5 8 5为例: 先从无限长的木板上锯下长度为 21 的木板,花费 21 再从长度为21的木板上锯下长度为5的木板,花费5 再从长度为16的木板上锯下长度为8的木板,花费8 总花费 = 21+5+8 =34 解题思路:哈夫曼编码模板 代码:…
Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He then purchases a sin…
题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<stack> #include<queue> #include<iomanip> #include<cmath> #include<…
http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<iostream> #include<queue> using namespace std; #define LL long long ; int n…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5的木板,花费21,再把16割两个8,花费16,总计37,现在就是问你花费最少的情况. 思路:转化为哈弗曼树 解法一:AC #include<cstdio> #include<algorithm> #define maxn 20010 using namespace std; int n…
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, eac…
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) u…
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He the…
这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少.显然的是一共会拼接n-1次,如果把每根木棍最初的长度想成叶子节点,两个节点的父节点就是这两个节点的长度相加.那么求的就是这n-1个父节点的总和最少. 为实现这个目标,我们使每个父节点的值尽可能的少,由此想到哈夫曼编码的原理.初始条件为一共n根木棍.然后从中选出最短的两根,将这两根拼接成一根,这样得到的父节点的值是最小的.然…