﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Onway-文章分类-伤不起的ACM</title><link>http://www.cppblog.com/Onway/category/14041.html</link><description>我是一只菜菜菜菜鸟...</description><language>zh-cn</language><lastBuildDate>Sun, 27 Nov 2011 03:12:39 GMT</lastBuildDate><pubDate>Sun, 27 Nov 2011 03:12:39 GMT</pubDate><ttl>60</ttl><item><title>pku3038 贪心加优先队列</title><link>http://www.cppblog.com/Onway/articles/142749.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Sat, 26 Mar 2011 03:24:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/142749.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/142749.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/142749.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/142749.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/142749.html</trackback:ping><description><![CDATA[<span style="font-size: 18pt;">题意：一辆公车从第1个站开到第n个站，再开回第1个站。车上人数容量为c。有K组数，每组数给出num个人要从s站坐到e站。问公车最多能让几个人到达目的站。<br />思路：用一个数组down[]记录每个站要下车的人数，用一个优先队列记录车上的人的终点站（用作踢人）。枚举从第1个站到最后一个站，先进行下车过程，再进行上车，注意上车时候可能要踢人。<br />贪心原则：<br />1）车厢容量要得到最好的利用。<br />2）优先选择路程短的乘客。<br /><br />解题步骤：<br />1）输入的时候，将公车线路转换为一条直线。输入注意起点站大于终点站的，为了满足第二条原则，都让他们在回程的时候上车。<br />2）为了满足第一条原则，当然要让起点站最先的人先上。要排序。<br />3）每个站都停一次，判断有没有人要上车和下车。先进行下车过程。上车过程有很多分支判断，要注意考虑周全。<br /><br />后记：<br />在练习赛里见到这个题目，有点象校现场赛的一题，但多了一个容量限制。所以当时也没多想，放弃了。<br />后来讨论后发现这个题目感觉自己有能力做。感觉要用到优先队列，所以一直拖到二叉堆的模板完成，期间最有成就感的是使用了回调函数。<br />写的过程一直很相信自己的思路，只使用了一个优先队列来进行上车和下车两个过程。但由于太多自信，修修补补两百行多的代码终于经过五个小时的调试由WA变成了TLE。期间最大的收获是发现对题意的理解出了一点偏差。<br />第二天重写了一个70行的代码，还是WA，又经过一个多小时的调试，发现了一个很弱智的错误。尝试用getback()从最小堆里取得最大元素。<br />这时马上否定了自信加坚持了多个小时的思路，只用一个队列进行上下车两个过程。在STL里随便找了一会，也没发现有满足要求的数据结构。<br />然后尝试用两个优先队列。由于思维被第一次的代码疆住了，感觉两个队列之间很难进行同步。<br />然后开始考虑洪爷说的枚举每一个站。然后莫名就想到了以上的思路，只有一个优先队列记录被踢的。然后又漏了一个变量的维护。<br /><br />在接近十个小时的调试中，vim最多的时候开了四个窗口，但不会调大小。gdb的调试窗口和条件断点又忘了。<br />STL的map和set，还有priority_queue都不会用。<br />传说中的线段树又是什么呢？<br /><br /><br /></span><img src ="http://www.cppblog.com/Onway/aggbug/142749.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2011-03-26 11:24 <a href="http://www.cppblog.com/Onway/articles/142749.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 2586 贪心</title><link>http://www.cppblog.com/Onway/articles/139159.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Sun, 23 Jan 2011 05:34:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/139159.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/139159.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/139159.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/139159.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/139159.html</trackback:ping><description><![CDATA[<div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><span style="color: #000000; "><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008000; ">/*</span><span style="color: #008000; "><br>题意：还是直接贴上discuss里别人给出的吧：<br>&nbsp;&nbsp;&nbsp;&nbsp;对于每一个月来说，是盈利如果则盈利S，如果亏空则亏d。<br>&nbsp;&nbsp;&nbsp;&nbsp;每五个月进行一次统计，共统计八次（1-5月一次，2-6月一次<img src="http://www.cppblog.com/Images/dot.gif"><img src="http://www.cppblog.com/Images/dot.gif">.）<br>&nbsp;&nbsp;&nbsp;&nbsp;统计的结果是这八次都是亏空。<br>&nbsp;&nbsp;&nbsp;&nbsp;问题：判断全年是否能盈利，如果能则求出最大的盈利。<br>&nbsp;&nbsp;&nbsp;&nbsp;如果不能盈利则输出Deficit<br>类型：贪心<br>思路：为了使得盈利最大，一次统计的五个月里，应该尽量使得s出现最多，而为了保证每一次统计都是亏空，显然一次统计的五个月里，s不能出现五次。如果s能出现四次的话，为了保证亏空，应该有4*s&lt;d，如果能出现3次，则应有3*s&lt;2d&#8230;&#8230;以此类推。当s&gt;=4d的时候，即只要有一个s出现，都不能满足统计亏空条件的时候，只能让其出现五个d了。在以上分析中，为了让d的出现尽可能的少，显然，一个d应该尽可能的跟大家（其他统计）&#8220;共享&#8221;,即对于第一次统计，s如果能出现，则让其出现在最前。以后各次统计，不到&#8220;万不得意&#8221;，不让d出现。<br><br>后记：看不懂英文题意，直觉discuss肯定有讲题意的，上去一题果然。而后觉得直接暴力能暴出来，写出来超时，从时间复杂度来看，我始终不懂为何超时，尽管我没有去剪枝。十二个月，每个月只能出现盈利或亏损，则有2^12=4096种情况，对于每一种情况，要判断是否能使8次统计亏空，进行8＊5次运算，从符合条件的情况中取出最大值并判断盈利或亏空。<br>然后，上discuss看了一下别人的提示，想到了上述思路，其实跟人家的已经是大同小异了。题意是看别人的，解题精华是看别人的，这个题目算是做得失败了。<br></span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br></span></div><br></span></div><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000; ">#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">cstring</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br>#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">iostream</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #0000FF; ">using</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">namespace</span><span style="color: #000000; ">&nbsp;std;<br></span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;seq[</span><span style="color: #000000; ">14</span><span style="color: #000000; ">];<br><br></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;s,d;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(cin</span><span style="color: #000000; ">&gt;&gt;</span><span style="color: #000000; ">s</span><span style="color: #000000; ">&gt;&gt;</span><span style="color: #000000; ">d)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;sum</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(4s</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">d)&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="color: #000000; ">=</span><span style="color: #000000; ">10</span><span style="color: #000000; ">*</span><span style="color: #000000; ">s</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">d;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(</span><span style="color: #000000; ">3</span><span style="color: #000000; ">*</span><span style="color: #000000; ">s</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">d)&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="color: #000000; ">=</span><span style="color: #000000; ">8</span><span style="color: #000000; ">*</span><span style="color: #000000; ">s</span><span style="color: #000000; ">-</span><span style="color: #000000; ">4</span><span style="color: #000000; ">*</span><span style="color: #000000; ">d;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">s</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">3</span><span style="color: #000000; ">*</span><span style="color: #000000; ">d)&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="color: #000000; ">=</span><span style="color: #000000; ">6</span><span style="color: #000000; ">*</span><span style="color: #000000; ">s</span><span style="color: #000000; ">-</span><span style="color: #000000; ">6</span><span style="color: #000000; ">*</span><span style="color: #000000; ">d;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(s</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">4</span><span style="color: #000000; ">*</span><span style="color: #000000; ">d)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="color: #000000; ">=</span><span style="color: #000000; ">3</span><span style="color: #000000; ">*</span><span style="color: #000000; ">s</span><span style="color: #000000; ">-</span><span style="color: #000000; ">9</span><span style="color: #000000; ">*</span><span style="color: #000000; ">d;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">Deficit</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">continue</span><span style="color: #000000; ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(sum</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">)&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">Deficit</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">sum</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br>}<br></span></div><img src ="http://www.cppblog.com/Onway/aggbug/139159.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2011-01-23 13:34 <a href="http://www.cppblog.com/Onway/articles/139159.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1328 贪心</title><link>http://www.cppblog.com/Onway/articles/139158.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Sun, 23 Jan 2011 05:20:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/139158.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/139158.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/139158.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/139158.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/139158.html</trackback:ping><description><![CDATA[<div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000; "><span style="FONT-SIZE: 18pt ">题意：在x轴上方，给出n个点的xy坐标，在x轴上安放的一点可以以半径为d产生覆盖，问在x轴上至少要安放几个点才可以将x轴上方的点都覆盖起来。如果不能将全部的点覆盖，那么输出-1。&nbsp;</span></span></div><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><span style="color: #000000; "><span style="FONT-SIZE: 18pt ">类型：贪心&nbsp;</span></span></div><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><span style="color: #000000; "><span style="FONT-SIZE: 18pt ">思路：如果上方的某个点S能进行覆盖，那么肯定这个点在x轴上能够确定一个范围，在这个范围安放的任何一个点都能将S覆盖。n个点可以产生n个范围，将这些范围的起点按x轴排序从左到右进行排序，注意这些范围中相同起点而不同终点的排法，应该是终点值较大的先排。排完以后，以第一个点的范围开始进行贪心，在这个范围能覆盖的点都放进来，注意更新这个覆盖范围，直到某个点不能进行覆盖为止，这时需要在x轴多安放一点了。&nbsp;</span></span></div><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><span style="color: #000000; "><span style="FONT-SIZE: 18pt ">解析得真糟糕。其实就是将二维的覆盖转化为一维的覆盖。
这个题目也想了一个多小时，主要是得到上面的思路比较曲折，自我感觉不太满意。这个题目是转到linxu系统上用vim写的第一个题，也值得纪念一下。用g++编译出来，一个编译错误。提交一次AC，gdb调试都不用，呵呵。</span></span><span style="color: #000000; "><br></span></div><img src ="http://www.cppblog.com/Onway/aggbug/139158.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2011-01-23 13:20 <a href="http://www.cppblog.com/Onway/articles/139158.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1064 Cable master 二分搜索</title><link>http://www.cppblog.com/Onway/articles/137117.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Tue, 21 Dec 2010 04:54:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/137117.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/137117.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/137117.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/137117.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/137117.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 14pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br>pku&nbsp;1064&nbsp;Cable&nbsp;master<br></span><span style="COLOR: #008000; TEXT-DECORATION: underline">http://poj.org/problem?id=1064</span><span style="COLOR: #008000"><br>题目分类：二分搜索<br>题意：给出n根不同长度的电缆，要求将这些电缆分割成长度最大且相同的m根。<br>思路：由题目给出的数据规模，最大长度在1厘米到100公里（1千万厘米）之间，<br>可以二分搜索最大长度。<br>总结：由于对二分不熟悉，也是看了别人的提示才想到切入点。有了切入点后，<br>这个题目基本上就算是水题了。写出来交上去居然是果断的WA，在discuss看了<br>很多人都说是卡精度。改了几次交上去还是WA，后来自己出的数据发现，原来是<br>二分的一个变量赋值搞错了。<br>二分的思路很简单，但细节很多，很容易就会写错。<br></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iomanip</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;LARGE</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10000000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cable[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,m;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cut(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;len)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">cable[i]</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">len;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;sum;<br>}<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">m);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%lf</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">tmp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cable[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">(tmp</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">end&nbsp;input</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;left</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,right</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">LARGE,mid,ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">do</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mid</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(left</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">right)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(cut(mid)</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">m)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;right</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mid</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;left</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mid</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(right</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">left</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">right</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">setiosflags(ios::</span><span style="COLOR: #0000ff">fixed</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">setprecision(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">(ans</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">100.0</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>}<br></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/137117.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-12-21 12:54 <a href="http://www.cppblog.com/Onway/articles/137117.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1915 广搜</title><link>http://www.cppblog.com/Onway/articles/136729.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Fri, 17 Dec 2010 05:02:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/136729.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/136729.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/136729.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/136729.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/136729.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br>pku&nbsp;1915&nbsp;Knight&nbsp;Moves<br></span><span style="COLOR: #008000; TEXT-DECORATION: underline">http://poj.org/problem?id=1915</span><span style="COLOR: #008000"><br>题目类型：广度优先搜索（分支限界法）<br>题意：国际象棋中，骑士的移动有一定的规则（具体见原题图），\<br>给定棋盘大小，骑士的起点和终点，求骑士\<br>到达终点的最少移动次数。<br>思路：维持一个队列，将骑士每一步可以到达的点入队，并进行枚举，看是否是终点。\<br>若当前点不是终点，则以该点为起点，将能到达的点入队。<br>总结：这个题目其实是入门级的\<br>广度优先搜索，真没什么好说的，注意一下剪枝就可以。队列可以自己写，也可以用\<br>STL的queue。\<br>用了一个点类，是为了入队。其实代码中的same函数可以放入类中的，但不太熟悉，<br>CE一次（我用VS2010是没事的）。写的过程中思维比较混乱，队列也没有维护好，也\<br>写了很久。对广搜和深搜真的不太熟练。<br></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">queue</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br><br></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;point<br>{<br></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,y;<br>&nbsp;&nbsp;&nbsp;&nbsp;point(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j):x(i),y(j){}<br>};<br>queue</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">point</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;vp;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;num,cnt;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sx,sy,ex,ey;<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;sgn[</span><span style="COLOR: #000000">401</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">401</span><span style="COLOR: #000000">];<br><br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;same(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ax,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ay,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;bx,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;by)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ax</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">bx</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">ay</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">by)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;clear()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">vp.empty())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.pop();<br>}<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;valid(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;y)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">num</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">num</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">sgn[x][y]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;bfs()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;point&nbsp;tmp(</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">vp.empty())<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">vp.front();vp.pop();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(same(tmp.x,tmp.y,ex,ey))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(same(tmp.x,tmp.y,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">cnt;vp.push(point(</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">));</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(sgn[tmp.x][tmp.y]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[tmp.x][tmp.y]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">right;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(tmp.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(tmp.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(tmp.x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(tmp.x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">left</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(tmp.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(tmp.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(tmp.x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(tmp.x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">up</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(tmp.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(tmp.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(tmp.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(tmp.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">down</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(tmp.x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(tmp.x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(tmp.x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(tmp.x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,tmp.y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cas;<br>&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">cas;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cas</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">num</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">sx</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">sy</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">ex</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">ey;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clear();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(sgn,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(sgn));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(sx,sy));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vp.push(point(</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cnt</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bfs();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">cnt</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/136729.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-12-17 13:02 <a href="http://www.cppblog.com/Onway/articles/136729.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1315 回溯法</title><link>http://www.cppblog.com/Onway/articles/136710.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Fri, 17 Dec 2010 02:10:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/136710.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/136710.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/136710.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/136710.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/136710.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">************************************************************************<br>*pku&nbsp;1315&nbsp;Don't&nbsp;Get&nbsp;Rooked<br></span><span style="COLOR: #008000; TEXT-DECORATION: underline">http://poj.org/problem?id=1315</span><span style="COLOR: #008000"><br>题目分类：回溯法<br>题意：n*n的棋盘，类似n后问题，横竖不能放两个棋子，不同的是，少了\<br>对角线的限制，棋盘里多了分割横行和竖行的墙。求最多能放棋子数。<br>思路：枚举第一个棋子的位子，确定第一个棋子位置后，对后面的棋子用\<br>递归深搜（即回溯法）暴力求解剩下能放的棋子。题目的难点是进入深搜时\<br>的标记和回溯时撤销标记的操作。<br>代码附注：近段时间比较少做题，在标记操作里调试了很久，最后还感觉\<br>改得挺恶心的，代码很臃肿。但交上去居然0MS一次AC了，也有借口不改进了。<br>*************************************************************************</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br><br><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;board[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;record[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum,tmp,n;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;code</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;rec(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">n)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(record,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(record));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">board[i];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[i][j]</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">X</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rec(i,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">sum</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;sign(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;k;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">X</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{board[k][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">u</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;record[k][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">code;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">X</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{board[k][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">u</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;record[k][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">code;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">X</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{board[i][k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">u</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;record[i][k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">code;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">X</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{board[i][k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">u</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;record[i][k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">code;}<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;reset(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;k;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">X</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">u</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">record[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">code)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{board[k][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;record[k][j]</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">X</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">u</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">record[k][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">code)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{board[k][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;record[k][j]</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">X</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">u</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">record[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">code)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{board[i][k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;record[i][k]</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">X</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">u</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">record[i][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">code)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{board[i][k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;record[i][k]</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;}<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;rec(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">code;<br>&nbsp;&nbsp;&nbsp;&nbsp;board[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;sign(i,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;row</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;row</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">row)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;col;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(row</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">i)&nbsp;&nbsp;&nbsp;&nbsp;col</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;col</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(;col</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">col)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(board[row][col]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rec(row,col);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(sum</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">tmp)&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;reset(i,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;board[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">code;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">tmp;<br>}<br></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/136710.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-12-17 10:10 <a href="http://www.cppblog.com/Onway/articles/136710.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1651 矩阵连乘</title><link>http://www.cppblog.com/Onway/articles/124888.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Thu, 26 Aug 2010 13:38:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/124888.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/124888.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/124888.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/124888.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/124888.html</trackback:ping><description><![CDATA[&nbsp;
<p style="FONT-SIZE: 18pt">pku 1651 Multiplication Puzzle</p>
<p style="FONT-SIZE: 18pt">题意：给出一组N个数，每次从中抽出一个数（第一和最后一个不能抽），该次的得分即为抽出的数与相邻两个数的乘积。直到只剩下首尾两个数为止。问最小得分是多少。<br></p>
<p style="FONT-SIZE: 18pt">最优子结构：</p>
<p style="FONT-SIZE: 18pt">假设总得分最小时最后抽出的数在k位置，则在1:k和k:n之间的得分也是最小的。因为如果1:k或者k:n具有更小得分，则总得分会更小，与假设矛盾。<br></p>
<p style="FONT-SIZE: 18pt">状态设计：</p>
<p style="FONT-SIZE: 18pt">设dp[i][j]为第i个数到第j个数的最小得分，则dp[1][n]即为题中的解。</p>
<p style="FONT-SIZE: 18pt">1&lt;=i&lt;=n-2;i+2&lt;=j&lt;=n;</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">dp方程：</p>
<p style="FONT-SIZE: 18pt">dp[i][j]=min(dp[i][k]+dp[k][j]+s[i]*s[k]*s[j]);i&lt;k&lt;j;</p>
<p style="FONT-SIZE: 18pt">观察方程，第一维中依赖的是i以后的值，第二维依赖的是j之前的值，所以第一维循环采用逆序循环，第二维采用顺序循环。</p>
<p style="FONT-SIZE: 18pt">&nbsp;
<table style="WIDTH: 180px; HEIGHT: 41px; FONT-SIZE: 12pt" align=center>
    <tbody>
        <tr>
            <td><strong>Memory:</strong> 756K</td>
            <td style="WIDTH: 5px; HEIGHT: 19px"></td>
            <td><strong>Time:</strong> 0MS</td>
        </tr>
        <tr>
            <td><strong>Language:</strong> G++</td>
            <td></td>
            <td><strong>Result:</strong> <font color=blue>Accepted</font></td>
        </tr>
    </tbody>
