poj 3253:Fence Repair(堆排序应用)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23913 | Accepted: 7595 |
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
Hint
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
Source
#define maxn 20000+2
int l[maxn];
void heap_sort(int h,int x)
{
int lg,lr,t;
while((x<<) <= h){
lg = x<<;
lr = (x<<) + ;
if( lr<=h && l[lg] > l[lr])
lg = lr;
if( l[x] > l[lg] ){
t = l[x];
l[x] = l[lg];
l[lg] = t;
x = lg;
}
else
break;
}
}
代码:
#include <stdio.h>
#define maxn 20000+2
int l[maxn];
void heap_sort(int h,int x)
{
int lg,lr,t;
while((x<<) <= h){
lg = x<<;
lr = (x<<) + ;
if( lr<=h && l[lg] > l[lr])
lg = lr;
if( l[x] > l[lg] ){
t = l[x];
l[x] = l[lg];
l[lg] = t;
x = lg;
}
else
break;
}
}
int main()
{
int i,n;
while(scanf("%d",&n)!=EOF){
int Min;
long long ans = ;
for(i=;i<=n;i++) //输入
scanf("%d",&l[i]);
//建堆
for(i=n/;i>;i--)
heap_sort(n,i);
//work
while(n>){
heap_sort(n,);
Min = l[];
l[] = l[n--];
heap_sort(n,);
Min += l[];
l[] = Min;
ans+=Min;
}
printf("%I64d\n",ans);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
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 (贪心)
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: 77001 Accepted: 25185 De ...
- poj 3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42979 Accepted: 13999 De ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
- POJ 3253 Fence Repair (哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19660 Accepted: 6236 Des ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
随机推荐
- 初识A*算法
写这篇文章的初衷是应一个网友的要求,当然我也发现现在有关人工智能的中文站点实在太少,我在这里抛砖引玉,希望大家都来热心的参与. 还是说正题,我先拿A*算法开刀,是因为A*在游戏中有它很典型的用法,是人 ...
- thinkphp空控制器的处理
<?php namespace Admin\Controller; use Think\Controller; class DengLuController extends Controller ...
- 锋利的jQuery-6--序列化函数serialize()和serializeArray()在表单提交中的作用
在通过jQuery ajax提交表单的时候,通常用下边的方法获取表单内容. var form = 'add-account-form'; //表单id $('#' + form).submit(fun ...
- 自动切换的JS菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" > <html xmlns=&quo ...
- C++中析构函数的作用,
如果构造函数打开了一个文件,最后不需要使用时文件就要被关闭.析构函数允许类自动完成类似清理工作,不必调用其他成员函数.析构函数也是特殊的类成员函数.简单来说,析构函数与构造函数的作用正好相反,它用来完 ...
- 是智能手机推动windows xp系统停止服务吗
昨天是windows xp系统停止服务的大限,各大媒体争相报道,漫天铺地的xp消息充斥网络,xp这个词的百度指数这段时间从4月1日的8411也开始猛涨,特别是这两天4月7日的36470飙升到4月8日的 ...
- 虚拟机里面安装Openfiler 2.99
简介 Openfiler 由rPath Linux驱动,它是一个基于浏览器的免费网络存储管理实用程序,可以在单一框架中提供基于文件的网络连接存储 (NAS) 和基于块的存储区域网 (SAN).Open ...
- Boltzmann机
博客园不能上传附件,所以这里贴两张流程图吧.一个是模拟退火算法的流程图(Boltzmann机本实上就是反复退火的过程), 个是Boltzmann调整权值的过程.
- ZeroMQ(java)之负载均衡
我们在实际的应用中最常遇到的场景如下: A向B发送请求,B向A返回结果.... 但是这种场景就会很容易变成这个样子: 很多A向B发送请求,所以B要不断的处理这些请求,所以就会很容易想到对B进行扩展,由 ...
- C++ Set
set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值:另外,还 ...