poj 3253 Fence Repair
来源:互联网 发布:连云港seo公司 编辑:程序博客网 时间:2024/06/04 17:40
题目大意:
FJ需要修补牧场的围栏,他需要 N 块长度为 Li 的木头(N planks of woods)。开始时,FJ只有一块无限长的木板,因此他需要把无限长的木板锯成 N 块长度
FJ需要修补牧场的围栏,他需要 N 块长度为 Li 的木头(N planks of woods)。开始时,FJ只有一块无限长的木板,因此他需要把无限长的木板锯成 N 块长度
为 Li 的木板,Farmer Don提供FJ锯子,但必须要收费的,收费的标准是对应每次据出木块的长度,比如说测试数据中 5 8 8,一开始,FJ需要在无限长的木板上锯下长度 21 的木板(5+8+8=21),第二次锯下长度为 5 的木板,第三次锯下长度为 8 的木板,至此就可以将长度分别为 5 8 8 的木板找出
题目可以转化为Huffman树构造问题 :
给定 N planks of woods,
1. 在 N planks 中每次找出两块长度最短的木板,然后把它们合并,加入到集合A中,
2. 在集合中找出两块长度最短的木板,合并加入到集合A中,重复过程,直到集合A中只剩下一个元素
显然,通过每次选取两块长度最短的木板,合并,最终必定可以合并出长度为 Sum(Li)的木板,并且可以保证总的耗费最少
#include<stdio.h>#include<string.h>#include<stdlib.h>int a[20010];int cmp(const void *a,const void *b){ return *(int *)a-*(int *)b;}int main(){ int n,i,m,t,j;long long s;//数据有可能超出int型 while(scanf("%d",&n)!=EOF) { memset(a,0,sizeof(a)); for(i=0;i<n;i++) scanf("%d",&a[i]); qsort(a,n,sizeof(a[0]),cmp); for(s=0,i=1;i<n;i++) { a[i]+=a[i-1]; s+=a[i]; for(j=i;j<n-1;j++) if(a[j]>a[j+1]){ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }//把剩下的元素重新排序 } printf("%lld\n",s); } return 0 ; }
0 0
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- POJ 3253Fence Repair
- POJ--3253 -- Fence Repair
- poj-3253-Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- poj 3253 Fence Repair
- POJ - 3253 Fence Repair
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- poj 3253---Fence Repair
- DBMS_UTILITY.get_time——比较时间差
- 最常用的设计模式---装饰者模式(C++实现)
- 南阳 86 找球号(一)
- winform 中 treeview 的根节点与子节点的同步问题
- 主题:如何彻底删除流氓软件“快盒应用”
- poj 3253 Fence Repair
- 我的播放器——使用Vitamio开发iOS平台上的万能播放器(1)
- 怎样批量删除.svn文件
- 02-Linux-GNOME图形界面基本操作
- Spark源码解析——Storage模块
- hdu 1050 Moving Tables
- 二十进制的加法
- 非IMU模式下DML语句产生的REDO日志内容格式解读
- Qt 做的全屏程序 配砟整形车 壁障系统