POJ3253 Frence repair
来源:互联网 发布:战斗妖精雪风模型淘宝 编辑:程序博客网 时间:2024/06/03 04:59
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
解题思路:最小堆:
AC情况:
AC的代码:
# include <stdio.h># define N 20000void insert(int Tree[],int X)//向Tree堆里插入X{ int par,i=++Tree[0]; //插入X 后 Tree[0]+1 while(i>1) //直到i不是根节点 { par=i/2; //父节点为par if(Tree[par]<=X) break;//如果父节点满足最大堆的特性 则插入当前的位置即可 Tree[i]=Tree[par]; //否则调整堆 即位置上移 i=par; } Tree[i]=X;//插入新节点}int DelMin(int Tree[])//删除最大值{ int i=1,root=Tree[1],R,L,X=Tree[Tree[0]--];//X记录当前末尾节点 root为根 即为最大值 if(Tree[0]<0) return -1; while(i*2<=Tree[0]) { L=i*2; R=L+1;//Left Right 记录左右节点 if(R<=Tree[0]&&Tree[R]<Tree[L])L=R; if(Tree[L]>=X) break;//如果所有的位置已经调整好 跳出循环 Tree[i]=Tree[L];//否则继续调整堆 i=L; } Tree[i]=X; return root;}int L[N+1],i,n,A;//L数组表示堆long long sum;int main(){ scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&A); insert(L,A); } while(L[0]>1) { A=DelMin(L)+DelMin(L); sum+=A; insert(L,A); } printf("%lld\n",sum); return 0;}
代码2:
# include <stdio.h># define N 20000void insert(int Tree[],int X)//向Tree堆里插入X{ int par,i=++Tree[0]; //插入X 后 Tree[0]+1 while(i>1) //直到i不是根节点 { par=i>>1; //父节点为par if(Tree[par]<=X) break;//如果父节点满足最大堆的特性 则插入当前的位置即可 Tree[i]=Tree[par]; //否则调整堆 即位置上移 i=par; } Tree[i]=X;//插入新节点}int DelMin(int Tree[])//删除最大值{ int i=1,root=Tree[1],R,L,X=Tree[Tree[0]];//X记录当前末尾节点 root为根 即为最大值 if(Tree[0]<0) return -1; while(i<<1<Tree[0]){ L=i<<1; R=L+1;//Left Right 记录左右节点 if(R<Tree[0]&&Tree[R]<Tree[L])L=R; if(Tree[L]>=X) break;//如果所有的位置已经调整好 跳出循环 Tree[i]=Tree[L];//否则继续调整堆 i=L; } Tree[i]=X; Tree[0]--; return root;}int L[N+1],n,A;//L数组表示堆long long sum;int main(){ scanf("%d",&n); while(n--){ scanf("%d",&A); insert(L,A); } while(L[0]>1) { A=DelMin(L)+DelMin(L); sum+=A; insert(L,A); } printf("%lld\n",sum); return 0;}
- POJ3253 Frence repair
- Frence Repair POJ3253 POJ2431 堆排序
- POJ3253 Fence Repair
- poj3253 Fence Repair( 贪心 )
- poj3253 Fence Repair
- poj3253 Fence Repair
- poj3253 Fence Repair---赫夫曼树
- poj3253 Fence Repair
- POJ3253 Fence Repair
- POJ3253 Fence Repair
- POJ3253--Fence Repair
- POJ3253 Fence Repair(贪心)
- POJ3253 Fence Repair
- poj3253 Fence Repair
- Poj3253—Fence Repair
- POJ3253-Fence Repair
- POJ3253-Fence Repair
- [POJ3253]-Fence Repair
- ACM总结报告
- 近期关于CI/CD策略以及git分支模型的思考
- linux下使用源文件安装jdk1.8
- 码制知识
- 什么是node.js?node.js的优缺点说明
- POJ3253 Frence repair
- STM32串口IAP(YModem)
- 50个必备的实用jQuery代码段+ 可以直接拿来用的15个jQuery代码片段
- 使用 Eigen 库写第一个程序
- linux系统中jdk环境安装配置方法
- 设计模式之单例模式(一)
- 上传单个或多个文件
- dllmain测试与相关实验
- centos 7 安装gitlab