哈夫曼树结合堆排序 POJ(3253)
来源:互联网 发布:nc.exe windows下载 编辑:程序博客网 时间:2024/06/08 14:48
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) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.
Input
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3858
Sample Output
34
很自然的想到的哈夫曼树(贪心策略).
但是怎么样排序有效率呢? 每次只是取出两个最小的,然后再相加,再放进去,再排序。
经过测试,普通的排序会超时。
下面代码运行正确,但是超时. 主要是sort 函数太费时了,因为每次排序只是插入一个数而已。
#include <iostream>#include <algorithm>using namespace std;typedef long long int64;int len;int64 arr[20001];int main() {while (cin >> len) {for (int i = 0; i < len; i++) {cin >> arr[i];}int tmplen = len;int sum = 0;sort(arr + (len - tmplen), arr + len);while (tmplen > 1) {sort(arr + (len - tmplen), arr + len);arr[len - tmplen + 1] = arr[len - tmplen] + arr[len - tmplen + 1];sum += arr[len - tmplen + 1];tmplen--;}cout << sum << endl;}return 0;}
由于只是插入一个数。下面自己写插入排序:
#include <iostream>#include <algorithm>using namespace std;typedef long long int64;int len;int64 arr[20001];int main() {while (cin >> len) {for (int i = 0; i < len; i++) {cin >> arr[i];}int index = 1;int64 sum = 0,temp;sort(arr, arr + len);while (index < len-1) {temp = arr[index-1] + arr[index];int i;for(i=index + 1; i<len; i++){if(temp < arr[i]){ //插入完成退出循环arr[i-1] = temp;break;}elsearr[i-1] = arr[i];}if(i==len) //如果遍历到了最后,也没插入arr[i-1] = temp;index++;sum += temp;}sum += arr[len-1] + arr[len-2]; //把剩余的两个数加上cout << sum << endl;}return 0;}
运行时间 550ms
算是勉强通过。
下面写更高效的堆排序,小顶推。
#include <iostream>using namespace std;typedef long long int64;int len;int64 arr[20001]; //arr[0] 存数当前数组的长度//调整堆void heap(int i) {int left = i * 2;int right = i * 2 + 1;int mins = i;if (left <= arr[0] && arr[left] < arr[mins])mins = left;if (right <= arr[0] && arr[right] < arr[mins])mins = right;if (i != mins) {swap(arr[i], arr[mins]);heap(mins);}}//想堆中插入一个数void inheap(int64 key) {arr[++arr[0]] = key;int64 i = arr[0];//i就是当前插入的最后位置. 循环 lgn 次即可while (i > 1 && arr[i] < arr[i / 2]) {swap(arr[i], arr[i / 2]);i = i / 2;}}//返回最小的两个数的和int64 get() {int64 p = arr[1], q;arr[1] = arr[arr[0]]; //第一个位置 和 最后一个位置交换arr[0]--; //数组长度减1heap(1); //调整位置1q = arr[1];arr[1] = arr[arr[0]];arr[0]--;heap(1);inheap(p + q); //返回最小的两个数的和return p + q;}int main() {while (cin >> len) {arr[0] = 0;for (int i = 1; i <= len; i++) {cin >> arr[i];inheap(arr[i]);}int64 ans = 0;for (int i = 1; i < len; i++) {ans += get();}cout << ans << endl;}return 0;}
运行时间70ms
- 哈夫曼树结合堆排序 POJ(3253)
- POJ 3253 Fence Repair (堆排序)
- poj-2833 堆排序
- poj 2388 堆排序
- poj 2833 堆排序
- POJ 2442 Sequence 堆和堆排序
- 堆排序练习:POJ 2388
- POJ 3253 Fence Repair (哈夫曼树,堆)
- POJ 3253 Fence Repair 堆 哈夫曼树
- poj 3253 最小堆 优先队列 哈夫曼树
- poj 2833 The Average 堆排序
- POJ 2388 (堆排序求中位数)
- POJ 2833 The Average 堆排序
- poj 3253 (二叉堆)
- poj 3253 优先队列 堆
- POJ 2388 Who's in the Middle(堆排序)
- Who's in the Middle 堆排序,poj 2388
- POJ 2051 Argus 堆排序 贴个模板
- 中语言超酷特性 之 --- 中语言标准库
- 数学建模的初步认识
- android 类似于微博拍照上传,压缩图片,避免oom
- cocos2d-x项目生成.Apk并装机后gdb调试
- Python 代码性能优化技巧
- 哈夫曼树结合堆排序 POJ(3253)
- jquery validate(1) : 将校验规则写到控件中
- WPF编译器错误 CS1566 g.resources出错的终极解决
- 保护模式下 8259A芯片编程及中断处理探究(上)【1】
- C++ VC socket 文件下载 简单
- Features of C99
- 读书笔记:自定义事件和jQuery插件
- hdu 1301 Jungle Roads
- 全屏游戏切换至窗口化