</table>
</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 14pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dp[</span><span style="COLOR: #000000">103</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">103</span><span style="COLOR: #000000">],s[</span><span style="COLOR: #000000">103</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,i,j,k,min;<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">s[i]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">((dp[i][k]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">dp[k][j]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">s[i]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">s[j]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">s[k])</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">(dp[i][min]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">dp[min][j]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s[i]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">s[j]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">s[min]))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">k;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i][min]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">dp[min][j]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">s[i]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">s[j]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">s[min];&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">dp[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][n]</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br><br></span></div>
<p style="FONT-SIZE: 18pt"><br></p>
<p style="FONT-SIZE: 18pt">这个题目折腾了我很久，估计也有五六个小时。可能是太久（也快半个月了）没做过题，也可能是对矩阵连乘这类DP，或者直接是对DP没有更深入的理解（其实真正领悟DP又何尝简单？可以原谅）。其实矩阵连乘，在前几天才看了第二遍，当时没看书上的代码，认为对于DP看方程看代码其实是没多少意思的，重要的是那个思路，问题的分析，子结构的证明，然后自己写方程写代码。</p>
<p style="FONT-SIZE: 18pt">我是百度矩阵连乘搜到这个题目的，所以这个题目的方法一开始就知道。最先自己简单的分析了一下子结构，然后找了个方程就开始写递归，结果调试就卡住了。然后没用递归，用数组，用堆栈又写了两次，发现都是卡在了同一个地方。</p>
<p style="FONT-SIZE: 18pt">后来怀疑起子结构和方程，原来对子结构没有搞清楚，写出的方程也错的很不靠谱。待我通过了这个题后，看回自己的DP方程，发现兜了一大圈，还真是回到了矩阵连乘这里。</p>
<p style="FONT-SIZE: 18pt">对于一个方法已经知道，类型也见过的题目，总是有一点轻视，加之对原来了解的就不深入，犯错就很容易理解了。</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">最后说说编程上的知识：</p>
<p style="FONT-SIZE: 18pt">1，&nbsp;memset,strlen等字符串处理函数在G++要用到&lt;cstring&gt;头文件</p>
<p style="FONT-SIZE: 18pt">2，&nbsp;scanf,printf要用到&lt;stdio.h&gt;头文件</p>
<p style="FONT-SIZE: 18pt">3，&nbsp;；abs在&lt;cstdlib&gt;中；fabs，sin，sqrt等数学函数在&lt;cmath&gt;中</p>
<img src ="http://www.cppblog.com/Onway/aggbug/124888.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-26 21:38 <a href="http://www.cppblog.com/Onway/articles/124888.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《计算机算法设计与分析》第二版 王晓东 “最大m字段和优化函数”——P57注释</title><link>http://www.cppblog.com/Onway/articles/123493.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Sun, 15 Aug 2010 06:07:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/123493.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/123493.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/123493.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/123493.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/123493.html</trackback:ping><description><![CDATA[<p style="font-size: 18pt">之所以想到要注释一下，没别的意思，只是因为几个月前刚学DP，完全看不懂，前几天费了几个小时终于看懂了，注释下来，能使自己整理一下思路，也作为自己的一篇日记。<o:p></o:p></p>
<p style="font-size: 18pt">当时我能看懂时间和空间均为O(MN^2)的函数，但可能由于自己看书是直接从动态规划一章看起，书上对于经过优化的函数也没有更多的解析，当时看起来完全不知所云。前几天看的时候，是对着方程结合书上的几句话自己去理解，尝试自己动手去写，但错了。看书上代码，都要自己去理解那些变量数组是干什么用的，感觉也是非常吃力。<o:p></o:p></p>
<p style="font-size: 18pt"><o:p></o:p></p>
<p style="font-size: 18pt">1，<o:p></o:p></p>
<p style="font-size: 18pt">我觉得理解那个优化函数，必须先要充分理解那个二维的DP方程：<o:p></o:p></p>
<p style="font-size: 18pt">b[i][j]=max(b[i][j-1]+a[j],max(b[i-1][t])+a[j])&nbsp; (i-1&lt;=t&lt;j)</p>
<p style="font-size: 18pt">b[i][j]表示的是第i段在前j项（含第j项）的最大值。对于b[i][j]含有第j项必须要理解好！则方程的意思是：对于a[j]项，要么将它加到第i段，要么它自己作为新的一段。<o:p></o:p></p>
<p style="font-size: 18pt">注意到方程外层的max选择，都要加上a[j]这一项。这里可能很容易产生一个疑问，如果a[j]是一个负数，为什么还要加上a[j]呢？回到b[i][j]所表示的意义上解释，由于b[i][j]表示的是含有第j项的最大值，所以a[j]是肯定要加进来的。假使a[j]是一个负数，它加进来了也不会影响在第i段中前j-1项的最大值。<o:p></o:p></p>
<p style="font-size: 18pt">所以第m段的最大值，并不是b[m][n]，而是b[m][m~n]之间的最大值。<o:p></o:p></p>
<p style="font-size: 18pt">同样道理，第i-1段的最大值是b[i-1][t],(i-i&lt;=t&lt;j)。<o:p></o:p></p>
<p style="font-size: 18pt">2，<o:p></o:p></p>
<p style="font-size: 18pt">理解了上述的DP方程后，再来考虑优化，正如书上所说的，由于在第i段中，只用到第i段和第i-1段的值。如果采用一维数组存储b[]，只要在计算第i段的时候，没有去改变第i-1段保留下来的值即可。再考虑到，内层max选择，需要用到第i-1段在i-1到j-1位置的最大值，不但要进行重复计算，也影响到b[j]的计算，因为j前面的值既要用作b[j]的计算，也要更新作为下一段i+1计算时所用。<o:p></o:p></p>
<p style="font-size: 18pt">为了解决这个问题，书上是利用了一个辅助的一维数组c[]，c[j]保存的是上一段i-1在j位置的最大值。显然计算第i段在j位置的值b[j]要用到的c[]的下标是从i-1到j-1的。<o:p></o:p></p>
<p style="font-size: 18pt">那么这时DP方程可以变为：<o:p></o:p></p>
<p style="font-size: 18pt">b[j]=max(b[j-1]+a[j],c[j-1]+a[j])</p>
<p style="font-size: 18pt">要注意，b[j-1]与c[j-1]是相差一段的，即b[j-1]计算的是第i段的值，而c[j-1]记录的是i-1段的值。<o:p></o:p></p>
<p style="font-size: 18pt">这里有一个关键是对c[]在计算第i段值b[j]的时候，要及时更新，以作计算下一段使用。<o:p></o:p></p>
<p style="font-size: 18pt">3，<o:p></o:p></p>
<p style="font-size: 18pt">以上的具体实现自己写不出来，还是照看书上的代码，略作解析。其实明白了上面说明的两段，书上代码是很好理解的。书上对于边界条件的处理，我觉得做得非常好，而我也正是错在这个地方。<br /><br /><o:p></o:p></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 14pt; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;MaxSum(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">a)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(n</span><span style="color: #000000">&lt;</span><span style="color: #000000">m</span><span style="color: #000000">||</span><span style="color: #000000">m</span><span style="color: #000000">&lt;</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">b</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">[n</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">b[j]保存的是第i段中在前j（含j）项的最大值</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">c</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">[n</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">c[j]保存的是i-1段中在i-1到j（含j）项中的最大值<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">注意，以上b[j]和c[j]都是指在计算第i段时的值；</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;b[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;c[</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">第0段时的两个值，初始化边界条件&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">m;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">i为当前计算段数&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b[i]</span><span style="color: #000000">=</span><span style="color: #000000">b[i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">a[i];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">b[i]即b[j]在j=i时的值，由于j=i，每一个项都要成为一段，这就是边界条件</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">b[i];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">这里很绕，其实这句是没用的，因为在第i段中数组c[]保存的值是为下一<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">段i+1服务的，i+1段只用到c[i]，可以直接删掉，免得误导&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;max</span><span style="color: #000000">=</span><span style="color: #000000">b[i];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">max记录第i段的从i位置开始所有能用到的项的最大值</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">i</span><span style="color: #000000">+</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">m;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b[j]</span><span style="color: #000000">=</span><span style="color: #000000">b[j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">&gt;</span><span style="color: #000000">c[j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">?</span><span style="color: #000000">b[j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">a[j]:c[j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">a[j];</span><span style="color: #008000">//</span><span style="color: #008000">这里用到的c[j-1]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">是没被修改的，还是上一段i-1中在j-1位置的最大值</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">max;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">用完后就要修改为第i段中在j-1位置的最大值，明显，max是记录了第i段中的</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(max</span><span style="color: #000000">&lt;</span><span style="color: #000000">b[j])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">在j-1位置的最大值，尽管当前循环中计算的是第j项</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max</span><span style="color: #000000">=</span><span style="color: #000000">b[j];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">更新max的值</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[i</span><span style="color: #000000">+</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">m]</span><span style="color: #000000">=</span><span style="color: #000000">max;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">由于max记录的项总比循环的j项小1，所以第i段在最后一项中的最大值放在循环外更新</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;sum</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">默认全是负数的时候，最大值是0,如果要计算负的最大值，可以将sum设为一个大负数</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">m;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;j</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">为什么还有一次循环找最大值，而不是直接使用b[n]呢？因为b[j]包含了a[j]，</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(sum</span><span style="color: #000000">&lt;</span><span style="color: #000000">b[j])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">在b[j]的值不一定比不包含a[j]的其他项大。</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="color: #000000">=</span><span style="color: #000000">b[j];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;sum;<br />}</span></div>
<br />附：pku 2479就是一题求最大2段和。<!--endfragment--><img src ="http://www.cppblog.com/Onway/aggbug/123493.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-15 14:07 <a href="http://www.cppblog.com/Onway/articles/123493.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 2109 一句话就能AC得题目？</title><link>http://www.cppblog.com/Onway/articles/123079.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Wed, 11 Aug 2010 08:49:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/123079.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/123079.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/123079.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/123079.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/123079.html</trackback:ping><description><![CDATA[<p style="font-size: 18pt">&nbsp;pku 2109 一句话就能AC得题目？</p>
<p style="font-size: 18pt">这个题目的是求一个大整数p（10^101）开n（1&lt;=n&lt;=200）次方的结果k，即k^n=p或者，n=log k (p).</p>
<p style="font-size: 18pt">题目说保证p和n都是整数，并且求到得结果k一定是一个整数。但discuss里有说，有些测试数据是不存在整数k的。</p>
<p style="font-size: 18pt">这个题目的本意应该是高精度加二分吧，但我没写。</p>
<p style="font-size: 18pt">在discuss看到，有些是直接用double和pow()函数的，一句话AC了以后，有很大的&#8220;罪恶感&#8221;。</p>
<p style="font-size: 18pt">本人小菜，连double和pow()都不会用，用了也是一头雾水，觉得这种方法能通过，完全是因为测试数据太弱。</p>
<p style="font-size: 18pt">（在VC++ 6.0调试）</p>
<p style="font-size: 18pt">一句话能AC的C代码是这样的：<br /></p>
<p style="font-size: 18pt"></p>
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 12pt; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">math.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;n,p;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%lf%lf</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n,</span><span style="color: #000000">&amp;</span><span style="color: #000000">p)</span><span style="color: #000000">!=</span><span style="color: #000000">EOF)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%.0f\n</span><span style="color: #000000">"</span><span style="color: #000000">,pow(p,</span><span style="color: #000000">1</span><span style="color: #000000">/</span><span style="color: #000000">n));<br />}</span></div>
<p style="font-size: 18pt"><br />首先用double接受一个10^101次方的数，确实可以，因为double的范围是-1.7^308~1.7^308，但精度只有16或17位（四舍五入位）。</p>
<p style="font-size: 18pt">然后设p是一个大于17位的整数，那么四舍五入后可能得到的两个值p1和p2，不妨记p1&gt;p,p2&lt;p。</p>
<p style="font-size: 18pt">就算测试数据都是合法的，就是说能保证k是一个整数，即有k=p^(1/n)。那么可以保证的是p1^(1/n)&gt;k而p2^(1/n)&lt;k的。</p>
<p style="font-size: 18pt">那么问题就来了，p1^(1/n)的上界怎么确定，p2^(1/n)的下界又怎么确定呢？</p>
<p style="font-size: 18pt">用double和pow()函数至少要能确保k+1&gt;p1^(1/n)&gt;k和k-1&lt;p2^(1/n)&lt;k吧？因为只有这样，对结果<span lang="EN-US" style="font-size: 18pt; font-family: 'Times New Roman'; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">pow(p,1/n)</span>四舍五入才能得到结果k。</p>
<p style="font-size: 18pt">但如何能确保对p用double存储的时候得到的估计值p1和p2的精确度在</p>
<p style="font-size: 18pt">p1-p&lt;(k+1)^n-k^n和p-p2&gt;k^n-(k-1)^n呢？</p>
<p style="font-size: 18pt">本人的智商只能到这里，还望各路神牛不吝赐教。</p><img src ="http://www.cppblog.com/Onway/aggbug/123079.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-11 16:49 <a href="http://www.cppblog.com/Onway/articles/123079.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 3620简单搜索</title><link>http://www.cppblog.com/Onway/articles/122949.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Tue, 10 Aug 2010 08:49:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122949.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122949.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122949.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122949.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122949.html</trackback:ping><description><![CDATA[&nbsp;
<p style="FONT-SIZE: 18pt">pku 3620简单搜索</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">题意：在一块地里面，干燥的点标记为0，潮湿的点标记为1；求相连的1的最大个数。</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">由于自己对搜索不太熟悉，当时以为是记忆化搜索，后来用dfs和bfs各写了一次后，感觉就是一纯粹的搜索而已。</p>
<p style="FONT-SIZE: 18pt">我到目前也还觉得，记忆化搜索是DP的一类，搜索过的地方，记录下来，是要多次用到的。而这个题的搜索，只需简单的标记一下搜索过的地方，以便不会再搜，而对已搜索的地方进行标记是搜索题的一个共性吧？</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">当时写dfs多开了一个数组进行标记，后来看了一下别人的代码，看到标记数组都不用开，直接在原数组进行标记就得。这样我才开始思考，记忆化搜索与简单搜索的不同，记忆化搜索由于要用到搜索过的地方，直接用原数组进行了标记，就无法记录（也不能太绝对，可能有些情况用记录值也可以达到标记的效果，这个题貌似也可以，但可能就比较麻烦）。</p>
<p style="FONT-SIZE: 18pt">这个题目用原数组进行标记的话，就要开一个变量统计每一轮搜索的最大值。但这种方法确实是好。</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">然后用bfs来写的话，就要用到一个队列。当时自己写一个队列类，调了N久才发现自己错在一个很低级的理解错误上。我定义一个类，类里面有静态成员，我的目的就是想让静态数据成员进行记录队列长度。然后又将类像链表结构体一样进行动态分配进行链接，还以为那个静态成员依然是原来那个。</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">写出来后发现有很大浪费，主要是由于指针指向没有认真分析，然后稳健性也非常不好。</p>
<p style="FONT-SIZE: 18pt">干脆用STL的queue算了。</p>
<p style="FONT-SIZE: 18pt">然后又没有注意同一个点进行了多次进队，WA一次。</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 14pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;dfs</span><span style="COLOR: #008000"><br>/*</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>#include&nbsp;&lt;iostream&gt;<br>using&nbsp;namespace&nbsp;std;<br>int&nbsp;data[101][101],n,m;<br>bool&nbsp;sgn[101][101];<br>int&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;dfs(int,int);<br>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;k,i,j,r,c,ans=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;memset(data,0,sizeof(data));<br>&nbsp;&nbsp;&nbsp;&nbsp;memset(sgn,0,sizeof(sgn));<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d%d%d",&amp;n,&amp;m,&amp;k);<br>&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=k;++i)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d%d",&amp;r,&amp;c);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[r][c]=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=n;++i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=1;j&lt;=m;++j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(i,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans=ans&gt;data[i][j]?ans:data[i][j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n",ans);<br>}<br>int&nbsp;dfs(int&nbsp;i,int&nbsp;j)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;if(sgn[i][j])&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;<br>&nbsp;&nbsp;&nbsp;&nbsp;if(data[i][j]!=1)&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;data[i][j];<br>&nbsp;&nbsp;&nbsp;&nbsp;sgn[i][j]=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;data[i][j]+=dfs(i-1,j)+dfs(i,j+1)+dfs(i+1,j)+dfs(i,j-1);<br>}<br></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br><br><br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">bfs</span><span style="COLOR: #008000"><br>/*</span><span style="COLOR: #008000"><br>#include&nbsp;&lt;iostream&gt;<br>#include&nbsp;&lt;queue&gt;<br>using&nbsp;namespace&nbsp;std;<br>int&nbsp;data[101][101],n,m,num;<br>struct&nbsp;point<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;x;int&nbsp;y;<br>};<br>queue&lt;point&gt;&nbsp;myq;<br>point&nbsp;conver(int&nbsp;a,int&nbsp;b)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;point&nbsp;f;f.x=a;f.y=b;return&nbsp;f;<br>}<br>void&nbsp;bfs()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;point&nbsp;s;<br>&nbsp;&nbsp;&nbsp;&nbsp;while(!myq.empty())<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s=myq.front();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myq.pop();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(data[s.x+1][s.y]==1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{myq.push(conver(s.x+1,s.y));data[s.x+1][s.y]=0;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(data[s.x][s.y+1]==1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{myq.push(conver(s.x,s.y+1));data[s.x][s.y+1]=0;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(data[s.x-1][s.y]==1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{myq.push(conver(s.x-1,s.y));data[s.x-1][s.y]=0;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(data[s.x][s.y-1]==1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{myq.push(conver(s.x,s.y-1));data[s.x][s.y-1]=0;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++num;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br>}<br>int&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;k,i,j,r,c,max=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d%d%d",&amp;n,&amp;m,&amp;k);<br>&nbsp;&nbsp;&nbsp;&nbsp;memset(data,0,sizeof(data));<br>&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=k;++i)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d%d",&amp;r,&amp;c);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[r][c]=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=n;++i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=1;j&lt;=n;++j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(data[i][j]==1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[i][j]=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myq.push(conver(i,j));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bfs();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(num&gt;max)&nbsp;&nbsp;&nbsp;&nbsp;max=num;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n",max);<br>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;<br>}<br><br><br></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/122949.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-10 16:49 <a href="http://www.cppblog.com/Onway/articles/122949.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>浅析《背包九讲-分组背包》中的错误</title><link>http://www.cppblog.com/Onway/articles/122695.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Sun, 08 Aug 2010 13:08:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122695.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122695.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122695.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122695.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122695.html</trackback:ping><description><![CDATA[<p style="font-size: 18pt">发现问题的起因是HDU&nbsp;1712，一个赤裸的分组背包。所以有必要说一下这个题目。<o:p></o:p></p>
<p style="font-size: 18pt"><o:p></o:p></p>
<p style="font-size: 18pt">题意：<o:p></o:p></p>
<p style="font-size: 18pt">一个学生用M天的时间复习N门课程，每门课程花费不同的天数，有不同的收获。问如何安排这M天，使得收获最大。<o:p></o:p></p>
<p style="font-size: 18pt"><o:p></o:p></p>
<p style="font-size: 18pt">思路：<o:p></o:p></p>
<p style="font-size: 18pt">可以将每一门课看成一个分组，每门课不同天数的选择看成是分组的物品（显然只能有一个选择），物品的费用即为花费的天数，物品的价值为题中给出的收获。该题中背包容量最大为M。<o:p></o:p></p>
<p style="font-size: 18pt">设dp[x]为前i组物品，在背包容量为x（即费用为x）时的最大价值。则将i从1到N进行过历遍后（第一重循环），dp[m]即为所求。<o:p></o:p></p>
<p style="font-size: 18pt">在这种状态设置中，容易想出以下两种阶段递推方式（以下所述都为第二和第三重循环）：<o:p></o:p></p>
<p style="font-size: 18pt">1，在同一个背包容量中，对不同费用的物品进行枚举比较：<o:p></o:p></p>
<p style="font-size: 18pt">for(j=MAX;j&gt;=1;--j)&nbsp;&nbsp;&nbsp;//背包容量<o:p></o:p></p>
<p style="font-size: 18pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(k=1;k&lt;=m;++k)&nbsp;&nbsp;//不同费用的物品<o:p></o:p></p>
<p style="font-size: 18pt">2，在同一费用的物品中，对放在不同背包容量时计算最大价值：（该方式同《背包九讲-分组背包》中的伪代码部分）<o:p></o:p></p>
<p style="font-size: 18pt">for(k=1;k&lt;=m;++k)&nbsp;&nbsp;&nbsp;&nbsp;//不同费用的物品<o:p></o:p></p>
<p style="font-size: 18pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=MAX;j&gt;=1;--j)&nbsp;&nbsp;//背包容量<o:p></o:p></p>
<p style="font-size: 18pt"><o:p></o:p></p>
<p style="font-size: 18pt">简略分析：<o:p></o:p></p>
<p style="font-size: 18pt">1，分析第一种递推方式的正确:<o:p></o:p></p>
<p style="font-size: 18pt">该方式即求在容量为j的背包中，选择哪一个物品可以有最大价值。<o:p></o:p></p>
<p style="font-size: 18pt">看递推方程：dp[j]=max(dp[j],dp[j-c[k]]+w[k]);（其中c[k]为k物品的费用，w[k]为价值），由于递降枚举背包容量，max比较中的dp[j]是由上一组物品决策所得，在这里将被忽略。因为就算不忽略，在本组物品中dp[j]的决策依然要取决于dp[j-c[k]]+w[k]。<o:p></o:p></p>
<p style="font-size: 18pt">而同样由于递降枚举背包容量（第二重循环），dp[j-c[k]]在本组物品中是未进行过决策的，亦即背包容量为j-c[k]时，在本组物品中是没有选择任何物品的，这可以保证对dp[j]决策时，不会多选本组中的物品。<o:p></o:p></p>
<p style="font-size: 18pt">2，分析第二种递推方式的错误：<o:p></o:p></p>
<p style="font-size: 18pt">该方式即求对物品k，放在所有背包中，计算各个最大价值。<o:p></o:p></p>
<p style="font-size: 18pt">同样是递推方程：dp[j]=max(dp[j],dp[j-c[k]]+w[k]);（其中c[k]为k物品的费用，w[k]为价值）。能否保证dp[j-c[k]]在本组中未经决策，就成了该递推方式对错的关键。<o:p></o:p></p>
<p style="font-size: 18pt">由于背包容量的递降枚举在第三重循环，只能保证k物品不会重复选择。对于另一k0物品，当背包容量枚举到j-c[k]的时候，由方程可以有：dp[j-c[k]]=max(dp[j-c[k]],dp[j-c[k]-c[k0]]+w[k0]，亦即dp[j-c[k]]可能在本组中的其他物品中进行过决策。<o:p></o:p></p>
<p style="font-size: 18pt">那么这样就可能导致在一组物品中选择了多件物品。<o:p></o:p></p>
<p style="font-size: 18pt"><o:p></o:p></p>
<p style="font-size: 18pt">----------------------------------------------------------------------------------------------------------------------<o:p></o:p></p>
<p style="font-size: 18pt">周五的早上AC了那个题目，但是感觉留下了一堆的问题。然后忙了两天别的事，直到今天才感觉彻底搞懂了。<o:p></o:p></p>
<p style="font-size: 18pt">在最近的几个题中，也算逐渐明白知识学习与能力培养的区别。<o:p></o:p></p>
<p style="font-size: 18pt">----------------------------------------------------------------------------------------------------------------------<o:p></o:p></p>
<p style="font-size: 18pt"><o:p></o:p></p>
<p style="font-size: 18pt">以下附《背包九讲-分组背包》中的内容和HDU&nbsp;1712的代码。<o:p></o:p></p>
<p style="font-size: 18pt">分组背包：<o:p></o:p></p>
<p style="color: #0000ff; font-size: 14pt">P06:&nbsp;分组的背包问题<br />&nbsp;问题<br />&nbsp;有N件物品和一个容量为V的背包。第i件物品的费用是c[i]，价值是w[i]。这些物品被划分为若干组，每组中的物品互相冲突，最多选一件。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量，且价值总和最大。<br /><br />&nbsp;算法<br />&nbsp;这个问题变成了每组物品有若干种策略：是选择本组的某一件，还是一件都不选。也就是说设f[k][v]表示前k组物品花费费用v能取得的最大权值，则有f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]|物品i属于第k组}。<br /><br />&nbsp;使用一维数组的伪代码如下：<br /><br />&nbsp;for&nbsp;所有的组k<br />&nbsp;for&nbsp;所有的i属于组k<br />&nbsp;for&nbsp;v=V..0<br />&nbsp;f[v]=max{f[v],f[v-c[i]]+w[i]}<br /><br />&nbsp;另外，显然可以对每组中的物品应用P02中&#8220;一个简单有效的优化&#8221;。<br /><br />&nbsp;小结<br />&nbsp;分组的背包问题将彼此互斥的若干物品称为一个组，这建立了一个很好的模型。不少背包问题的变形都可以转化为分组的背包问题（例如P07），由分组的背包问题进一步可定义&#8220;泛化物品&#8221;的概念，十分有利于解题。<o:p></o:p></p>
<p style="font-size: 18pt"><o:p></o:p></p>
<p style="font-size: 18pt">----------------------------------------------------------------------------------------------------------------------<br /><o:p></o:p></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 14pt; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008000">//</span><span style="color: #008000">HDU&nbsp;1712(被注释的为以上第二种递推方式）：</span><span style="color: #008000"><br /></span><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;MAX</span><span style="color: #000000">=</span><span style="color: #000000">100</span><span style="color: #000000">;<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;dp[MAX</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">],data[MAX</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">][MAX</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,m;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n,</span><span style="color: #000000">&amp;</span><span style="color: #000000">m)</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">(n</span><span style="color: #000000">||</span><span style="color: #000000">m))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,k;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">m;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">data[i][j]);<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">/*</span><span style="color: #008000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,0,sizeof(dp));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=n;++i)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=m;j&gt;=1;--j)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(k=MAX;k&gt;=j;--k)&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(dp[k]&lt;dp[k-j]+data[i][j])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[k]=dp[k-j]+data[i][j];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">*/</span><span style="color: #000000"><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(dp));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">MAX;j</span><span style="color: #000000">&gt;=</span><span style="color: #000000">1</span><span style="color: #000000">;</span><span style="color: #000000">--</span><span style="color: #000000">j)&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(k</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;k</span><span style="color: #000000">&lt;=</span><span style="color: #000000">m;</span><span style="color: #000000">++</span><span style="color: #000000">k)&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(j</span><span style="color: #000000">&gt;=</span><span style="color: #000000">k)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j]</span><span style="color: #000000">=</span><span style="color: #000000">dp[j]</span><span style="color: #000000">&gt;</span><span style="color: #000000">dp[j</span><span style="color: #000000">-</span><span style="color: #000000">k]</span><span style="color: #000000">+</span><span style="color: #000000">data[i][k]</span><span style="color: #000000">?</span><span style="color: #000000">dp[j]:dp[j</span><span style="color: #000000">-</span><span style="color: #000000">k]</span><span style="color: #000000">+</span><span style="color: #000000">data[i][k];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,dp[m]);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />}</span></div>
<!--endfragment--><img src ="http://www.cppblog.com/Onway/aggbug/122695.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-08 21:08 <a href="http://www.cppblog.com/Onway/articles/122695.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1948 0-1背包 加强版</title><link>http://www.cppblog.com/Onway/articles/122625.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Sun, 08 Aug 2010 03:08:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122625.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122625.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122625.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122625.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122625.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 18pt">pku&nbsp;1948&nbsp;Triangular&nbsp;Pastures&nbsp;0-1背包思想<o:p></o:p></p>
<p style="FONT-SIZE: 18pt"><o:p></o:p></p>
<p style="FONT-SIZE: 18pt">题意：有N个不同长度fence，求用所有fence能组成的最大三角形的面积。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt"><o:p></o:p></p>
<p style="FONT-SIZE: 18pt">几天前就看到这个题了，今天才过掉，对这个题感觉非常失败。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt"><o:p></o:p></p>
<p style="FONT-SIZE: 18pt">首先第一想法是：将总边长的三分之一做为背包容量，进行一次0-1背包，然后再用剩下的三分之二边长的一半做背包容量，再进行一次0-1背包。但问题是进行第一次背包的时候要记录使用过的边，以便在第二次背包时不再使用。但由于能力问题，动手写过两次都无法解决这个问题。百度一下，发现人家也想到这个思路，但都被否决了，原因是这样的思路，通过找三边差值最小，并不能保证面值最大。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt"><o:p></o:p></p>
<p style="FONT-SIZE: 18pt">然后我就没想法了。直到今天，看到marvin（网上认识的ACMer）的blog，才知道思路应该是这样的：枚举第一条边和第二条边的组合，用sgn[j][k]标记第一边长为j，第二边长为k。如果能从给出的边里组成这两条边，则进行标记。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">看到这个思路后，发现原来就是这么简单，但怎么自己就没有想到呢。然后马上动手就写，又WA到快吐血。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">虽然很快发现问题出在了用背包枚举两边组合的那几行代码，但就是改不过来，在那里纠结得跳楼的心都有。做了一个星期的背包，居然连这几行代码都写不出，都羞死了。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt"><o:p></o:p></p>
<p style="FONT-SIZE: 18pt">写不出那几个代码的原因：1，对用背包枚举两边组合的原理理解不透彻。2，没有认真分析组合情况。3，做单纯的背包题时留下的思维僵硬。4.，将或运算想当然的写成了并运算。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt"><o:p></o:p></p>
<p style="FONT-SIZE: 18pt">AC了这个题目后，对背包枚举两边组合部分的三重循环的边界条件感觉还是很抽象。再深入去想，才逐渐明了。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">1，需要枚举的两条边的长度都是不能超过总长的一半的。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">2，由于各边只能使用一次，同0-1背包，循环需要用逆序。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">3，对于第条边，如果已有sgn[j-fence[i]][k]或者sgn[j][k-fence[i]]，则必有sgn[j][k]。由于这种组合只需满足其中一个即可，所以j和k都要枚举到0并且要保证j&gt;=fence[i]或者k&gt;=fence[i]。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">4，优化速度的问题。由于j和k可能会出现长度交换时会进行重复计算，所以在计算的时候可以只取j&gt;=k的情况，但枚举组合的时候能取j&gt;=k进行枚举吗？表面上对j&gt;=k的情况进行过枚举，似乎就满足目的了。但实际上，这样的j&gt;=k的限制导致，能枚举，但不能有效地标记组合。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt"><o:p></o:p></p>
<p style="FONT-SIZE: 18pt">设sgn[j0][k0]为确定组合(0&lt;=k0&lt;=j0)&nbsp;&nbsp;（确定组合即为能够确定这种组合的存在与否）<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">若k0+fenc[i]&gt;j0时<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">fence[i]只能加到第一边，得到确定组合sgn[j0+fence[i]][k0]<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">当fence[i+1]+k0&gt;j0+fence[i]时，(显然，这时fence[i+1]&gt;fence[i])<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">fence[i+1]又只能加到第一边。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt">这样sgn[j0+fence[i+1]][k0+fence[i]]的组合就只能标记为失败了。<o:p></o:p></p>
<p style="FONT-SIZE: 18pt"><o:p></o:p></p>
<p style="FONT-SIZE: 18pt">解决方式是：对于fence[i]，只要第一边不使用这个fence[i]的时候，必须要让它能够加到第二边。这样去k=j+fence[i]即可。<br><br><o:p></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 14pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">math.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">800</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;sgn[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;fence[</span><span style="COLOR: #000000">41</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;find(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,k,n,sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)&nbsp;&nbsp;&nbsp;&nbsp;{scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">fence[i]);sum</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">fence[i];}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;memset(sgn,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(sgn));<br>&nbsp;&nbsp;&nbsp;&nbsp;sgn[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sum</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">fence[i];k</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">((j</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">fence[i]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">sgn[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">fence[i]][k])</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(k</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">fence[i]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">sgn[j][k</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">fence[i]]))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[j][k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;ans</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">0.01</span><span style="COLOR: #000000">,tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">sum</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">i;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(sgn[i][j])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">find(sum</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">j,i,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">ans</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">tmp</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">ans:tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">(ans</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;find(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;y,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;z)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">z</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">z</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">z</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">x)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;sqrt(</span><span style="COLOR: #000000">1.0</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">z)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">z)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">z</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">y)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">z</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">x))</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">0.25</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">0.01</span><span style="COLOR: #000000">;<br>}<br></span></div>
</o:p><!--endfragment-->
<img src ="http://www.cppblog.com/Onway/aggbug/122625.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-08 11:08 <a href="http://www.cppblog.com/Onway/articles/122625.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU 2159 FATE 二维费用的背包</title><link>http://www.cppblog.com/Onway/articles/122367.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Thu, 05 Aug 2010 14:37:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122367.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122367.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122367.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122367.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122367.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 14pt">HDU&nbsp;2159&nbsp;FATE&nbsp;二维费用的背包<o:p></o:p></p>
<p style="FONT-SIZE: 14pt"><o:p></o:p></p>
<p style="FONT-SIZE: 14pt">在pku用二维费用重做了一次1882以后，发现这个题其实也挺简单的。<o:p></o:p></p>
<p style="FONT-SIZE: 14pt"><o:p></o:p></p>
<p style="FONT-SIZE: 14pt">先说一下pku1882，第一个费用是张数，第二个费用是与价值等同的重量，即题中的面值。<o:p></o:p></p>
<p style="FONT-SIZE: 14pt">可以设计状态为f[i][j]表示用到第i张邮票组成面值为j时能获得的最大价值，即能组成最大的面值（&lt;=j）。然后是转化为完全背包来做，循环顺序由低到高。<o:p></o:p></p>
<p style="FONT-SIZE: 14pt"><o:p></o:p></p>
<p style="FONT-SIZE: 14pt">然后hdu&nbsp;2159比pku&nbsp;1882好处理，要处理的数据比较少。不同的是，二维费用改变了，背包状态的设计也改变了。<o:p></o:p></p>
<p style="FONT-SIZE: 14pt">设f[i][j]表示杀到第i个怪，忍耐度在j的时候能获得的最大价值。注意这样设计状态的话，开的数组不用很大。我是数组开小了，贡献了一次RE，很厚道，报的是RE，如果是WA，就难受了。<o:p></o:p></p>
<p style="FONT-SIZE: 14pt">这样设计状态，在后面查找结果的时候要对j进行一次历遍查找。<br><br><o:p></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 14pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;data[</span><span style="COLOR: #000000">101</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">],exp[</span><span style="COLOR: #000000">101</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">101</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;fmax(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;b)<br>{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;a</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">a:b;}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;rem,bear,kind,kill;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">rem,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">bear,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">kind,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">kill)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">EOF)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,k;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">kind;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">data[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">],</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">data[i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(exp,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(exp));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">kind;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">kill;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">data[i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];k</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">bear;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exp[j][k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">fmax(exp[j][k],exp[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][k</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">data[i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">data[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">kill,j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">bear;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(exp[i][j]</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">rem)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">bear)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,bear</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">j);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div>
</o:p><!--endfragment-->
<img src ="http://www.cppblog.com/Onway/aggbug/122367.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-05 22:37 <a href="http://www.cppblog.com/Onway/articles/122367.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 2392 Space Elevator 多重背包</title><link>http://www.cppblog.com/Onway/articles/122283.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Thu, 05 Aug 2010 03:05:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122283.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122283.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122283.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122283.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122283.html</trackback:ping><description><![CDATA[&nbsp;
<p style="FONT-SIZE: 18pt">pku 2392 Space Elevator 多重背包</p>
<p style="FONT-SIZE: 18pt">&nbsp;其实这个题找到了切入口后，是挺简单的。我似乎总有一种先入为主的倾向，想到一点点就下手，结果浪费时间，还掉入死胡同里。总之我是没找到那个切入口，然后看了一下discuss，原来排序就可以了，ft！！怎么我就笨得想不到了。其实我是想过排序，但没有深入去考虑。</p>
<p style="FONT-SIZE: 18pt">&nbsp;整个题最后的解肯定是不大于最大高度的type的。</p>
<p style="FONT-SIZE: 18pt">将最大高度排序后，先叠好高度低的，高度大的叠在上面。</p>
<p style="FONT-SIZE: 18pt">每一高度只要能组合就行进标记，不再浪费后面的blocks。同时对于某一blocks，只要枚举高度到该type的最大高度即可，并记录改高度使用了该type的blocks的个数。（背包部分）</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">40000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MMAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">400</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;type<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_115_129_Open_Image onclick="this.style.display='none'; Codehighlighter1_115_129_Open_Text.style.display='none'; Codehighlighter1_115_129_Closed_Image.style.display='inline'; Codehighlighter1_115_129_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_115_129_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_115_129_Closed_Text.style.display='none'; Codehighlighter1_115_129_Open_Image.style.display='inline'; Codehighlighter1_115_129_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_115_129_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_115_129_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;h,a,c;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">data[MMAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;cmp(type&nbsp;x,type&nbsp;y)<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_168_187_Open_Image onclick="this.style.display='none'; Codehighlighter1_168_187_Open_Text.style.display='none'; Codehighlighter1_168_187_Closed_Image.style.display='inline'; Codehighlighter1_168_187_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_168_187_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_168_187_Closed_Text.style.display='none'; Codehighlighter1_168_187_Open_Image.style.display='inline'; Codehighlighter1_168_187_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_168_187_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_168_187_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;x.a</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">y.a;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnt[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;sgn[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img id=Codehighlighter1_233_690_Open_Image onclick="this.style.display='none'; Codehighlighter1_233_690_Open_Text.style.display='none'; Codehighlighter1_233_690_Closed_Image.style.display='inline'; Codehighlighter1_233_690_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_233_690_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_233_690_Closed_Text.style.display='none'; Codehighlighter1_233_690_Open_Image.style.display='inline'; Codehighlighter1_233_690_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_233_690_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_233_690_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,i,j;<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">data[i].h,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">data[i].a,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">data[i].c);<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;sort(data</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,data</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">n,cmp);<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;memset(sgn,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(sgn));<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;sgn[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_421_605_Open_Image onclick="this.style.display='none'; Codehighlighter1_421_605_Open_Text.style.display='none'; Codehighlighter1_421_605_Closed_Image.style.display='inline'; Codehighlighter1_421_605_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_421_605_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_421_605_Closed_Text.style.display='none'; Codehighlighter1_421_605_Open_Image.style.display='inline'; Codehighlighter1_421_605_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_421_605_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_421_605_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(cnt,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(cnt));<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">data[i].h;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">data[i].a;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">sgn[j]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">sgn[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">data[i].h]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cnt[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">data[i].h]</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">data[i].c)<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img id=Codehighlighter1_552_602_Open_Image onclick="this.style.display='none'; Codehighlighter1_552_602_Open_Text.style.display='none'; Codehighlighter1_552_602_Closed_Image.style.display='inline'; Codehighlighter1_552_602_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_552_602_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_552_602_Closed_Text.style.display='none'; Codehighlighter1_552_602_Open_Image.style.display='inline'; Codehighlighter1_552_602_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_552_602_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_552_602_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cnt[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cnt[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">data[i].h]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">data[n].a;j</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(sgn[j])<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,j);<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/122283.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-05 11:05 <a href="http://www.cppblog.com/Onway/articles/122283.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1384 Piggy-Bank 完全背包</title><link>http://www.cppblog.com/Onway/articles/122267.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Thu, 05 Aug 2010 01:28:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122267.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122267.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122267.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122267.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122267.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 18pt">&nbsp;pku 1384 Piggy-Bank 完全背包。</p>
<p style="FONT-SIZE: 18pt">正如网上说的，这是个挺裸的背包了。做了几个背包的题后看到这题，感觉就挺简单了。</p>
<p style="FONT-SIZE: 18pt">这里需要注意的是首先要能判断题目有没有解，有解的情况下要找出最小解。</p>
<p style="FONT-SIZE: 18pt">思路依然是数组标记的方法，对于第j面值，如果第j-w[i]面值有解，则j面值肯定有解。</p>
<p style="FONT-SIZE: 18pt">这时要找最小解。要注意的是，如果j面值当前是0的话，首先为了确保有解，这时不用找最小解，直接加过去就得。</p>
<br><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;w[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">],v[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dp[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;sgn[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_135_798_Open_Image onclick="this.style.display='none'; Codehighlighter1_135_798_Open_Text.style.display='none'; Codehighlighter1_135_798_Closed_Image.style.display='inline'; Codehighlighter1_135_798_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_135_798_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_135_798_Closed_Text.style.display='none'; Codehighlighter1_135_798_Open_Image.style.display='inline'; Codehighlighter1_135_798_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_135_798_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_135_798_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;t;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">t);<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_179_783_Open_Image onclick="this.style.display='none'; Codehighlighter1_179_783_Open_Text.style.display='none'; Codehighlighter1_179_783_Closed_Image.style.display='inline'; Codehighlighter1_179_783_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_179_783_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_179_783_Closed_Text.style.display='none'; Codehighlighter1_179_783_Open_Image.style.display='inline'; Codehighlighter1_179_783_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_179_783_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_179_783_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;start,end,n,i,j;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">start,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">end,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">v[i],</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">w[i]);<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(sgn,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(sgn));<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">w[i];j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">MAX;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(sgn[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w[i]]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">dp[j]</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img id=Codehighlighter1_448_528_Open_Image onclick="this.style.display='none'; Codehighlighter1_448_528_Open_Text.style.display='none'; Codehighlighter1_448_528_Closed_Image.style.display='inline'; Codehighlighter1_448_528_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_448_528_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_448_528_Closed_Text.style.display='none'; Codehighlighter1_448_528_Open_Image.style.display='inline'; Codehighlighter1_448_528_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_448_528_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_448_528_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[j]</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">dp[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w[i]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v[i]</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">dp[j]:dp[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w[i]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v[i];<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(sgn[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w[i]]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">dp[j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img id=Codehighlighter1_571_623_Open_Image onclick="this.style.display='none'; Codehighlighter1_571_623_Open_Text.style.display='none'; Codehighlighter1_571_623_Closed_Image.style.display='inline'; Codehighlighter1_571_623_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_571_623_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_571_623_Closed_Text.style.display='none'; Codehighlighter1_571_623_Open_Image.style.display='inline'; Codehighlighter1_571_623_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_571_623_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_571_623_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w[i]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v[i];<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(dp[end</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">start]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">This&nbsp;is&nbsp;impossible.\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">The&nbsp;minimum&nbsp;amount&nbsp;of&nbsp;money&nbsp;in&nbsp;the&nbsp;piggy-bank&nbsp;is&nbsp;%d.\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,dp[end</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">start]);<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/122267.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-05 09:28 <a href="http://www.cppblog.com/Onway/articles/122267.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1014 Dividing 多重背包</title><link>http://www.cppblog.com/Onway/articles/122247.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Wed, 04 Aug 2010 15:26:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122247.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122247.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122247.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122247.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122247.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 14pt">pku&nbsp;1014&nbsp;Dividing<o:p></o:p></p>
<p style="FONT-SIZE: 14pt">这个题目跟3211&nbsp;Washing&nbsp;Clothes做法非常相似，虽然3211是0-1背包，1014是多重背包。思路都是将总价值的一半作为背包容量，然后进行背包策略就行了。<o:p></o:p></p>
<p style="FONT-SIZE: 14pt">用多重背包的个数标记，时间为O(N*M),一次AC。<o:p></o:p></p>
<p style="FONT-SIZE: 14pt">然后用二进制物品压缩，数组开小了，居然报WA，晕死。<br><br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">60000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;sgn[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnt[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;data[</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_119_807_Open_Image onclick="this.style.display='none'; Codehighlighter1_119_807_Open_Text.style.display='none'; Codehighlighter1_119_807_Closed_Image.style.display='inline'; Codehighlighter1_119_807_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_119_807_Closed_Image onclick="this.style.display='none'; Codehighlighter1_119_807_Closed_Text.style.display='none'; Codehighlighter1_119_807_Open_Image.style.display='inline'; Codehighlighter1_119_807_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_119_807_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_119_807_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cas</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">data[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">data[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">data[</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">data[</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">data[</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">])<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_203_794_Open_Image onclick="this.style.display='none'; Codehighlighter1_203_794_Open_Text.style.display='none'; Codehighlighter1_203_794_Closed_Image.style.display='inline'; Codehighlighter1_203_794_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_203_794_Closed_Image onclick="this.style.display='none'; Codehighlighter1_203_794_Closed_Text.style.display='none'; Codehighlighter1_203_794_Open_Image.style.display='inline'; Codehighlighter1_203_794_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_203_794_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_203_794_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">,sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(data[i]</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">n)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">cas;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(sgn,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(sgn));<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img id=Codehighlighter1_367_519_Open_Image onclick="this.style.display='none'; Codehighlighter1_367_519_Open_Text.style.display='none'; Codehighlighter1_367_519_Closed_Image.style.display='inline'; Codehighlighter1_367_519_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_367_519_Closed_Image onclick="this.style.display='none'; Codehighlighter1_367_519_Closed_Text.style.display='none'; Codehighlighter1_367_519_Open_Image.style.display='inline'; Codehighlighter1_367_519_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_367_519_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_367_519_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(cnt,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(cnt));<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">MAX;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">sgn[j]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">sgn[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">i]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cnt[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">i]</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">data[i])<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_470_515_Open_Image onclick="this.style.display='none'; Codehighlighter1_470_515_Open_Text.style.display='none'; Codehighlighter1_470_515_Closed_Image.style.display='inline'; Codehighlighter1_470_515_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_470_515_Closed_Image onclick="this.style.display='none'; Codehighlighter1_470_515_Closed_Text.style.display='none'; Codehighlighter1_470_515_Open_Image.style.display='inline'; Codehighlighter1_470_515_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_470_515_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_470_515_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cnt[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cnt[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">i]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">data[i]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(sum</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">||!</span><span style="COLOR: #000000">sgn[sum</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">])<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img id=Codehighlighter1_590_686_Open_Image onclick="this.style.display='none'; Codehighlighter1_590_686_Open_Text.style.display='none'; Codehighlighter1_590_686_Closed_Image.style.display='inline'; Codehighlighter1_590_686_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_590_686_Closed_Image onclick="this.style.display='none'; Codehighlighter1_590_686_Closed_Text.style.display='none'; Codehighlighter1_590_686_Open_Image.style.display='inline'; Codehighlighter1_590_686_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_590_686_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_590_686_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Collection&nbsp;#</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">cas</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Can't&nbsp;be&nbsp;divided.</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img id=Codehighlighter1_697_791_Open_Image onclick="this.style.display='none'; Codehighlighter1_697_791_Open_Text.style.display='none'; Codehighlighter1_697_791_Closed_Image.style.display='inline'; Codehighlighter1_697_791_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_697_791_Closed_Image onclick="this.style.display='none'; Codehighlighter1_697_791_Closed_Text.style.display='none'; Codehighlighter1_697_791_Open_Image.style.display='inline'; Codehighlighter1_697_791_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_697_791_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_697_791_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Collection&nbsp;#</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">cas</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Can&nbsp;be&nbsp;divided.</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<!--endfragment-->
<img src ="http://www.cppblog.com/Onway/aggbug/122247.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-04 23:26 <a href="http://www.cppblog.com/Onway/articles/122247.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 3211 Washing Clothes 0-1背包</title><link>http://www.cppblog.com/Onway/articles/122113.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Tue, 03 Aug 2010 14:23:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122113.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122113.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122113.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122113.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122113.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 18pt">前几天刚学0-1背包的时候就看过了这题，但没什么思路，今天算是学过了三种背包了，再看回这题，还是没什么思路。怎么人家都说是0-1背包呢，想了很久还是不知道怎么背包法，笨死。无奈看了discuss，豁然开朗。原来背包还可以这样玩，太有意思了。</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">题意：</p>
<p style="FONT-SIZE: 18pt">有一堆衣服，多种颜色，每种颜色的可能有多件。有两个洗衣服速度相同的人要洗干净这堆衣服。每件衣服都有一个洗干净所要花费的时间。两个人同时工作，但只有一种颜色的衣服都洗完后，两人才可以开始洗其他颜色。问这两个人洗完这堆衣服最少需要多少时间。</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">思路：将某种颜色的时间之和的一半作为0-1背包的容量，物品的价值与重量等同于该种颜色的衣服所花费的时间，这是关键思路啊！只要求得这个背包的能获得的最大价值，就是说在这一半的时间里，有多少是被最有效利用的。然后总时间减去这个有效利用的，就是洗完这堆衣服最少要花费的时间。</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">要处理这个题的输入可能比较麻烦。将输入的数据转化为要进行背包的数据后，背包部分就简单了。</p>
<p style="FONT-SIZE: 18pt">&nbsp;</p>
<p style="FONT-SIZE: 18pt">如果，这个题改一下，要分两行输出没人要洗的衣服呢？又怎么处理了？<br><br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">50000</span><span style="COLOR: #000000">;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">变成两个背包以后，一个的容量最多就是50000</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;name[</span><span style="COLOR: #000000">150</span><span style="COLOR: #000000">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">这个没什么用，就是吃掉那一行的名字输入</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum[</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">每一种颜色的时间之和</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;clother<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img id=Codehighlighter1_212_239_Open_Image onclick="this.style.display='none'; Codehighlighter1_212_239_Open_Text.style.display='none'; Codehighlighter1_212_239_Closed_Image.style.display='inline'; Codehighlighter1_212_239_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_212_239_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_212_239_Closed_Text.style.display='none'; Codehighlighter1_212_239_Open_Image.style.display='inline'; Codehighlighter1_212_239_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_212_239_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_212_239_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tim;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;color;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">clo[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;data[</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">将原来读入的数据转化后好进行0-1背包</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dp[</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">][MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">不解析</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;cmp(clother&nbsp;a,clother&nbsp;b)<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img id=Codehighlighter1_352_376_Open_Image onclick="this.style.display='none'; Codehighlighter1_352_376_Open_Text.style.display='none'; Codehighlighter1_352_376_Closed_Image.style.display='inline'; Codehighlighter1_352_376_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_352_376_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_352_376_Closed_Text.style.display='none'; Codehighlighter1_352_376_Open_Image.style.display='inline'; Codehighlighter1_352_376_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_352_376_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_352_376_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;a.color</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">b.color;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_390_1528_Open_Image onclick="this.style.display='none'; Codehighlighter1_390_1528_Open_Text.style.display='none'; Codehighlighter1_390_1528_Closed_Image.style.display='inline'; Codehighlighter1_390_1528_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_390_1528_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_390_1528_Closed_Text.style.display='none'; Codehighlighter1_390_1528_Open_Image.style.display='inline'; Codehighlighter1_390_1528_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_390_1528_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_390_1528_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;m,n;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">m</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">(n</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">m))<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img id=Codehighlighter1_429_1515_Open_Image onclick="this.style.display='none'; Codehighlighter1_429_1515_Open_Text.style.display='none'; Codehighlighter1_429_1515_Closed_Image.style.display='inline'; Codehighlighter1_429_1515_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_429_1515_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_429_1515_Closed_Text.style.display='none'; Codehighlighter1_429_1515_Open_Image.style.display='inline'; Codehighlighter1_429_1515_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_429_1515_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_429_1515_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin.getline(name,</span><span style="COLOR: #000000">150</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,k;<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">clo[i].tim</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">clo[i].color;<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(clo</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,clo</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">n,cmp);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">输入完以后马上排序</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">29</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">clo[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">].tim;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">data[j][0]是记录第j种颜色的衣服件数</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(clo[i].color</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">clo[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">].color)<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[j][k</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">clo[i].tim;<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img id=Codehighlighter1_746_816_Open_Image onclick="this.style.display='none'; Codehighlighter1_746_816_Open_Text.style.display='none'; Codehighlighter1_746_816_Closed_Image.style.display='inline'; Codehighlighter1_746_816_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_746_816_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_746_816_Closed_Text.style.display='none'; Codehighlighter1_746_816_Open_Image.style.display='inline'; Codehighlighter1_746_816_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_746_816_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_746_816_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=--</span><span style="COLOR: #000000">k;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j;<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[j][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">clo[i].tim;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=--</span><span style="COLOR: #000000">k;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">n变成了需要进行背包的颜色种数<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">这一段很容易理解，但可能处理比较麻烦，从排序后的第二个数据，即i=2开始<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">感觉这样比较好处理</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">45</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(sum,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(sum));&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">这段是数据转化后进行时间之和统计</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">47</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img id=Codehighlighter1_997_1055_Open_Image onclick="this.style.display='none'; Codehighlighter1_997_1055_Open_Text.style.display='none'; Codehighlighter1_997_1055_Closed_Image.style.display='inline'; Codehighlighter1_997_1055_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_997_1055_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_997_1055_Closed_Text.style.display='none'; Codehighlighter1_997_1055_Open_Image.style.display='inline'; Codehighlighter1_997_1055_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_997_1055_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_997_1055_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">data[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum[i]</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">data[i][j];<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">这一段就是进行0-1背包了</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">54</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">data[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">MAX;k</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">data[i][j];</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">k)<br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i][k]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">dp[i][k</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">data[i][j]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">data[i][j]</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][k]:dp[i][k</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">data[i][j]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">data[i][j];<br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">61</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum[i]</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">dp[i][sum[i]</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];&nbsp;&nbsp;<br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">摘自poj&nbsp;discuss&nbsp;TangMing&nbsp;的一句话：<br></span><span style="COLOR: #008080">63</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">把sum[i]/2的背包最大填满,第i种颜色耗时就为&nbsp;sum[i]-dp[sum[i]/2];<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">虽然这段代码很短，但是整个思路的关键</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">65</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">67</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">sum[i];<br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">sum[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">69</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">70</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">71</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/122113.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-03 22:23 <a href="http://www.cppblog.com/Onway/articles/122113.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1742 多重背包转完全背包</title><link>http://www.cppblog.com/Onway/articles/122075.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Tue, 03 Aug 2010 08:37:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122075.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122075.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122075.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122075.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122075.html</trackback:ping><description><![CDATA[<p>pku 1742 多重背包转完全背包</p>
<p>我觉得这个题目跟pku 1276 cash machine和pku 1882 stamps都有点像。<br>首先与1276 cash machine一样，都是价值等于重量的多重背包，但1276可以用二进制压缩物品转0-1背包，这个题</p>
<p>目当然也可以，但会超时。所以discuss有一帖说教主忽悠了大家。<br>与1882 stamps像，是因为是都是有张数限制，都是通过完全背包来做吧，个人觉得。只是张数限制稍有不同，一个</p>
<p>是单个物品张数，一个是所有物品张数。就因为这个，两个题一个分在多重背包，一个分在了完全背包。转完全背</p>
<p>包后，时间就可以变为O(N*M)了。</p>
<p>我是看《背包九讲》的二进制压缩物品，然后直接拿这个题开刀。超时几次后，上网找O(N*M)的算法途中，看到别</p>
<p>人的一点提示，发现跟1882可以一样做法。</p>
<p>题意：<br>有多种面值不同的硬币，每种硬币有多枚，给出一个数M，求出1到M之间，有多少个面值是可以通过这些硬币组成的</p>
<p>。</p>
<p>思路：<br>定义一个标记数组sgn[MAX+1]，将1到MAX之间的能组成的面值进行标记。一个张数统计数组num[MAX+1],表示对于第</p>
<p>i种硬币，当组成j面值时，使用的张数num[j]。<br>这里可能有一个比较绕的问题：过早的使用完了第i种硬币，会不会对第i+1种硬币进行策略的时候造成影响呢？答</p>
<p>案是会的，但正是需要这种影响。对第i+1种硬币进行策略组合的时候，正是建立在前i种硬币组成的基础上。<br>对已组合的面值j进行标记，一是因为这是后面组成面值的基础，二能保证对后面的硬币个数不会造成浪费。</p>
<p>&nbsp;</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">100000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;sgn[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnt[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a[</span><span style="COLOR: #000000">101</span><span style="COLOR: #000000">],c[</span><span style="COLOR: #000000">101</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_126_577_Open_Image onclick="this.style.display='none'; Codehighlighter1_126_577_Open_Text.style.display='none'; Codehighlighter1_126_577_Closed_Image.style.display='inline'; Codehighlighter1_126_577_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_126_577_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_126_577_Closed_Text.style.display='none'; Codehighlighter1_126_577_Open_Image.style.display='inline'; Codehighlighter1_126_577_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_126_577_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_126_577_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,m;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">m)</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">(n</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">m))<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_175_564_Open_Image onclick="this.style.display='none'; Codehighlighter1_175_564_Open_Text.style.display='none'; Codehighlighter1_175_564_Closed_Image.style.display='inline'; Codehighlighter1_175_564_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_175_564_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_175_564_Closed_Text.style.display='none'; Codehighlighter1_175_564_Open_Image.style.display='inline'; Codehighlighter1_175_564_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_175_564_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_175_564_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a[i]);<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">c[i]);<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(sgn,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(sgn));<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_330_491_Open_Image onclick="this.style.display='none'; Codehighlighter1_330_491_Open_Text.style.display='none'; Codehighlighter1_330_491_Closed_Image.style.display='inline'; Codehighlighter1_330_491_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_330_491_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_330_491_Closed_Text.style.display='none'; Codehighlighter1_330_491_Open_Image.style.display='inline'; Codehighlighter1_330_491_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_330_491_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_330_491_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(cnt,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(cnt));<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[i];j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">MAX;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">sgn[j]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">sgn[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">a[i]]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cnt[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">a[i]]</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">c[i])<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img id=Codehighlighter1_439_487_Open_Image onclick="this.style.display='none'; Codehighlighter1_439_487_Open_Text.style.display='none'; Codehighlighter1_439_487_Closed_Image.style.display='inline'; Codehighlighter1_439_487_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_439_487_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_439_487_Closed_Text.style.display='none'; Codehighlighter1_439_487_Open_Image.style.display='inline'; Codehighlighter1_439_487_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_439_487_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_439_487_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cnt[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cnt[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">a[i]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgn[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">m;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(sgn[i])<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j;<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,j);<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/122075.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-03 16:37 <a href="http://www.cppblog.com/Onway/articles/122075.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1276 价值等于重量的多重背包</title><link>http://www.cppblog.com/Onway/articles/122041.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Tue, 03 Aug 2010 02:50:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/122041.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/122041.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/122041.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/122041.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/122041.html</trackback:ping><description><![CDATA[<p>pku 1276 价值等于重量的多重背包</p>
<p>题意：<br>取款机里有多种不同面值的钞票，各种面值的钞票的张数也不同，给出取款值CASH，问最多能拿出多少钱。</p>
<p>思路：<br>将钞票看成是价值等于重量的物品，取款值为背包容量，那么这个题就是一个典型的多重背包。<br>从数据规模考虑，直接变为0-1背包求解，O(CASH*NK*N)的时间肯定超时。参考《背包九讲》中的用二进制压缩</p>
<p>物品可以将时间优化为O(CASH*N*logNK)，这样可以变成物品个数不超过100个的0-1背包。<br>然后便是0-1背包的求解了。</p>
<p>失误：<br>由于第一次学多重背包，看到二进制压缩物品时，当时以为看懂了，提交后错了N多次，才发现压缩时的最后一</p>
<p>项系数搞错了。然后处理不好，又出现负数的情况，导致一直RE。修补压缩物品部分，感觉又长又臭，遂通过研</p>
<p>究一番将物品压缩部分缩小到11行。<br><br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">math.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">100005</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;w[</span><span style="COLOR: #000000">150</span><span style="COLOR: #000000">],dp[MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;deno<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_115_137_Open_Image onclick="this.style.display='none'; Codehighlighter1_115_137_Open_Text.style.display='none'; Codehighlighter1_115_137_Closed_Image.style.display='inline'; Codehighlighter1_115_137_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_115_137_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_115_137_Closed_Text.style.display='none'; Codehighlighter1_115_137_Open_Image.style.display='inline'; Codehighlighter1_115_137_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_115_137_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_115_137_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnt;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;num;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">data[</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_159_709_Open_Image onclick="this.style.display='none'; Codehighlighter1_159_709_Open_Text.style.display='none'; Codehighlighter1_159_709_Closed_Image.style.display='inline'; Codehighlighter1_159_709_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_159_709_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_159_709_Closed_Text.style.display='none'; Codehighlighter1_159_709_Open_Image.style.display='inline'; Codehighlighter1_159_709_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_159_709_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_159_709_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,m;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">EOF)<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img id=Codehighlighter1_200_696_Open_Image onclick="this.style.display='none'; Codehighlighter1_200_696_Open_Text.style.display='none'; Codehighlighter1_200_696_Closed_Image.style.display='inline'; Codehighlighter1_200_696_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_200_696_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_200_696_Closed_Text.style.display='none'; Codehighlighter1_200_696_Open_Image.style.display='inline'; Codehighlighter1_200_696_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_200_696_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_200_696_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">m);<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,tmp;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">m;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">data[i].cnt,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">data[i].num);<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(w,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(w));<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">m;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img id=Codehighlighter1_379_561_Open_Image onclick="this.style.display='none'; Codehighlighter1_379_561_Open_Text.style.display='none'; Codehighlighter1_379_561_Closed_Image.style.display='inline'; Codehighlighter1_379_561_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_379_561_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_379_561_Closed_Text.style.display='none'; Codehighlighter1_379_561_Open_Image.style.display='inline'; Codehighlighter1_379_561_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_379_561_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_379_561_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(data[i].cnt</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">pow(</span><span style="COLOR: #000000">2.0</span><span style="COLOR: #000000">,j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_430_485_Open_Image onclick="this.style.display='none'; Codehighlighter1_430_485_Open_Text.style.display='none'; Codehighlighter1_430_485_Closed_Image.style.display='inline'; Codehighlighter1_430_485_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_430_485_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_430_485_Closed_Text.style.display='none'; Codehighlighter1_430_485_Open_Image.style.display='inline'; Codehighlighter1_430_485_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_430_485_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_430_485_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">pow(</span><span style="COLOR: #000000">2.0</span><span style="COLOR: #000000">,j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w[k</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">data[i].num;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">data[i].cnt</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">pow(</span><span style="COLOR: #000000">2.0</span><span style="COLOR: #000000">,j);<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp)<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w[k</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">data[i].num;<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">k;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">k;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">MAX;j</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">w[i];</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[j]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">dp[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w[i]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">w[i]</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">dp[j]:dp[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w[i]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">w[i];<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,dp[n]);<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/122041.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-03 10:50 <a href="http://www.cppblog.com/Onway/articles/122041.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1882 完全背包？</title><link>http://www.cppblog.com/Onway/articles/121967.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Mon, 02 Aug 2010 07:24:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/121967.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/121967.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/121967.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/121967.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/121967.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 18pt">pku 1882</p>
<p style="FONT-SIZE: 18pt">题意：<br>给出多组由不同面值组成的邮票，求出由给出的那些面值能组成连续面额值最大的一组。（具体见原题，做完后发</p>
<p style="FONT-SIZE: 18pt">现是95年的finals,呵呵）</p>
<p style="FONT-SIZE: 18pt">刚看完背包九讲的第二讲完全背包，百度一下pku 完全背包，看到有说pku1882是一完全背包。按照完全背包去想，</p>
<p style="FONT-SIZE: 18pt">写出的代码调了很久，然后越调发现越不像完全背包。完全背包里物品都有一个价值，物品个数不限，而在这个题</p>
<p style="FONT-SIZE: 18pt">目里，都不满足这两个条件，可能我太水吧，但我真的不知道怎么能将这个题按完全背包去想。如果说有相似的话</p>
<p style="FONT-SIZE: 18pt">，我觉得就是在状态设计和递推顺序与完全背包是很相似的，可能吧，就是这个原因，就可以将这个DP分在完全背</p>
<p style="FONT-SIZE: 18pt">包一类中。</p>
<p style="FONT-SIZE: 18pt">思路：<br>假设邮票没有张数限制，那么就很像完全背包了(ft!!)。然后对1到1000（最多是(91+92+&#8230;&#8230;100)*10）的各种面额</p>
<p style="FONT-SIZE: 18pt">进行枚举。同时记录每一种面额的最小张数。最后做一次统计，张数为0或是大于题目要求的都是不符合的面额。<br>枚举的过程是很像完全背包是挺相似的说。</p>
<p><span style="FONT-SIZE: 18pt">题目的测试数据里有一些毛病，在每一组邮票中，面值为1的邮票是一定出现了的。</span><br><br></p>
<div style="FONT-SIZE: 14pt">&nbsp;<span style="FONT-SIZE: 12pt">1<img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;&lt;iostream&gt;<br>&nbsp;2<img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>using&nbsp;namespace&nbsp;std;<br>&nbsp;3<img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>const&nbsp;int&nbsp;MAX=961;<br>&nbsp;4<img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>int&nbsp;dp[12][MAX];<br>&nbsp;5<img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>int&nbsp;data[12][12];<br>&nbsp;6<img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>int&nbsp;fmin(int&nbsp;a,int&nbsp;b)<br>&nbsp;7<img id=Codehighlighter1_117_133_Open_Image onclick="this.style.display='none'; Codehighlighter1_117_133_Open_Text.style.display='none'; Codehighlighter1_117_133_Closed_Image.style.display='inline'; Codehighlighter1_117_133_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_117_133_Closed_Image onclick="this.style.display='none'; Codehighlighter1_117_133_Closed_Text.style.display='none'; Codehighlighter1_117_133_Open_Image.style.display='inline'; Codehighlighter1_117_133_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top><img src="http://www.cppblog.com/Images/dot.gif">{return&nbsp;a&lt;b?a:b;}<br>&nbsp;8<img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>int&nbsp;main()<br>&nbsp;9<img id=Codehighlighter1_146_1253_Open_Image onclick="this.style.display='none'; Codehighlighter1_146_1253_Open_Text.style.display='none'; Codehighlighter1_146_1253_Closed_Image.style.display='inline'; Codehighlighter1_146_1253_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_146_1253_Closed_Image onclick="this.style.display='none'; Codehighlighter1_146_1253_Closed_Text.style.display='none'; Codehighlighter1_146_1253_Open_Image.style.display='inline'; Codehighlighter1_146_1253_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top><img src="http://www.cppblog.com/Images/dot.gif">{<br>10<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;s;<br>11<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;while(scanf("%d",&amp;s)&amp;&amp;s)<br>12<img id=Codehighlighter1_183_1240_Open_Image onclick="this.style.display='none'; Codehighlighter1_183_1240_Open_Text.style.display='none'; Codehighlighter1_183_1240_Closed_Image.style.display='inline'; Codehighlighter1_183_1240_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_183_1240_Closed_Image onclick="this.style.display='none'; Codehighlighter1_183_1240_Closed_Text.style.display='none'; Codehighlighter1_183_1240_Open_Image.style.display='inline'; Codehighlighter1_183_1240_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{<br>13<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,0,sizeof(dp));<br>14<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;n,i,j,k;<br>15<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&amp;n);<br>16<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=n;++i)<br>17<img id=Codehighlighter1_267_393_Open_Image onclick="this.style.display='none'; Codehighlighter1_267_393_Open_Text.style.display='none'; Codehighlighter1_267_393_Closed_Image.style.display='inline'; Codehighlighter1_267_393_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_267_393_Closed_Image onclick="this.style.display='none'; Codehighlighter1_267_393_Closed_Text.style.display='none'; Codehighlighter1_267_393_Open_Image.style.display='inline'; Codehighlighter1_267_393_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{<br>18<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&amp;data[i][0]);<br>19<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=1;j&lt;=data[i][0];++j)<br>20<img id=Codehighlighter1_330_389_Open_Image onclick="this.style.display='none'; Codehighlighter1_330_389_Open_Text.style.display='none'; Codehighlighter1_330_389_Closed_Image.style.display='inline'; Codehighlighter1_330_389_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_330_389_Closed_Image onclick="this.style.display='none'; Codehighlighter1_330_389_Closed_Text.style.display='none'; Codehighlighter1_330_389_Open_Image.style.display='inline'; Codehighlighter1_330_389_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{<br>21<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&amp;data[i][j]);<br>22<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][data[i][j]]=1;<br>23<img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>24<img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>25<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>26<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=n;++i)<br>27<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=1;j&lt;=data[i][0];++j)<br>28<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(k=data[i][j]+1;k&lt;=MAX-1;++k)<br>29<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(dp[i][k-data[i][j]]==0)<br>30<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][k]=0;<br>31<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if(dp[i][k]==0)<br>32<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][k]=dp[i][k-data[i][j]]+1;<br>33<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>34<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][k]=fmin(dp[i][k],dp[i][k-data[i][j]]+1);<br>35<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>36<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=n;++i)<br>37<img id=Codehighlighter1_687_790_Open_Image onclick="this.style.display='none'; Codehighlighter1_687_790_Open_Text.style.display='none'; Codehighlighter1_687_790_Closed_Image.style.display='inline'; Codehighlighter1_687_790_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_687_790_Closed_Image onclick="this.style.display='none'; Codehighlighter1_687_790_Closed_Text.style.display='none'; Codehighlighter1_687_790_Open_Image.style.display='inline'; Codehighlighter1_687_790_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{<br>38<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=1;j&lt;=MAX-1;++j)<br>39<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(dp[i][j]==0||dp[i][j]&gt;s)<br>40<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>41<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[i][data[i][0]+1]=j-1;<br>42<img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>43<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br>44<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k=1;<br>45<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=2;i&lt;=n;++i)<br>46<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(data[i][data[i][0]+1]&gt;data[k][data[k][0]+1])<br>47<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k=i;<br>48<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if(data[k][data[k][0]+1]==data[i][data[i][0]+1]&amp;&amp;data[i][0]&lt;data[k][0])<br>49<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k=i;<br>50<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if(data[k][data[k][0]+1]==data[i][data[i][0]+1]&amp;&amp;data[k][0]==data[i][0]<br>51<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;data[i][data[i][0]]&lt;data[k][data[k][0]])<br>52<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k=i;<br>53<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<br>54<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("max&nbsp;coverage&nbsp;=&nbsp;%d&nbsp;:&nbsp;",data[k][data[k][0]+1]);<br>55<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=data[k][0];++i)<br>56<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d&nbsp;",data[k][i]);<br>57<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br>58<img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br>59<img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;<br>60<img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}<br>61<img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<br>今天看了二维费用的背包才知道，原来这个题是属于二维费用背包的。当然二维费用的背包，也是基于0-1背包或是完全背包的。<br>用二维费用的方法再写了一次这个题，时间和内存都没有上面那个好。<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 12pt; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;d[</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">1002</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;test[</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">13</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;fmax(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;b)<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img id=Codehighlighter1_102_118_Open_Image onclick="this.style.display='none'; Codehighlighter1_102_118_Open_Text.style.display='none'; Codehighlighter1_102_118_Closed_Image.style.display='inline'; Codehighlighter1_102_118_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_102_118_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_102_118_Closed_Text.style.display='none'; Codehighlighter1_102_118_Open_Image.style.display='inline'; Codehighlighter1_102_118_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_102_118_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_102_118_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;a</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">a:b;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_131_1084_Open_Image onclick="this.style.display='none'; Codehighlighter1_131_1084_Open_Text.style.display='none'; Codehighlighter1_131_1084_Closed_Image.style.display='inline'; Codehighlighter1_131_1084_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_131_1084_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_131_1084_Closed_Text.style.display='none'; Codehighlighter1_131_1084_Open_Image.style.display='inline'; Codehighlighter1_131_1084_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_131_1084_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_131_1084_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;s,n;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">s)</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">s)<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_170_1071_Open_Image onclick="this.style.display='none'; Codehighlighter1_170_1071_Open_Text.style.display='none'; Codehighlighter1_170_1071_Closed_Image.style.display='inline'; Codehighlighter1_170_1071_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_170_1071_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_170_1071_Closed_Text.style.display='none'; Codehighlighter1_170_1071_Open_Image.style.display='inline'; Codehighlighter1_170_1071_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_170_1071_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_170_1071_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,k,l;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img id=Codehighlighter1_227_314_Open_Image onclick="this.style.display='none'; Codehighlighter1_227_314_Open_Text.style.display='none'; Codehighlighter1_227_314_Closed_Image.style.display='inline'; Codehighlighter1_227_314_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_227_314_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_227_314_Closed_Text.style.display='none'; Codehighlighter1_227_314_Open_Image.style.display='inline'; Codehighlighter1_227_314_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_227_314_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_227_314_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">test[i][j]);<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(d,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(d));<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img id=Codehighlighter1_364_613_Open_Image onclick="this.style.display='none'; Codehighlighter1_364_613_Open_Text.style.display='none'; Codehighlighter1_364_613_Closed_Image.style.display='inline'; Codehighlighter1_364_613_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_364_613_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_364_613_Closed_Text.style.display='none'; Codehighlighter1_364_613_Open_Image.style.display='inline'; Codehighlighter1_364_613_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_364_613_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_364_613_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;k</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">s;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">k)<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(l</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">test[i][j];l</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">l)<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[i][k][l]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">fmax(d[i][k][l],d[i][k</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][l</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">test[i][j]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">test[i][j]);<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(d[i][s][j]</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test[i][test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(test[i][test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">test[k][test[k][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(test[i][test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">test[k][test[k][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">test[k][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">])<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(test[i][test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">test[k][test[k][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">test[k][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">test[i][test[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]]</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">test[k][test[k][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]])<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">max&nbsp;coverage&nbsp;=&nbsp;%d&nbsp;:&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test[k][test[k][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">test[k][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test[k][i]);<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/121967.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-08-02 15:24 <a href="http://www.cppblog.com/Onway/articles/121967.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku2063w可变的完全背包</title><link>http://www.cppblog.com/Onway/articles/121707.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Fri, 30 Jul 2010 09:01:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/121707.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/121707.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/121707.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/121707.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/121707.html</trackback:ping><description><![CDATA[<p>pku 2063 容量可变的完全背包</p>
<p>题意：<br>某人有一大笔资金s(s&lt;1000000)，想通过购买债券获利。现有n(n&lt;=10)种债券可买。已知每种债券购买金额w(1000的倍数)和一年后的获利v(不多于购买金额的10%)。可以将每年得到的所有获利都再次投入。问t(t&lt;=40)年后，该人共有多少钱。</p>
<p>看懂题目的第一感觉是没思路，在网上找别的完全背包，找不到了，只能去想这个题。</p>
<p>当时的第一思路是每年获利后都做一次完全背包。至于状态压缩，是将原有资金和购买金额都除以1000，获利不变。每次背包后，都将最大获利除以1000，加到原有资金里（用double型）。<br>过了样例，提交返回了多次RE。看discuss说数组开小了。为了验证的却是数组越界的错误，一下狠心数组开大了两个数量级。<br>这下不是RE了，是WA。还是PKU比HDU厚道一点，数组访问越界是报RE，不是WA。</p>
<p>我觉得我是完全看懂了题意，是跟着题意写的代码，而且一次过了样例，这说明思路没错啊。在这种情况下，是可以少测别的数据吧，最多就是测一下边界数组。而直觉告诉我，边界是没问题的。</p>
<p>我怀疑状态压缩出乱子了（没试过压缩的），就干脆不压了，每次RE后都加大数组，一直到MLE。然后编译选错，又来了几个CE。盼星星盼月亮就是盼不到一个AC。<br>上网查看别人的代码，发现思路跟我的好像不一样。而且都不像我那样，用到double型。我也去掉double类型，将获利先加到总金额，在除以1000赋值给整形。这下提交终于看到AC了。</p>
<p>虽然AC了，但还是有三个地方是不懂得。</p>
<p>1，为什么数组要开到46000呢。<br>假设将所有资金都能获得最大获利，那么40年后，就是：<br>1 000 000*(1.1^40)=45259255.568175951805889356034897<br>2,为什么我用double的时候会出错呢？<br>其实现在还是不懂，估计也就是有精度损失。<br>3，别人的思路为什么只求一次完全背包，就的了呢？<br>由于可以知道40年后的最大金额，不超过46000（压缩后），一次背包后，就能得出每一金额的最大获利。然后每一年获利后都加进去，直接提取出来就可以再次求获利。<br><br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">46000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dp[MAX],w[</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">],v[</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;fmax(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;b)<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_130_146_Open_Image onclick="this.style.display='none'; Codehighlighter1_130_146_Open_Text.style.display='none'; Codehighlighter1_130_146_Closed_Image.style.display='inline'; Codehighlighter1_130_146_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_130_146_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_130_146_Closed_Text.style.display='none'; Codehighlighter1_130_146_Open_Image.style.display='inline'; Codehighlighter1_130_146_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_130_146_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_130_146_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;a</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">a:b;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img id=Codehighlighter1_159_566_Open_Image onclick="this.style.display='none'; Codehighlighter1_159_566_Open_Text.style.display='none'; Codehighlighter1_159_566_Closed_Image.style.display='inline'; Codehighlighter1_159_566_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_159_566_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_159_566_Closed_Text.style.display='none'; Codehighlighter1_159_566_Open_Image.style.display='inline'; Codehighlighter1_159_566_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_159_566_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_159_566_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;t;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">t);<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_199_553_Open_Image onclick="this.style.display='none'; Codehighlighter1_199_553_Open_Text.style.display='none'; Codehighlighter1_199_553_Closed_Image.style.display='inline'; Codehighlighter1_199_553_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_199_553_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_199_553_Closed_Text.style.display='none'; Codehighlighter1_199_553_Open_Image.style.display='inline'; Codehighlighter1_199_553_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_199_553_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_199_553_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;money,year,bond,i,j,sum;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">money,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">year,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">bond);<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">bond;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img id=Codehighlighter1_295_344_Open_Image onclick="this.style.display='none'; Codehighlighter1_295_344_Open_Text.style.display='none'; Codehighlighter1_295_344_Closed_Image.style.display='inline'; Codehighlighter1_295_344_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_295_344_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_295_344_Closed_Text.style.display='none'; Codehighlighter1_295_344_Open_Image.style.display='inline'; Codehighlighter1_295_344_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_295_344_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_295_344_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">w[i],</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">v[i]);<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w[i]</span><span style="COLOR: #000000">/=</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">bond;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">w[i];j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">MAX</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">fmax(dp[j],dp[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w[i]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v[i]);<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(year</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_483_525_Open_Image onclick="this.style.display='none'; Codehighlighter1_483_525_Open_Text.style.display='none'; Codehighlighter1_483_525_Closed_Image.style.display='inline'; Codehighlighter1_483_525_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_483_525_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_483_525_Closed_Text.style.display='none'; Codehighlighter1_483_525_Open_Image.style.display='inline'; Codehighlighter1_483_525_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_483_525_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_483_525_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">money</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;money</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">dp[sum];<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,money);<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/121707.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-07-30 17:01 <a href="http://www.cppblog.com/Onway/articles/121707.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1221   UNIMODAL PALINDROMIC DECOMPOSITIONS</title><link>http://www.cppblog.com/Onway/articles/121550.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Thu, 29 Jul 2010 03:07:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/121550.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/121550.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/121550.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/121550.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/121550.html</trackback:ping><description><![CDATA[<p>pku 1221&nbsp;&nbsp; UNIMODAL PALINDROMIC DECOMPOSITIONS<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1221">http://acm.pku.edu.cn/JudgeOnline/problem?id=1221</a><br>题意：给定一个n，求串里元素之和为n的数字回文串的个数。</p>
<p>这个题目想了我很久，看着所有讨论都说是简单题，想死的心都有。<br>最后自己还是想不出来，看了人家的DP状态设计才写出来了。</p>
<p>刚开始，总是往一维DP（其实不算DP吧，都没有状态转移的，只是简单的递推而已）里想，<br>还想出了其他一些结论，只是还不至于能解出这个题，说白了就是，还是没想到。<br>以下的内容都是参考了网上已经摆放了N久的题解，加上了自己的理解而已。</p>
<p>假设dp[i][j]为：将i这一个数拆分为串里元素均不少于j的回文串的总个数。<br>对于这种状态设计的理解很重要，至少要理解里面的两个意思：<br>1，串里元素均不少于j，也就是这些回文串最外面的两个数至少为j。<br>2，当j=1时，dp[i][j]即表示，元素之和为i的回文串的总个数，因为元素至少都要为1。<br>&nbsp;很明显，dp[i][1]包含了dp[i][2]，dp[i][2]又包含了dp[i][3]&#8230;&#8230;。</p>
<p><br>然后对于dp[i][j]，再分两层理解来设计转移方程：<br>1，当最外面的两个元素为j的时候，这两个元素之间的其他元素之和就为i-2*j。<br>&nbsp;dp[i-2*j][j]里的所有个数只要往两边加上j就变成了dp[i][j]的一部分解。<br>2，当最外面的两个元素大于j的时候，只要将dp[i][j]加上dp[i][j+1]即可。<br>&nbsp;因为dp[i][j+1]包含了dp[i][j+2]。</p>
<p>所以DP方程可以设计为下：</p>
<p>dp[i][j]=dp[i][j+1]+dp[i-2*j][j];</p>
<p>然后是处理这个方程的边界条件。j&lt;=i,当j==i的时候，dp[i][j]==1；</p>
<p>当i-2*j&lt;0的时候，即代表i不能拆分，应直接加0<br>当i-2*j==0的时候，这时该这么理解，i可以拆分为最外两个元素为j，中间元素为0<br>即不存在中间元素，这时的回文串只有一个，即(j,j)。所以dp[0][j]应初始化为1<br><br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>__int64&nbsp;dp[</span><span style="COLOR: #000000">301</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">301</span><span style="COLOR: #000000">];&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">假设了数组最大的输入为300。</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img id=Codehighlighter1_93_397_Open_Image style="DISPLAY: inline" onclick="this.style.display='none'; Codehighlighter1_93_397_Open_Text.style.display='none'; Codehighlighter1_93_397_Closed_Image.style.display='inline'; Codehighlighter1_93_397_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_93_397_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_93_397_Closed_Text.style.display='none'; Codehighlighter1_93_397_Open_Image.style.display='inline'; Codehighlighter1_93_397_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_93_397_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_93_397_Open_Text style="DISPLAY: inline"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">300</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">初始化，详细见上。</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #008000"><img id=Codehighlighter1_140_175_Open_Image onclick="this.style.display='none'; Codehighlighter1_140_175_Open_Text.style.display='none'; Codehighlighter1_140_175_Closed_Image.style.display='inline'; Codehighlighter1_140_175_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_140_175_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_140_175_Closed_Text.style.display='none'; Codehighlighter1_140_175_Open_Image.style.display='inline'; Codehighlighter1_140_175_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_140_175_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_140_175_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">300</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">j)<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img id=Codehighlighter1_222_303_Open_Image onclick="this.style.display='none'; Codehighlighter1_222_303_Open_Text.style.display='none'; Codehighlighter1_222_303_Closed_Image.style.display='inline'; Codehighlighter1_222_303_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_222_303_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_222_303_Closed_Text.style.display='none'; Codehighlighter1_222_303_Open_Image.style.display='inline'; Codehighlighter1_222_303_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_222_303_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_222_303_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i][j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">dp[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">j][j]:</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">如果不对i-2*j进行判断，会导致数组访问下溢</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n;<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">EOF</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">n)<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img id=Codehighlighter1_345_384_Open_Image onclick="this.style.display='none'; Codehighlighter1_345_384_Open_Text.style.display='none'; Codehighlighter1_345_384_Closed_Image.style.display='inline'; Codehighlighter1_345_384_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_345_384_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_345_384_Closed_Text.style.display='none'; Codehighlighter1_345_384_Open_Image.style.display='inline'; Codehighlighter1_345_384_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_345_384_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_345_384_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d&nbsp;%I64d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,n,dp[n][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<img src ="http://www.cppblog.com/Onway/aggbug/121550.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-07-29 11:07 <a href="http://www.cppblog.com/Onway/articles/121550.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hdu 1176 免费馅饼</title><link>http://www.cppblog.com/Onway/articles/121549.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Thu, 29 Jul 2010 03:04:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/121549.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/121549.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/121549.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/121549.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/121549.html</trackback:ping><description><![CDATA[<p><br>hdu 1176<br>免费馅饼，类似数塔找路径求最大值的一道简单DP，N久前在PKU做过，似乎（馅饼做过，数塔也做过）。今天在HDU看到，并不是马上有思路，也没有去回忆做过的，想了好一会才找到一个思路，扫了一眼题解（从低向上）。<br>写的第一次，用结构体，用快排。自己出数据，Debug出两个错误，两次提交都是WA。越改感觉越复杂。然后厕所途中又发现了一个错误，导致发现思路都是根本性错误的。<br>回来再改一下，居然连样例都没调过，又要Debug，很快发现改漏一个细节。<br>过了几组数据，提交还是WA，吐血了。</p>
<p>看来看去，就剩下快排是可以省掉的，但无大碍。无奈上网找答案，看到一代码跟我的省掉快排后就几乎是一模一样了。这下百分百确定思路没错了，只能喊着命苦继续Debug。</p>
<p>最后居然发现是数组开小了。<br>思路：先初始化数组，然后在时间上从低往上，每个时间点都进行一次更新。<br>错误的就是，直接从时间上操作数组，更新时间也没有对每个时间点进行。</p>
<img src ="http://www.cppblog.com/Onway/aggbug/121549.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-07-29 11:04 <a href="http://www.cppblog.com/Onway/articles/121549.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1006</title><link>http://www.cppblog.com/Onway/articles/121547.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Thu, 29 Jul 2010 03:02:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/121547.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/121547.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/121547.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/121547.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/121547.html</trackback:ping><description><![CDATA[<p>pku1006<br>中国剩余定理</p>
<p>题意：每个人都有三个天赋，分别是体能，智商，情商。这三个天赋都有一个最高点。每个天赋的最高点都有出现的周期，分别是23,28,33天。假设三个天赋在某一年的第p,i,e三天分别出现了。问，从该年的第n天开始，还要经过多少天，这三个天赋会在同一天出现。</p>
<p>中国剩余定理（中国古代求解一次同余式组的方法）</p>
<p>引出：<br>公元前后的《孙子算经》中有&#8220;物不知数&#8221;问题：&#8220;今有物不知其数，三三数之余二 ，五五数之余三 ，七七数之余二，问物几何？&#8221;答为&#8220;23&#8221;</p>
<p>方法：<br>设总数n,三个除数分别为a,b,c，三个余数依次为p,i,e，即：<br>n%a==p;<br>n%b==i;<br>n%c==e;</p>
<p>令x满足x%b==0&amp;&amp;x%c==0&amp;&amp;x%a==1,y满足y%a==0,y%c==0,y%b==1,z满足z%a==0,z%b==0,z%c==1;</p>
<p>则n=x*p+y*i+z*e分别除以a,b,c，余数必分别为p,i,e.</p>
<p>当然n不一定是满足条件的最小数。通过将n对a*b*c求余即可得到满足条件的最小数。</p>
<p>当时对这个方法了解不清，半懂不懂得调试了很久。明白了这个定理以后，其实就水题一道。</p>
<p>这个题目要注意的地方是三个天赋一起出现的那一天可能出现在给出的日期前面。这样的话，只要将出现的那一天加上一个&#8220;大周期&#8221;即为第二次出现的天数。<br></p>
<img src ="http://www.cppblog.com/Onway/aggbug/121547.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-07-29 11:02 <a href="http://www.cppblog.com/Onway/articles/121547.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1528</title><link>http://www.cppblog.com/Onway/articles/117955.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Tue, 15 Jun 2010 03:01:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/117955.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/117955.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/117955.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/117955.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/117955.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 14pt"><strong>以我的英文水平来说题目还是挺长的，而且那个样例输出看起来也复杂</strong></p>
<p style="FONT-SIZE: 14pt"><strong>得很。以致我一直没去碰这个题，虽然我早知道这是个水题。昨天去做</strong></p>
<p style="FONT-SIZE: 14pt"><strong>是因为那个贪心的题目想了很久也没思路，也快够钟熄灯睡觉了。所以</strong></p>
<p style="FONT-SIZE: 14pt"><strong>只能以一个水题完成任务。<br>看题肯定要借助有道词典，不然真的看不下去。看懂后发现果真是水题</strong></p>
<p style="FONT-SIZE: 14pt"><strong>。<br>题意如下：输入给出多个正整数，以0结束输入。对每个数，判断它是 </strong></p>
<p style="FONT-SIZE: 14pt"><strong>perfect，还是abundant，deficient。<br>思路：找到每个数小于自身的所有因数，将他们相加，如果等于原来的</strong></p>
<p style="FONT-SIZE: 14pt"><strong>数，就是perfect，大于则是abundant，小于则是deficient。<br>注意的地方：&#8220;1&#8221;是deficient，因为是要小于自身的因数。（WA一次</strong></p>
<p style="FONT-SIZE: 14pt"><strong>）<br>&nbsp;用试除法找因数，时间复杂度是O(N^1/2)。<br>&nbsp;注意输出是设置宽度。</strong></p>
<img src ="http://www.cppblog.com/Onway/aggbug/117955.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-06-15 11:01 <a href="http://www.cppblog.com/Onway/articles/117955.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku3518</title><link>http://www.cppblog.com/Onway/articles/117950.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Tue, 15 Jun 2010 02:27:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/117950.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/117950.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/117950.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/117950.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/117950.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 14pt"><strong>今天完成了昨天剩下的一个水题。关于素数判断的，用C写三次CE后还是老老实实的用C++,但居然返回一个TLE。后来想想真的应得TLE，只能怪自己。总以为素数判断用试除法的复杂度是O(N/2)，做了一些优化后4000+MS才过，但发现原来可以O(N^1/2)，改了后马上回到16MS，也太强大了。<br>然后再看了一个DP。当时推了一下规律，推不出，觉得非常像数学题，分析过题目发现可以暴力打表，想想反正没试过自己打表。便让电脑跑了几百亿步，费时接近五十分钟才出了结果。<br>发现程序运行的那段时间，CPU的占用率一直在33%左右，原来三核的CPU只用了一个核心运行，途中想研究一下多线程怎么个写法，剩两个核似乎有点浪费，但上网查了一下，太复杂了，就算了。<br>然后上泳课的路上回想discuss里的人都说是DP，再想便发现了一点规律，回来很快就推出DP方程了。还是用DP踏实一点。<br>用暴力费时四十多分钟，用算法0秒。&#8220;提倡和谐，拒绝暴力&#8221;。</strong></span>
<img src ="http://www.cppblog.com/Onway/aggbug/117950.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-06-15 10:27 <a href="http://www.cppblog.com/Onway/articles/117950.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1088</title><link>http://www.cppblog.com/Onway/articles/117949.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Tue, 15 Jun 2010 02:25:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/117949.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/117949.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/117949.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/117949.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/117949.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 14pt"><strong>pku1088说是DP，想了很久，觉得更像DFS，但看一下数据规模，肯定超时。想用BFS进行记录，没思路。纠结了一个小时，看discuss，说是递归加记忆化搜索。大概看了一下代码，那个递归就是DFS，然后自己写。超时。后来发现在一个很微小的地方导致超时了。原来忽略的地方就是记忆化搜索的重点。</strong></span> 
<img src ="http://www.cppblog.com/Onway/aggbug/117949.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-06-15 10:25 <a href="http://www.cppblog.com/Onway/articles/117949.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1163</title><link>http://www.cppblog.com/Onway/articles/117945.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Tue, 15 Jun 2010 02:08:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/117945.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/117945.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/117945.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/117945.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/117945.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 14pt"><strong>一看题目以为是DFS加回溯，写完提交，CE，原来在C++里不能自定义一个max的函数，但可以定义一个max的变量。<br>改过后，再交，TLE，总是不会算搜索的时间复杂度。<br>再想想用DP的方法也可以。用c++加个fmax函数从上到下记录跑了32MS，256K的内存。<br>看discuss人家跑了0MS，我用C改一下，CE一次。原来所有的变量都要定义在main函数的开头，这次跑了0MS，164K的内存。<br>再试将二维数组定义为全局变量，时间不变，多跑了4K的内存。</strong></p>
<p style="FONT-SIZE: 14pt"><strong>C的优势真的很明显。有空得研究一下C的语法。</strong></p>
<img src ="http://www.cppblog.com/Onway/aggbug/117945.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-06-15 10:08 <a href="http://www.cppblog.com/Onway/articles/117945.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>并查集二题</title><link>http://www.cppblog.com/Onway/articles/110570.html</link><dc:creator>Onway</dc:creator><author>Onway</author><pubDate>Fri, 26 Mar 2010 01:40:00 GMT</pubDate><guid>http://www.cppblog.com/Onway/articles/110570.html</guid><wfw:comment>http://www.cppblog.com/Onway/comments/110570.html</wfw:comment><comments>http://www.cppblog.com/Onway/articles/110570.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Onway/comments/commentRss/110570.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Onway/services/trackbacks/110570.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 18pt"><strong>PKU1308<br>好像是星期二晚看的题，一个小时写出来，错了，看了discuss，有些数据确实不能过，自己也没考虑到。也够钟熄灯睡觉了，只能搁下这个题了。星期三，上课。星期四，去医院，回来没啥心思，有心思那个时候断网了。<br>一直留到今天星期五的早上。其实这个题真的没啥好说的，算法和数据结构都没啥特别，就是要考虑很周全，对着这个题目，要问很多个&#8220;这样可不可以呢&#8221;，漏问一个可能就错了，问了还得记下来，不然写的时候可能又漏。<br>写的时候及之前，我就是这么做的，写的过程很吃力，结果还是漏掉了很多情况，而郁闷的是我考虑的情况都不是题目要考虑的。特别的是我漏的情况都是属于边界数据之类的，在出这些数据的能力还是很差。<br>还要提醒自己的是，很多情况分析后一定要看看有没有同属的情况，不然真的写得很累。</strong></p>
<p style="FONT-SIZE: 18pt"><strong>&nbsp;</strong></p>
<p style="FONT-SIZE: 18pt"><strong>PKU1861<br>在PKU1861里，循环下标从一开始，但我用了vector的push_back，sort，都没有注意这个问题，在试调花了不少时间，还导致了一个Validator Error，当然将相同代码再提交就WA了，不知道怎么回事。<br><br><br>暂结：<br>1.可以只用一个关系数组来作辅助<br>2.关系中的合并与查找可以写得不那么分明，毕竟也只有几个语句。当然分两个函数来写，思路都结构都会比较清晰。<br>3.判断两个对象所在集合不能直接用关系数组进行一次对比，即不能通过比较父亲，而是要循环到集合顶点，即祖先。<br>4.同样，合并两个对象，也要找到集合顶点。<br>5.并查集还有路径压缩的东西。</strong></p>
<p><strong>ps:在杭电找了几个说是并查集的题，看了两个，不会做。感觉有点难，看起来也不像并查集。还是先这样吧，校赛将近，还有别的要复习准备，不能一个算法吊死。</strong><br></p>
<img src ="http://www.cppblog.com/Onway/aggbug/110570.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Onway/" target="_blank">Onway</a> 2010-03-26 09:40 <a href="http://www.cppblog.com/Onway/articles/110570.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>