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 ...
随机推荐
- 提示用户一直输入数字(默认为正整数),当用户输入end的时候显示当前输入数字中的最大值。
string input = ""; ; while (input != "end") { Console.WriteLine("请输入一个正整数,输 ...
- Sql Server中启用分布式事务小结
1.web服务器与数据库服务器同时启动msdtc服务 2. 2台服务器做出如下配置: 控制面板->管理工具->组件服务->计算机->我的电脑->本地DTC .Net示例: ...
- 在xib里,拖一个UIView到UITableView中作为tableHeaderView
原贴地址:http://blog.csdn.net/haoxinqingb/article/details/41683881 内容 在xib里,拖一个UIView到UITableView中作为tabl ...
- 自定义 SqlHelp
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Data; usin ...
- System.gc()与Object.finalize()的区别
finalize()是由JVM自动调用的,你可以用System.gc(),但JVM不一定会立刻执行,JVM感觉内存空间有限时,才会开始执行finalize(),至于新的对象创建个数和被收集个数不同是因 ...
- linux下防火墙开启某个端口号及防火墙常用命令使用
linux防火墙常用命令 1.永久性生效,重启后不会复原 开启:chkconfigiptables on 关闭:chkconfigiptables off 2.即时生效,重启后复原 重启防火墙 方式一 ...
- object-c(oc)内存管理机制详解
1.内存的创建和释放 让我们以Object-c世界中最最简单的申请内存方式展开,谈谈关于一个对象的生命周期.首先创建一个对象: 1 2 3 //“ClassName”是任何你想写的类名,比如NSStr ...
- IMEI是什么? 怎样查手机串号IMEI
IMEI的基本含义 IMEI(International Mobile Equipment Identity,移动设备国际识别码,又称为国际移动设备标识)是手机的唯一识别号码.我们从这个缩写的全称中来 ...
- 反射中 GetCustomAttributes
public abstract object[] GetCustomAttributes(bool inherit); 这是GetCustomAttributes方法的一个重载,参数为bool类型返回 ...
- bat programming is easy and powerful
用linux的角度来思考windows,习惯了linux的shell后, 再来看windows的bat编程,就简单多了,简直就是理所当然 实际上windows的cmd命令行和linux的shell命令 ...