POJ 3253-Fence Repair(堆)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27055 | Accepted: 8800 |
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
3
8
5
8
Sample Output
34
哈曼夫树。
题意 :把一根长度为x的木板切成n段。每段的长度已给出来。切某块木板时。花费的费用为该木板的长度。一開始以为仅仅要每次选出最长的木板然后从总的中切掉就是最小花费。实际上不是,其有用脑子想想也知道了,假设总长非常长的话,而须要的木板的长度又非常小,每次从总长中切掉花费将会非常大,所以这样的思路是不正确的,看了讨论区才明确,我们能够把切这个过程逆回去。就是把这n段木板连接起来,每次花费的钱为待连接的两段木板的长度。(细致想想,切跟连花费的钱是同样的,即是等价的,画一下图能够看出来) 然后有一种思想是:初始化一个最小堆,每次从堆中选出最小的两段木板进行连接。连接完之后在放入到堆中。总共n-1次连接就可以
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
ll a[20010];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%lld",a+i);
make_heap(a,a+n,greater<int>());
int t=n-1;ll ans=0,tem;
while(t--)
{
tem=a[0];pop_heap(a,a+n,greater<int>());--n;
tem+=a[0];pop_heap(a,a+n,greater<int>());--n;
ans+=tem;a[n++]=tem;
push_heap(a,a+n,greater<int>());
}
printf("%lld\n",ans);
}
return 0;
}
POJ 3253-Fence Repair(堆)的更多相关文章
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- 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 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28359 Accepted: 9213 Des ...
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
- poj 3253 Fence Repair(priority_queue)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40465 Accepted: 13229 De ...
- POJ 3253 Fence Repair 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
随机推荐
- jQuery+ajax中,让window.open不被拦截(转)
方法1:<input type="button" class="preview" value="预览"/>$('.preview ...
- 一、springMVC、freemarker页面半自动静态化
说明:刚刚接到公司的通知,实现(半自动化),即通过参数控制是否需要静态化页面(哪里我说错了,勿喷!谢谢) 1,请求.do的URL时直接生成对应的.htm文件,并将请求转发到该htm文件 2,自由控制某 ...
- Js操作Select大全(取值、设置选中等等)
jquery操作select(取值,设置选中) 每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selecto ...
- inux --- 服务器性能监控
几个问题 先来看看下面几个问题: CPU.内存以及硬盘的关系是怎样的? 进程和线程是什么?有什么区别?有什么优缺点? 什么是物理内存?什么是虚拟内存?什么时候要用到虚拟内存? 什么是CPU中断?CPU ...
- Linux内核(12) - 子系统的初始化之那些入口函数
内核选项的解析完成之后,各个子系统的初始化即进入第二部分—入口函数的调用.通常USB.PCI这样的子系统都会有一个名为subsys_initcall的入口,如果你选择它们作为研究内核的切入点,那么就请 ...
- BPMN2.0 规范
1 启动事件 每个流程总是以启动事件作为入口,启动事件在BPMN2.0 中以细线圆圈表示.分为三种类型 空启动事件 定时启动事件 异常启动事件 消息启动事件 启动事件都是等待第三方触发才可以启动. 定 ...
- javascript常用字符串函数和本地存储
concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a.conca ...
- MySQL Study之--MySQL下图形工具的使用(MySQL Administrator)
MySQL Study之--MySQL下图形工具的使用(MySQL Administrator) 系统环境: 操作系统: Windows 7(64) 下载地址:http://www.soft ...
- Ubuntu和centos下查看包的安装路径
安装包后,如何查看安装的具体路径? Ubuntu下: dpkg -L <包名> CentOS下: rpm -ql <包名> 助记: l为list的首字母. q为query ...
- [na]交换技术知识点-提纲
vlan - trunk - vtp(vtp prune) stp portfast rootgurd bpduguard bpdufilter uplinkfast backbonfast loop ...