c案例05贪心算法c案例0贪心算法5贪心算法.ppt

c案例05贪心算法c案例0贪心算法5贪心算法.ppt

  1. 1、本文档共34页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
c案例05贪心算法c案例0贪心算法c案例05贪心算法c案例05贪心算法

第五讲 贪心算法 (Greedy Algorithm) 导引问题:FatMouse Trade FatMouse Trade 1009 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain. FatMouse Trade 1009 Input The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1s. All integers are not greater than 1000. Output For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain. FatMouse Trade 1009 Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 ? Sample Output 13.333 31.500 FatMouse Trade 1009 题目分析:用贪心算法求的背包问题! 只要老鼠每次先去换单位猫粮内最多的咖啡豆,最终获得的就是最大的咖啡豆。 ? 算法设计: 1. 计算 咖啡豆/猫粮,从大到小排序。 ??2. 每次去换 咖啡豆/猫粮 最大的咖啡豆,直到猫粮用完。 所谓“贪心算法”是指: 在对问题求解时,总是作出在当前看来是最好的选择。也就是说,不从整体上加以考虑,它所作出的仅仅是在某种意义上的局部最优解(是否是全局最优,需要证明)。 特别说明: 若要用贪心算法求解某问题的整体最优解,必须首先证明贪心思想在该问题的应用结果就是最优解!! 用事实说话—— 算法分析: 不妨用Begin[i]和End[i]表示事件i的开始时刻和结束时刻。则原题的要求就是找一个最长的序列a1a2…an,满足: Begin[a1]End[a1]=…= Begin[an]End[an] 解题思路 先按结束时间进行排序,然后寻找合适的事件就可以了 今年暑假不AC 2037 今年暑假不AC 2037 今年暑假不AC 2037 算法分析: 如果N=M,那么显然用M条长度为1的线段可以覆盖住所有的区间,所求的线段总长为M。 如果N=1,那么显然所需线段总长为:… 如果N=2,相当于N=1的情况下从某处断开(从哪儿断开呢?)。 如果N=k呢? Moving Tables 1050 Moving Tables 1050 Moving Tables 1050 Moving Tables 1050 Moving Tables 1050 Sample Input 3 4 10 20 30 40 50 60 70 80 2 1 3 2 200 3 10 100 20 80 30 50 算法分析: 1、如果没有交叉,总时

文档评论(0)

ganqludp + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档