当前位置: 首页 > news >正文

poj 3253(Fence Repair)

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN(1 ≤N≤ 20,000) planks of wood, each having some integer lengthLi(1 ≤Li≤ 50,000) units. He then purchases a single long board just long enough to saw into theNplanks (i.e., whose length is the sum of the lengthsLi). 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 theN-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 theNplanks. 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

Line 1: One integerN, the number of planks
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to makeN-1 cuts

Sample Input

3 8 5 8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
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).

思路:类似霍夫曼编码,将最小的板子和次最小的板子相加,(L1+L2),L3,L4.....Ln,再对这n-1个值进行一次最小值和次最小值的相加,直到只剩下1个板子数为止。

#include<stdio.h> #include<stdlib.h> #include<algorithm> #include<string.h> using namespace std; int n; long long ans=0,L[20001]; int main() { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%lld",&L[i]); while(n>1) { int mii1=0,mii2=1; if(L[mii1]>L[mii2]) swap(mii1,mii2); for(int i=2;i<n;i++)//寻找最小值和次最小值 { if(L[i]<L[mii1]) { mii2=mii1; mii1=i; } else if(L[i]<L[mii2]) mii2=i; } long long t=L[mii1]+L[mii2]; ans+=t; if(mii1==n-1) swap(mii1,mii2);//因为下一句的赋值原因,防止L[mii1]=L[mii2]=L[n-1] L[mii1]=t; L[mii2]=L[n-1]; n--; } printf("%lld\n",ans); }
http://www.jsqmd.com/news/1281238/

相关文章:

  • 中山废旧变压器回收避坑问答:资质怎么看铜铝怎么区分价格差多少 - 广东再生资源回收
  • 抖音下载器完全指南:三步保存无水印高清视频,支持批量下载与直播录制
  • 射频系统中噪声系数的概念、测量与应用
  • 合肥除甲醛机构深度测评:绿舒等7大品牌横向对比 - 绿舒环保母婴除甲醛
  • 上交大开源《动手学大模型》实战教程,真的把我当小孩教啊!
  • 读书笔记-哈佛大学极简经济学1.
  • 2026娄底黄金回收哪里靠谱?五区县口碑商家真实测评,上门也能放心卖 - 城刊速递
  • 生成式 UI 的落地避坑:过度生成、样式失控与交互不一致的应对策略
  • AAC Smart (世成家居) 解读:河津全屋智能家居设备行业现状、用户痛点与落地干货 - 百航
  • 如何轻松掌控华硕笔记本性能:G-Helper轻量级控制工具终极指南
  • 抖音下载器完全指南:三步掌握无水印视频批量下载技巧
  • 2026安徽专升本备考首选!库课专升本深耕本土,历年公办录取率稳居行业前列 - 最新资讯
  • Linux文件搜索终极指南:FSearch如何让你告别find命令的等待时间
  • vLLM与SGLang:大模型推理框架的技术对比与应用指南
  • TI TPIC7710 EVM评估模块:加速汽车电子EPB系统开发的硬件调试平台
  • 构网型逆变器状态空间建模与MATLAB稳定性分析实践
  • 武汉襄武学校-正规高考复读中心 - 湖北成人升学提升
  • 数据框的合并排序、描述统计、分箱
  • Adobe-GenP 3.0:终极Adobe全家桶免费激活指南
  • 2026绍兴装修简装公司哪家靠谱?本地老师傅甄选攻略+避坑指南 - 装修新知
  • Grasscutter Tools终极指南:原神私服玩家的可视化管理解决方案
  • 大模型应用的基础知识:Transformer架构、Attention机制、位置编码
  • 如何快速掌握QModMaster:工业自动化调试的终极指南
  • 物联网设备硬件级安全芯片SE050应用解析
  • Arduino电子琴制作指南:从原理到实践,打造你的第一台DIY乐器
  • 黄山买金水太深?手把手教你用“通货价”拿货,这3家店老板看了都直呼内行! - 黄金珠宝
  • c++ array学习
  • 国内铝木门窗品牌榜单分析 - 品牌推广大师
  • CSS 架构的反模式清单:全局污染、选择器爆炸与 z-index 战争
  • 暗黑破坏神2存档编辑器终极指南:5分钟掌握角色装备修改技巧