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.

For example, 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).

Now, please design an algorithm to help FJ with minimum cost.

题目大概是讲有一个农夫要把一个木板锯成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木板的长度,给定小木板的个数n,及各个要求的小木板的长度,求最小费用。

例如长度为21 的木板要切成长度为5、8、8的三块木板。长21的木板切成长为13和8的板时,开销为21,再将长度为13的板切成长度为5和8的板时,开销是13,于是合计开销为34。

利用Huffman思想,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;int main()
{int *a;int i,j,n,sum;__int64 ans;   while(1){printf("请输入所需木板的数量:");scanf("%d",&n);a=new int[n];   //每块木板的长度for(i=0;i<n;++i){printf("\n请输入第%d块木板的长度:",i+1);scanf("%d",&a[i]);}sort(a,a+n);ans=0;for(i=0;i<n-1;++i){sum=a[i]+a[i+1];ans+=sum;for(j=i+2;j<n;++j){if(sum>a[j])a[j-1]=a[j];else{a[j-1]=sum;break;}}if(j==n)   //说明sum比所有元素都大,把sum直接放在最后a[j-1]=sum;}printf("\n总费用为:%I64d\n\n",ans);   }return 0;
}

数据结构与算法 Farmer John 问题 农夫锯木板问题相关推荐

  1. 数据结构与算法经典程序:农夫过河问题讲解

    首先,在这里祝各位看到这条博客的同学五一快乐呀,今天,我们讲解一个数据结构与算法中的经典题目(小程序),题目如下: 题目: 一个农夫在河边带了一只狼.一只羊和一颗白菜,他需要把这三样东西用船带到河的对 ...

  2. 【数据结构与算法】狼、羊、菜和农夫过河:使用图的广度优先遍历实现

    [数据结构与算法]狼.羊.菜和农夫过河:使用图的广度优先遍历实现 Java 农夫需要把狼.羊.菜和自己运到河对岸去,只有农夫能够划船,而且船比较小.除农夫之外每次只能运一种东西.还有一个棘手问题,就是 ...

  3. 数据结构与算法 10.30

    数据结构与算法 10.30 Lake Counting Description Due to recent rains, water has pooled in various places in F ...

  4. 算法java语言描述_java语言描述数据结构与算法崔笑颜的博客

    java语言描述数据结构与算法崔笑颜的博客 冒泡排序 插入排序 选择排序 希尔排序 快速排序 归并排序 二分查找package com.demo.test; import java.util.Arra ...

  5. Python3-Cookbook总结 - 第一章:数据结构和算法

    第一章:数据结构和算法 Python 提供了大量的内置数据结构,包括列表,集合以及字典.大多数情况下使用这些数据结构是很简单的. 但是,我们也会经常碰到到诸如查询,排序和过滤等等这些普遍存在的问题. ...

  6. 年后跳槽BAT必看:10种数据结构、算法和编程课助你面试通关

    作者 | javinpaul 译者 | 大鱼 编辑 | 一一 出品 | AI 科技大本营 进入 BAT 这样的巨头企业工作,无疑是很多程序员的梦想.但事实上,能通过这些公司高难度编程面试的只是一小撮人 ...

  7. 如何求解问题--数据结构与算法入门

    如何求解问题–数据结构与算法入门 在学习数据结构与算法前,我们大多有这样的困惑,难道学习了数据结构与算法就能帮助我们解决学习Java.Python时的大作业吗?数据结构与算法是什么? 回答这个问题之前 ...

  8. 为什么我要放弃javaScript数据结构与算法(第二章)—— 数组

    第二章 数组 几乎所有的编程语言都原生支持数组类型,因为数组是最简单的内存数据结构.JavaScript里也有数组类型,虽然它的第一个版本并没有支持数组.本章将深入学习数组数据结构和它的能力. 为什么 ...

  9. 《数据结构与算法之美》学习汇总

    此篇文章是对自己学习这门课程的一个总结和课后的一些练习,做一个汇总,希望对大家有帮助.本人是半路程序员,2018年2月开始学习C++的,下面的代码基本都是C++11版本的,代码有错误的地方请不吝留言赐 ...

最新文章

  1. 62岁程序员植入逻辑炸弹, 面临10年监禁和25万美元罚款
  2. 利用FRIDA攻击Android应用程序(一)
  3. Oracle 10g数据库基础之基本查询语句-中-函数
  4. 00_python安装与配置(mac)
  5. Tomcat Caused by java lang OutOfMemoryError PermGen space
  6. linux编译minix,MINIX对Linux
  7. Windows Server 2008 R2无密码共享设置
  8. 【工具】支付宝免费卡校验接口调用及常用银行简称整理
  9. 《人工智能》之《计算智能》习题解析
  10. 腔体缝隙天线[搬运]
  11. 使用adb安装apk
  12. 电脑和ubuntu开发板用网线连接的方法
  13. 超简单方法搭建Eclipse下的Android NDK
  14. PDF文件格式转换工具 迅捷PDF转换成Word转换器
  15. 网易历届笔试面试题整理大全
  16. Python Level 4 程序题:布置任务
  17. 紫薇星上的Java——映射转换
  18. 巴西龟饲养日志----冬眠苏醒
  19. 前端xlsx插件简单说明
  20. 基于 Python 爬虫+简单数据分析的课程设计(附PPT)

热门文章

  1. 哈工大软件构造Lab1(2022)
  2. 【面试题分析】第九篇 顺丰科技安卓客户端面经【2轮技术+1轮HR面】(21届秋招)
  3. 压电水听器原理/水声传感器工作原理
  4. GPU(Graphics Processing Unit,图形处理器)
  5. 面试着装需要注意的点有哪些
  6. 抖音怎么养号,老抖音号怎么重新养号
  7. 基于MATLAB的汽车制动力分配曲线
  8. jQuery表单验证实例 / 包含用户名、密码、住址、邮箱验证
  9. 如果这就是爱情,本来就不公平
  10. 建设银行重磅发布5G无人银行