尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

poj 3253(Fence Repair)

poj 3253(Fence Repair)
📅 发布时间:2026/7/28 15:00:40

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); }

相关新闻

  • vLLM与SGLang:大模型推理框架的技术对比与应用指南
  • Linux文件搜索终极指南:FSearch如何让你告别find命令的等待时间
  • 数据框的合并排序、描述统计、分箱

最新新闻

  • 扎根昆仑绿洲打造高端米——羊脂白露米品牌发展纪实 - 品牌推荐君
  • 终极SVG编辑神器:SVGEdit免费高效导出PDF、PNG和高质量SVG的完整指南
  • AI智能体正在集体“上岗“:当Agent学会替你干活,人机交互的最后一层界面为什么还是“哑“的?
  • VisualCppRedist AIO:Windows运行库管理的终极解决方案
  • OpenClaw智能代理框架:金融分析、自动化与本地部署实战
  • 焊接符号知识问答

日新闻

  • 力旷智能:伺服驱动系统在制药收瓶设备中的应用解析
  • 2026 网安入门避坑指南,零基础如何避开无效学习直接上手实战
  • 揭秘CFC项目:如何通过手机摄像头实现850kbps无网络文件传输

周新闻

  • 大连理工大学与东京大学联手打造的“主动型AI助手“
  • 170.2026年国家级科研瓶颈:超精密单点金刚石切削(SPDT)光学表面生成
  • SongBloom:革命性歌曲生成框架深度解析——如何通过交织自回归与扩散模型创作完整音乐

月新闻

  • 2026年6月公司网站搭建最新热门渠道测评:四大低成本/零代码平台对比+避坑
  • 【Linux】Linux arm 编译QT程序,出现expected “}“报错
  • 【MATLAB例程】四基站二维AOA定位与距离辅助增强对比仿真。基于角度观测和测距修正的固定目标平面定位精度分析

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号