﻿<?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++博客-zhangzm</title><link>http://www.cppblog.com/zhangzm/</link><description /><language>zh-cn</language><lastBuildDate>Thu, 09 Apr 2026 10:42:46 GMT</lastBuildDate><pubDate>Thu, 09 Apr 2026 10:42:46 GMT</pubDate><ttl>60</ttl><item><title>程序员面试100题_01 把二元查找树转变成排序的双向链表</title><link>http://www.cppblog.com/zhangzm/archive/2011/05/31/147764.html</link><dc:creator>bluedream</dc:creator><author>bluedream</author><pubDate>Tue, 31 May 2011 09:12:00 GMT</pubDate><guid>http://www.cppblog.com/zhangzm/archive/2011/05/31/147764.html</guid><wfw:comment>http://www.cppblog.com/zhangzm/comments/147764.html</wfw:comment><comments>http://www.cppblog.com/zhangzm/archive/2011/05/31/147764.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhangzm/comments/commentRss/147764.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhangzm/services/trackbacks/147764.html</trackback:ping><description><![CDATA[<p style="text-indent:21.0pt;"><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";"Times New Roman"'>题目：输入一棵二元查找树，将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点，只调整指针的指向。</span></p>
<p><span>&nbsp;&nbsp; &nbsp;10</span></p>
<p>&nbsp;<span>&nbsp;&nbsp;/ \</span></p>
<p>&nbsp;6&nbsp;&nbsp;14</p>
<p>&nbsp;/ \ <span>&nbsp;&nbsp;&nbsp;/ \</span></p>
<p><span>4&nbsp;8
12 16</span></p>
<p>&nbsp;<span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";"Times New Roman"'>转换成双向链表</span></p>
<p>4=6=8=10=12=14=16<span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";"Times New Roman"'>。</span></p>
<p>&nbsp;</p>
<p>&nbsp;<span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";"Times New Roman"'>首先我们定义的二元查找树</span> <span style='font-family:宋体;"Times New Roman";mso-hansi-font-family:"Times New Roman"'>节点的数据结构如下：</span></p>
<p>struct BTNode{<br />
&nbsp; &nbsp; int value;<br />
&nbsp; &nbsp; BTNode *left;<br />
&nbsp; &nbsp; BTNode *right;<br />
};</p>
<p>
typedef BTNode BTree;
<br />
</p>
<p>
<div style="font-size: 13px; border-top-color: #cccccc; border-left-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-top-width: 1px; border-left-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-left-style: solid; border-right-style: solid; border-bottom-style: solid; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; background-color: #eeeeee"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080; ">&nbsp;&nbsp;1</span>&nbsp;<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;&nbsp;2</span>&nbsp;<span style="color: #000000; ">#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">cassert</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />
</span><span style="color: #008080; ">&nbsp;&nbsp;3</span>&nbsp;<span style="color: #000000; ">#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stack</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />
</span><span style="color: #008080; ">&nbsp;&nbsp;4</span>&nbsp;<span style="color: #000000; "></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;&nbsp;5</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">&nbsp;&nbsp;6</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">struct</span><span style="color: #000000; ">&nbsp;BTNode{<br />
</span><span style="color: #008080; ">&nbsp;&nbsp;7</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;value;<br />
</span><span style="color: #008080; ">&nbsp;&nbsp;8</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTNode&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">left;<br />
</span><span style="color: #008080; ">&nbsp;&nbsp;9</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTNode&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">right;<br />
</span><span style="color: #008080; ">&nbsp;10</span>&nbsp;<span style="color: #000000; ">};<br />
</span><span style="color: #008080; ">&nbsp;11</span>&nbsp;<span style="color: #000000; ">typedef&nbsp;BTNode&nbsp;BTree;<br />
</span><span style="color: #008080; ">&nbsp;12</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">&nbsp;13</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;DestoryLink(BTree&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">T){<br />
</span><span style="color: #008080; ">&nbsp;14</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTree&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">&nbsp;15</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(T){<br />
</span><span style="color: #008080; ">&nbsp;16</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">T;<br />
</span><span style="color: #008080; ">&nbsp;17</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T</span><span style="color: #000000; ">=</span><span style="color: #000000; ">T</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;18</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;tmp;<br />
</span><span style="color: #008080; ">&nbsp;19</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">&nbsp;20</span>&nbsp;<span style="color: #000000; ">}<br />
</span><span style="color: #008080; ">&nbsp;21</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;PrintTree(BTree&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">T){<br />
</span><span style="color: #008080; ">&nbsp;22</span>&nbsp;<span style="color: #000000; ">&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; ">T)</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />
</span><span style="color: #008080; ">&nbsp;23</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;PrintTree(T</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left);<br />
</span><span style="color: #008080; ">&nbsp;24</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">T</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&nbsp;&nbsp;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">;<br />
</span><span style="color: #008080; ">&nbsp;25</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;PrintTree(T</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right);<br />
</span><span style="color: #008080; ">&nbsp;26</span>&nbsp;<span style="color: #000000; ">}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;27</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">&nbsp;28</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">&nbsp;29</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">&nbsp;30</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;Insert(BTree&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">T,BTNode&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">node){<br />
</span><span style="color: #008080; ">&nbsp;31</span>&nbsp;<span style="color: #000000; ">&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; ">T)</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />
</span><span style="color: #008080; ">&nbsp;32</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTNode&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">T,</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pre;<br />
</span><span style="color: #008080; ">&nbsp;33</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(tmp){<br />
</span><span style="color: #008080; ">&nbsp;34</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value&nbsp;</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;node</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value){<br />
</span><span style="color: #008080; ">&nbsp;35</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">&nbsp;36</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left;<br />
</span><span style="color: #008080; ">&nbsp;37</span>&nbsp;<span style="color: #000000; ">&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; ">(tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;node</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value){<br />
</span><span style="color: #008080; ">&nbsp;38</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">&nbsp;39</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right;<br />
</span><span style="color: #008080; ">&nbsp;40</span>&nbsp;<span style="color: #000000; ">&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; ">&nbsp;41</span>&nbsp;<span style="color: #000000; ">&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; ">must&nbsp;be&nbsp;unique&nbsp;key</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />
</span><span style="color: #008080; ">&nbsp;42</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;;<br />
</span><span style="color: #008080; ">&nbsp;43</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">&nbsp;44</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">&nbsp;45</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(pre</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value&nbsp;</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;node</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value){<br />
</span><span style="color: #008080; ">&nbsp;46</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left</span><span style="color: #000000; ">=</span><span style="color: #000000; ">node;<br />
</span><span style="color: #008080; ">&nbsp;47</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="color: #0000FF; ">else</span><span style="color: #000000; ">{<br />
</span><span style="color: #008080; ">&nbsp;48</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right</span><span style="color: #000000; ">=</span><span style="color: #000000; ">node;<br />
</span><span style="color: #008080; ">&nbsp;49</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">&nbsp;50</span>&nbsp;<span style="color: #000000; ">}<br />
</span><span style="color: #008080; ">&nbsp;51</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">&nbsp;52</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;BuildBTree(BTree&nbsp;</span><span style="color: #000000; ">*&amp;</span><span style="color: #000000; ">T,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;array[],</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;num){<br />
</span><span style="color: #008080; ">&nbsp;53</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;assert(num</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">);<br />
</span><span style="color: #008080; ">&nbsp;54</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;55</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;T</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;BTNode;<br />
</span><span style="color: #008080; ">&nbsp;56</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;T</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left</span><span style="color: #000000; ">=</span><span style="color: #000000; ">T</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right</span><span style="color: #000000; ">=</span><span style="color: #000000; ">NULL;<br />
</span><span style="color: #008080; ">&nbsp;57</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;T</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value</span><span style="color: #000000; ">=</span><span style="color: #000000; ">array[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">];<br />
</span><span style="color: #008080; ">&nbsp;58</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;59</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTNode&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">NULL;<br />
</span><span style="color: #008080; ">&nbsp;60</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;61</span>&nbsp;<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; ">num;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">){<br />
</span><span style="color: #008080; ">&nbsp;62</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;BTNode;<br />
</span><span style="color: #008080; ">&nbsp;63</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right</span><span style="color: #000000; ">=</span><span style="color: #000000; ">NULL;<br />
</span><span style="color: #008080; ">&nbsp;64</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value</span><span style="color: #000000; ">=</span><span style="color: #000000; ">array[i];<br />
</span><span style="color: #008080; ">&nbsp;65</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Insert(T,tmp);<br />
</span><span style="color: #008080; ">&nbsp;66</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">cout&lt;&lt;array[i]&lt;&lt;"&nbsp;*&nbsp;";</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">&nbsp;67</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">&nbsp;68</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />
</span><span style="color: #008080; ">&nbsp;69</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">PrintTree(T);</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">&nbsp;70</span>&nbsp;<span style="color: #008000; "></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; ">1</span><span style="color: #000000; ">;<br />
</span><span style="color: #008080; ">&nbsp;71</span>&nbsp;<span style="color: #000000; ">}<br />
</span><span style="color: #008080; ">&nbsp;72</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">&nbsp;73</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;BTreeToDoubleLinkList(BTree&nbsp;</span><span style="color: #000000; ">*&amp;</span><span style="color: #000000; ">T,BTNode&nbsp;</span><span style="color: #000000; ">*&amp;</span><span style="color: #000000; ">Head){<br />
</span><span style="color: #008080; ">&nbsp;74</span>&nbsp;<span style="color: #000000; ">&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; ">T)</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; ">&nbsp;75</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;76</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;77</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTNode&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pre,</span><span style="color: #000000; ">*</span><span style="color: #000000; ">next,</span><span style="color: #000000; ">*</span><span style="color: #000000; ">tmp,</span><span style="color: #000000; ">*</span><span style="color: #000000; ">connect;<br />
</span><span style="color: #008080; ">&nbsp;78</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">=</span><span style="color: #000000; ">next</span><span style="color: #000000; ">=</span><span style="color: #000000; ">NULL;<br />
</span><span style="color: #008080; ">&nbsp;79</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;Head</span><span style="color: #000000; ">=</span><span style="color: #000000; ">NULL;<br />
</span><span style="color: #008080; ">&nbsp;80</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">return&nbsp;1;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">&nbsp;81</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;stack</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">BTNode</span><span style="color: #000000; ">*&gt;</span><span style="color: #000000; ">&nbsp;st;<br />
</span><span style="color: #008080; ">&nbsp;82</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;st.push(T);<br />
</span><span style="color: #008080; ">&nbsp;83</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">T;<br />
</span><span style="color: #008080; ">&nbsp;84</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">&nbsp;86</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&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; ">st.empty()){<br />
</span><span style="color: #008080; ">&nbsp;87</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;88</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">st.top();<br />
</span><span style="color: #008080; ">&nbsp;89</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left){</span><span style="color: #008000; ">//</span><span style="color: #008000; ">一直向左边查找；&nbsp;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">&nbsp;90</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.push(tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left);<br />
</span><span style="color: #008080; ">&nbsp;91</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;92</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left;<br />
</span><span style="color: #008080; ">&nbsp;93</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">&nbsp;94</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;95</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(NULL&nbsp;</span><span style="color: #000000; ">==</span><span style="color: #000000; ">&nbsp;Head)Head</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">记录头结点；&nbsp;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">&nbsp;96</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">st.top();<br />
</span><span style="color: #008080; ">&nbsp;97</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.pop();<br />
</span><span style="color: #008080; ">&nbsp;98</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">&nbsp;99</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(NULL&nbsp;</span><span style="color: #000000; ">==</span><span style="color: #000000; ">&nbsp;pre){</span><span style="color: #008000; ">//</span><span style="color: #008000; ">记录前驱；&nbsp;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">100</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">101</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="color: #0000FF; ">else</span><span style="color: #000000; ">{&nbsp;<br />
</span><span style="color: #008080; ">102</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">进行链接；&nbsp;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">103</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">104</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left</span><span style="color: #000000; ">=</span><span style="color: #000000; ">pre;<br />
</span><span style="color: #008080; ">105</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">106</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">107</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">108</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">109</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right){<br />
</span><span style="color: #008080; ">110</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right;<br />
</span><span style="color: #008080; ">111</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.push(tmp);<br />
</span><span style="color: #008080; ">112</span>&nbsp;<span style="color: #000000; ">&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; ">113</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">当前元素出栈；&nbsp;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">114</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">st.top();<br />
</span><span style="color: #008080; ">115</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.pop();<br />
</span><span style="color: #008080; ">116</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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; ">tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right){<br />
</span><span style="color: #008080; ">117</span>&nbsp;<span style="color: #000000; ">&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; ">右节点为空，进行链接；&nbsp;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">118</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">119</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left</span><span style="color: #000000; ">=</span><span style="color: #000000; ">pre;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">120</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">121</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">122</span>&nbsp;<span style="color: #000000; ">&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; ">(st.empty())</span><span style="color: #0000FF; ">break</span><span style="color: #000000; ">;<br />
</span><span style="color: #008080; ">123</span>&nbsp;<span style="color: #000000; ">&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; ">st.top();<br />
</span><span style="color: #008080; ">124</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.pop();<br />
</span><span style="color: #008080; ">125</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">126</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">127</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">128</span>&nbsp;<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; ">(tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right){<br />
</span><span style="color: #008080; ">129</span>&nbsp;<span style="color: #000000; ">&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; ">右节点不为空，进行链接，入栈；&nbsp;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">130</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">131</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left</span><span style="color: #000000; ">=</span><span style="color: #000000; ">pre;<br />
</span><span style="color: #008080; ">132</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">133</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.push(tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right);<br />
</span><span style="color: #008080; ">134</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">135</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">136</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">137</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">tmp=tmp-&gt;right;</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">138</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">139</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">140</span>&nbsp;<span style="color: #000000; ">}&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">141</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">142</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">143</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main(){<br />
</span><span style="color: #008080; ">144</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">145</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">int&nbsp;array[]={10,6,11,4,9,13,8,12,16,7,15,14};</span><span style="color: #008000; "><br />
</span><span style="color: #008080; ">146</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;array[]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">{</span><span style="color: #000000; ">14</span><span style="color: #000000; ">,</span><span style="color: #000000; ">13</span><span style="color: #000000; ">,</span><span style="color: #000000; ">15</span><span style="color: #000000; ">,</span><span style="color: #000000; ">7</span><span style="color: #000000; ">,</span><span style="color: #000000; ">16</span><span style="color: #000000; ">,</span><span style="color: #000000; ">10</span><span style="color: #000000; ">,</span><span style="color: #000000; ">20</span><span style="color: #000000; ">,</span><span style="color: #000000; ">9</span><span style="color: #000000; ">,</span><span style="color: #000000; ">12</span><span style="color: #000000; ">,</span><span style="color: #000000; ">19</span><span style="color: #000000; ">,</span><span style="color: #000000; ">18</span><span style="color: #000000; ">}&nbsp;;<br />
</span><span style="color: #008080; ">147</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;num</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(array)</span><span style="color: #000000; ">/</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">);<br />
</span><span style="color: #008080; ">148</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">149</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTree&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">T</span><span style="color: #000000; ">=</span><span style="color: #000000; ">NULL;<br />
</span><span style="color: #008080; ">150</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTNode&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">Head</span><span style="color: #000000; ">=</span><span style="color: #000000; ">NULL;<br />
</span><span style="color: #008080; ">151</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BuildBTree(T,array,num);<br />
</span><span style="color: #008080; ">152</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">153</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTreeToDoubleLinkList(T,Head);<br />
</span><span style="color: #008080; ">154</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">155</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;BTNode&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">Head,</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pre;<br />
</span><span style="color: #008080; ">156</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">157</span>&nbsp;<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; ">&nbsp;\n&nbsp;in&nbsp;main&nbsp;&nbsp;print&nbsp;list</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />
</span><span style="color: #008080; ">158</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">159</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(tmp){<br />
</span><span style="color: #008080; ">160</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">;<br />
</span><span style="color: #008080; ">161</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp;<br />
</span><span style="color: #008080; ">162</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">tmp</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">right;<br />
</span><span style="color: #008080; ">163</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">164</span>&nbsp;<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; ">&nbsp;\nfrom&nbsp;tail&nbsp;to&nbsp;head</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />
</span><span style="color: #008080; ">165</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(pre){<br />
</span><span style="color: #008080; ">166</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">pre</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">value</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">;<br />
</span><span style="color: #008080; ">167</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre</span><span style="color: #000000; ">=</span><span style="color: #000000; ">pre</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">left;<br />
</span><span style="color: #008080; ">168</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080; ">169</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">170</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />
</span><span style="color: #008080; ">171</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;DestoryLink(Head);<br />
</span><span style="color: #008080; ">172</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">173</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">pause</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />
</span><span style="color: #008080; ">174</span>&nbsp;<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 />
</span><span style="color: #008080; ">175</span>&nbsp;<span style="color: #000000; ">}<br />
</span><span style="color: #008080; ">176</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">177</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">178</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">179</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">180</span>&nbsp;<span style="color: #000000; "><br />
</span><span style="color: #008080; ">181</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">182</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">183</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">184</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">185</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">186</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">187</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">188</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080; ">189</span>&nbsp;<span style="color: #000000; "></span></div>
<br />
<br />
<br />
<span style='font-family:宋体;"Times New Roman";mso-hansi-font-family:"Times New Roman"'><br />
</span>
<p>&nbsp;</p>
<br />
<img src ="http://www.cppblog.com/zhangzm/aggbug/147764.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhangzm/" target="_blank">bluedream</a> 2011-05-31 17:12 <a href="http://www.cppblog.com/zhangzm/archive/2011/05/31/147764.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转 POJ 计算几何入门题目推荐</title><link>http://www.cppblog.com/zhangzm/archive/2010/06/17/118081.html</link><dc:creator>bluedream</dc:creator><author>bluedream</author><pubDate>Thu, 17 Jun 2010 07:58:00 GMT</pubDate><guid>http://www.cppblog.com/zhangzm/archive/2010/06/17/118081.html</guid><wfw:comment>http://www.cppblog.com/zhangzm/comments/118081.html</wfw:comment><comments>http://www.cppblog.com/zhangzm/archive/2010/06/17/118081.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhangzm/comments/commentRss/118081.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhangzm/services/trackbacks/118081.html</trackback:ping><description><![CDATA[<p>计算几何题的特点与做题要领：<br>1.大部分不会很难，少部分题目思路很巧妙<br>2.做计算几何题目，模板很重要，模板必须高度可靠。<br>3.要注意代码的组织，因为计算几何的题目很容易上两百行代码，里面大部分是模板。如果代码一片混乱，那么会严重影响做题正确率。<br>4.注意精度控制。<br>5.能用整数的地方尽量用整数，要想到扩大数据的方法（扩大一倍，或扩大sqrt2）。因为整数不用考虑浮点误差，而且运算比浮点快。</p>
<p>一。点，线，面，形基本关系，点积叉积的理解</p>
<p>POJ 2318 TOYS（推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2318">http://acm.pku.edu.cn/JudgeOnline/problem?id=2318</a><br>POJ 2398 Toy Storage（推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2398">http://acm.pku.edu.cn/JudgeOnline/problem?id=2398</a><br>一个矩形，有被若干直线分成N个格子，给出一个点的坐标，问你该点位于哪个点中。<br>知识点：其实就是点在凸四边形内的判断，若利用叉积的性质，可以二分求解。</p>
<p>POJ 3304 Segments<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3304">http://acm.pku.edu.cn/JudgeOnline/problem?id=3304</a><br>知识点：线段与直线相交，注意枚举时重合点的处理</p>
<p>POJ 1269 Intersecting Lines <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1269">http://acm.pku.edu.cn/JudgeOnline/problem?id=1269</a><br>知识点：直线相交判断，求相交交点</p>
<p>POJ 1556 The Doors （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1556">http://acm.pku.edu.cn/JudgeOnline/problem?id=1556</a><br>知识点：简单图论＋简单计算几何，先求线段相交，然后再用Dij求最短路。</p>
<p>POJ 2653 Pick-up sticks <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2653">http://acm.pku.edu.cn/JudgeOnline/problem?id=2653</a><br>知识点：还是线段相交判断</p>
<p>POJ 1066 Treasure Hunt <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1066">http://acm.pku.edu.cn/JudgeOnline/problem?id=1066</a><br>知识点：线段相交判断，不过必须先理解&#8220;走最少的门&#8221;是怎么一回事。</p>
<p>POJ 1410 Intersection <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1410">http://acm.pku.edu.cn/JudgeOnline/problem?id=1410</a><br>知识点：线段与矩形相交。正确理解题意中相交的定义。<br>详见：<a href="http://hi.baidu.com/novosbirsk/blog/item/68c682c67e8d1f1d9d163df0.html">http://hi.baidu.com/novosbirsk/blog/item/68c682c67e8d1f1d9d163df0.html</a></p>
<p>POJ 1696 Space Ant （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1696">http://acm.pku.edu.cn/JudgeOnline/problem?id=1696</a><br>德黑兰赛区的好题目。需要理解点积叉积的性质</p>
<p>POJ 3347 Kadj Squares <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3347">http://acm.pku.edu.cn/JudgeOnline/problem?id=3347</a><br>本人的方法极度猥琐。复杂的线段相交问题。这个题目是计算几何的扩大数据运算的典型应用，扩大根号2倍之后就避免了小数。</p>
<p>POJ 2826 An Easy Problem?! （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2826">http://acm.pku.edu.cn/JudgeOnline/problem?id=2826</a><br>问：两条直线组成一个图形，能容纳多少雨水。很不简单的Easy Problem，要考虑所有情况。你不看discuss看看能否AC。（本人基本不能）提示一下，水是从天空垂直落下的。</p>
<p>POJ 1039 Pipe <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1039">http://acm.pku.edu.cn/JudgeOnline/problem?id=1039</a><br>又是线段与直线相交的判断，再加上枚举的思想即可。</p>
<p>POJ 3449 Geometric Shapes <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3449">http://acm.pku.edu.cn/JudgeOnline/problem?id=3449</a><br>判断几何体是否相交，不过输入输出很恶心。<br>此外，还有一个知识点，就是给出一个正方形（边不与轴平行）的两个对角线上的顶点，需要你求出另外两个点。必须掌握其方法。</p>
<p>POJ 1584 A Round Peg in a Ground Hole <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1584">http://acm.pku.edu.cn/JudgeOnline/problem?id=1584</a><br>知识点：点到直线距离，圆与多边形相交，多边形是否为凸</p>
<p>POJ 2074 Line of Sight （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2074">http://acm.pku.edu.cn/JudgeOnline/problem?id=2074</a><br>与视线问题的解法，关键是求过两点的直线方程，以及直线与线段的交点。数据有一个trick，要小心。</p>
<p>二。凸包问题</p>
<p>POJ 1113 Wall <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1113">http://acm.pku.edu.cn/JudgeOnline/problem?id=1113</a><br>知识点：赤裸裸的凸包问题，凸包周长加上圆周。</p>
<p>POJ 2007 Scrambled Polygon <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2007">http://acm.pku.edu.cn/JudgeOnline/problem?id=2007</a><br>知识点：凸包，按极角序输出方案</p>
<p>POJ 1873 The Fortified Forest （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1873">http://acm.pku.edu.cn/JudgeOnline/problem?id=1873</a><br>World Final的水题，先求凸包，然后再搜索。由于规模不大，可以使用位运算枚举。<br>详见：<a href="http://hi.baidu.com/novosbirsk/blog/item/333abd54c7f22c52574e0067.html">http://hi.baidu.com/novosbirsk/blog/item/333abd54c7f22c52574e0067.html</a></p>
<p>POJ 1228 Grandpa's Estate （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1228">http://acm.pku.edu.cn/JudgeOnline/problem?id=1228</a><br>求凸包顶点数目，很多人求凸包的模板是会多出点的，虽然求面积时能得到正确答案，但是在这个题目就会出问题。此外，还要正确理解凸包的性质。</p>
<p>POJ 3348 Cows <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3348">http://acm.pku.edu.cn/JudgeOnline/problem?id=3348</a><br>凸包面积计算</p>
<p>三。面积问题，公式问题</p>
<p>POJ 1654 Area <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1654">http://acm.pku.edu.cn/JudgeOnline/problem?id=1654</a><br>知识点：利用有向面积（叉积）计算多边形面积</p>
<p>POJ 1265 Area <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1265">http://acm.pku.edu.cn/JudgeOnline/problem?id=1265</a><br>POJ 2954 Triangle <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2954">http://acm.pku.edu.cn/JudgeOnline/problem?id=2954</a><br>Pick公式的应用，多边形与整点的关系。（存在一个GCD的关系）</p>
<p>四。半平面交</p>
<p>半平面交的主要应用是判断多边形是否存在核，还可以解决一些与线性方程组可行区域相关的问题（就是高中时的那些）。</p>
<p>POJ 3335 Rotating Scoreboard<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3335">http://acm.pku.edu.cn/JudgeOnline/problem?id=3335</a><br>POJ 3130 How I Mathematician Wonder What You Are! <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3130">http://acm.pku.edu.cn/JudgeOnline/problem?id=3130</a><br>POJ 1474 Video Surveillance<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1474">http://acm.pku.edu.cn/JudgeOnline/problem?id=1474</a><br>知识点：半平面交求多边形的核，存在性判断</p>
<p>POJ 1279 Art Gallery <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1279">http://acm.pku.edu.cn/JudgeOnline/problem?id=1279</a><br>半平面交求多边形的核，求核的面积</p>
<p>POJ 3525 Most Distant Point from the Sea （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3525">http://acm.pku.edu.cn/JudgeOnline/problem?id=3525</a><br>给出一个多边形，求里面的一个点，其距离离多边形的边界最远，也就是多边形中最大半径圆。<br>可以使用半平面交+二分法解。二分这个距离，边向内逼近，直到达到精度。</p>
<p>POJ 3384 Feng Shui （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3384">http://acm.pku.edu.cn/JudgeOnline/problem?id=3384</a><br>半平面交实际应用，用两个圆覆盖一个多边形，问最多能覆盖多边形的面积。<br>解法：用半平面交将多边形的每条边一起向&#8220;内&#8221;推进R，得到新的多边形，然后求多边形的最远两点。</p>
<p>POJ 1755 Triathlon （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1755">http://acm.pku.edu.cn/JudgeOnline/problem?id=1755</a><br>半平面交判断不等式是否有解。注意不等式在转化时正负号的选择，这直接影响到半平面交的方向。</p>
<p>POJ 2540 Hotter Colder <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2540">http://acm.pku.edu.cn/JudgeOnline/problem?id=2540</a><br>半平面交求线性规划可行区域的面积。</p>
<p>POJ 2451 Uyuw's Concert<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2451">http://acm.pku.edu.cn/JudgeOnline/problem?id=2451</a><br>Zzy专为他那篇nlogn算法解决半平面交问题的论文而出的题目。</p>
<p>五。计算几何背景，实际上解题的关键是其他问题（数据结构、组合数学，或者是枚举思想）<br>若干道经典的离散化＋扫描线的题目，ACM选手必做题目</p>
<p>POJ 1151 Atlantis （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1151">http://acm.pku.edu.cn/JudgeOnline/problem?id=1151</a><br>POJ 1389 Area of Simple Polygons<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1389">http://acm.pku.edu.cn/JudgeOnline/problem?id=1389</a><br>矩形离散化，线段树处理，矩形面积求交</p>
<p>POJ 1177 Picture （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1177">http://acm.pku.edu.cn/JudgeOnline/problem?id=1177</a><br>矩形离散化，线段树处理，矩形交的周长，这个题目的数据比较强。线段树必须高效。 </p>
<p>POJ 3565 Ants （推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3565">http://acm.pku.edu.cn/JudgeOnline/problem?id=3565</a><br>计算几何中的调整思想，有点像排序。要用到线段相交的判断。<br>详见：<a href="http://hi.baidu.com/novosbirsk/blog/item/fb668cf0f362bec47931aae2.html">http://hi.baidu.com/novosbirsk/blog/item/fb668cf0f362bec47931aae2.html</a></p>
<p>POJ 3695 Rectangles&nbsp;&nbsp;&nbsp; <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3695">http://acm.pku.edu.cn/JudgeOnline/problem?id=3695</a><br>又是矩形交的面积，但是由于是多次查询，而且矩形不多，使用组合数学中的容斥原理解决之最适合。线段树是通法，但是除了线段树，还有其他可行的方法。</p>
<p>POJ 2002 Squares&nbsp;&nbsp;&nbsp; <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2002">http://acm.pku.edu.cn/JudgeOnline/problem?id=2002</a><br>枚举思想，求平面上若干个点最多能组成多少个正方形，点的Hash</p>
<p>POJ 1434 Fill the Cisterns!（推荐）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1434">http://acm.pku.edu.cn/JudgeOnline/problem?id=1434</a><br>一开始发昏了，准备弄个线段树。其实只是个简单的二分。</p>
<p>六。随机算法<br>POJ 2420 A Star not a Tree? <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2420">http://acm.pku.edu.cn/JudgeOnline/problem?id=2420</a><br>多边形的费马点。所谓费马点，就是多边形中一个点P，该点到其他点的距离之和最短。四边形以上的多边形没有公式求费马点，因此可以使用随机化变步长贪心法。<br>详见：<a href="http://hi.baidu.com/novosbirsk/blog/item/75983f138499f825dd54019b.html">http://hi.baidu.com/novosbirsk/blog/item/75983f138499f825dd54019b.html</a></p>
<p>七。解析几何<br>这种题目本人不擅长，所以做得不多，模板很重要。当然，熟练运用叉积、点积的性质还是很有用的。<br>POJ 1375 Intervals <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1375">http://acm.pku.edu.cn/JudgeOnline/problem?id=1375</a><br>知识点：过圆外一点求与圆的切线</p>
<p>POJ 1329 Circle Through Three Points&nbsp;&nbsp;&nbsp; <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1329">http://acm.pku.edu.cn/JudgeOnline/problem?id=1329</a><br>求三角形外接圆</p>
<p>POJ 2354 Titanic<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2354">http://acm.pku.edu.cn/JudgeOnline/problem?id=2354</a><br>求球面上两个点的距离，而且给的是地理经纬坐标。</p>
<p>POJ 1106 Transmitters<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1106">http://acm.pku.edu.cn/JudgeOnline/problem?id=1106</a><br>角度排序，知道斜率求角度，使用atan函数。</p>
<p>POJ 1673 EXOCENTER OF A TRIANGLE<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1673">http://acm.pku.edu.cn/JudgeOnline/problem?id=1673</a><br>可以转化为三角形的垂心问题。</p>
<p>八。旋转卡壳</p>
<p>POJ 2187 Beauty Contest <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2187">http://acm.pku.edu.cn/JudgeOnline/problem?id=2187</a><br>凸包求最远点对。可以暴力枚举，也可以使用旋转卡壳。</p>
<p>POJ 3608 Bridge Across Islands（难）<br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3608">http://acm.pku.edu.cn/JudgeOnline/problem?id=3608</a><br>两个凸包的最近距离。本人的卡壳始终WA。郁闷。</p>
<p>九。其他问题<br>POJ 1981 Circle and Points <br><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1981">http://acm.pku.edu.cn/JudgeOnline/problem?id=1981</a><br>求单位圆最多能覆盖平面上多少个点</p>
<p>&nbsp;</p>
<p>本文来自CSDN博客，转载请标明出处：<a href="http://blog.csdn.net/linleiqin/archive/2010/04/04/5450150.aspx">http://blog.csdn.net/linleiqin/archive/2010/04/04/5450150.aspx</a></p>
<img src ="http://www.cppblog.com/zhangzm/aggbug/118081.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhangzm/" target="_blank">bluedream</a> 2010-06-17 15:58 <a href="http://www.cppblog.com/zhangzm/archive/2010/06/17/118081.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POJ 2593 Max Sequence</title><link>http://www.cppblog.com/zhangzm/archive/2010/06/16/118013.html</link><dc:creator>bluedream</dc:creator><author>bluedream</author><pubDate>Wed, 16 Jun 2010 03:10:00 GMT</pubDate><guid>http://www.cppblog.com/zhangzm/archive/2010/06/16/118013.html</guid><wfw:comment>http://www.cppblog.com/zhangzm/comments/118013.html</wfw:comment><comments>http://www.cppblog.com/zhangzm/archive/2010/06/16/118013.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhangzm/comments/commentRss/118013.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhangzm/services/trackbacks/118013.html</trackback:ping><description><![CDATA[<p class=pst>Description</p>
<div class=ptx lang=en-US>Give you N integers a1, a2 ... aN (|ai| &lt;=1000, 1 &lt;= i &lt;= N). <br>
<center><img src="http://acm.pku.edu.cn/JudgeOnline/images/2593_1.jpg"></center><br>You should output S. <br></div>
<p class=pst>Input</p>
<div class=ptx lang=en-US>The input will consist of several test cases. For each test case, one integer N (2 &lt;= N &lt;= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.</div>
<p class=pst>Output</p>
<div class=ptx lang=en-US>For each test of the input, print a line containing S.</div>
<p class=pst>Sample Input</p>
<pre class=sio>5
-5 9 -5 11 20
0
</pre>
<p class=pst>Sample Output</p>
<pre class=sio>40</pre>
<p class=pst>Source<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">cstdio</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: #008000">//</span><span style="COLOR: #008000">#include&nbsp;&lt;fstream&gt;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #008000"><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">int</span><span style="COLOR: #000000">&nbsp;a[</span><span style="COLOR: #000000">100002</span><span style="COLOR: #000000">],b[</span><span style="COLOR: #000000">100002</span><span style="COLOR: #000000">],c[</span><span style="COLOR: #000000">100002</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img id=Codehighlighter1_105_988_Open_Image onclick="this.style.display='none'; Codehighlighter1_105_988_Open_Text.style.display='none'; Codehighlighter1_105_988_Closed_Image.style.display='inline'; Codehighlighter1_105_988_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_105_988_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_105_988_Closed_Text.style.display='none'; Codehighlighter1_105_988_Open_Image.style.display='inline'; Codehighlighter1_105_988_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()</span><span id=Codehighlighter1_105_988_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_105_988_Open_Text><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;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_109_222_Open_Image onclick="this.style.display='none'; Codehighlighter1_109_222_Open_Text.style.display='none'; Codehighlighter1_109_222_Closed_Image.style.display='inline'; Codehighlighter1_109_222_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_109_222_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_109_222_Closed_Text.style.display='none'; Codehighlighter1_109_222_Open_Image.style.display='inline'; Codehighlighter1_109_222_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top></span><span id=Codehighlighter1_109_222_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">/**/</span><span id=Codehighlighter1_109_222_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;fstream&nbsp;fin("2593.txt");<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;if(!fin){<br></span><span style="COLOR: #008080">&nbsp;9</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;cout&lt;&lt;"can&nbsp;not&nbsp;open&nbsp;the&nbsp;file&nbsp;"&lt;&lt;endl;<br></span><span style="COLOR: #008080">10</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;system("pause");<br></span><span style="COLOR: #008080">11</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;return&nbsp;0;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="COLOR: #008000">*/</span></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/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">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">int</span><span style="COLOR: #000000">&nbsp;max;<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;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</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">16</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">17</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img id=Codehighlighter1_288_959_Open_Image onclick="this.style.display='none'; Codehighlighter1_288_959_Open_Text.style.display='none'; Codehighlighter1_288_959_Closed_Image.style.display='inline'; Codehighlighter1_288_959_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_288_959_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_288_959_Closed_Text.style.display='none'; Codehighlighter1_288_959_Open_Image.style.display='inline'; Codehighlighter1_288_959_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(n)</span><span id=Codehighlighter1_288_959_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_288_959_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_312_354_Open_Image onclick="this.style.display='none'; Codehighlighter1_312_354_Open_Text.style.display='none'; Codehighlighter1_312_354_Closed_Image.style.display='inline'; Codehighlighter1_312_354_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_312_354_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_312_354_Closed_Text.style.display='none'; Codehighlighter1_312_354_Open_Image.style.display='inline'; Codehighlighter1_312_354_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 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">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_312_354_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_312_354_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;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">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;b[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</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;&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;<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;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</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;sum</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1000000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_411_532_Open_Image onclick="this.style.display='none'; Codehighlighter1_411_532_Open_Text.style.display='none'; Codehighlighter1_411_532_Closed_Image.style.display='inline'; Codehighlighter1_411_532_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_411_532_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_411_532_Closed_Text.style.display='none'; Codehighlighter1_411_532_Open_Image.style.display='inline'; Codehighlighter1_411_532_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&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">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_411_532_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_411_532_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;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)tmp</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">a[j];<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_448_471_Open_Image onclick="this.style.display='none'; Codehighlighter1_448_471_Open_Text.style.display='none'; Codehighlighter1_448_471_Closed_Image.style.display='inline'; Codehighlighter1_448_471_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_448_471_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_448_471_Closed_Text.style.display='none'; Codehighlighter1_448_471_Open_Image.style.display='inline'; Codehighlighter1_448_471_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;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span id=Codehighlighter1_448_471_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_471_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;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[j];<br></span><span style="COLOR: #008080">30</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;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img id=Codehighlighter1_489_511_Open_Image onclick="this.style.display='none'; Codehighlighter1_489_511_Open_Text.style.display='none'; Codehighlighter1_489_511_Closed_Image.style.display='inline'; Codehighlighter1_489_511_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_489_511_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_489_511_Closed_Text.style.display='none'; Codehighlighter1_489_511_Open_Image.style.display='inline'; Codehighlighter1_489_511_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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">sum)</span><span id=Codehighlighter1_489_511_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_489_511_Open_Text><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;sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp;<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;}</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sum;<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;}</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</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;sum</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1000000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img id=Codehighlighter1_590_711_Open_Image onclick="this.style.display='none'; Codehighlighter1_590_711_Open_Text.style.display='none'; Codehighlighter1_590_711_Closed_Image.style.display='inline'; Codehighlighter1_590_711_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_590_711_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_590_711_Closed_Text.style.display='none'; Codehighlighter1_590_711_Open_Image.style.display='inline'; Codehighlighter1_590_711_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&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">n</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">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_590_711_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_590_711_Open_Text><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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)tmp</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">a[j];<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img id=Codehighlighter1_627_650_Open_Image onclick="this.style.display='none'; Codehighlighter1_627_650_Open_Text.style.display='none'; Codehighlighter1_627_650_Closed_Image.style.display='inline'; Codehighlighter1_627_650_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_627_650_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_627_650_Closed_Text.style.display='none'; Codehighlighter1_627_650_Open_Image.style.display='inline'; Codehighlighter1_627_650_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;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span id=Codehighlighter1_627_650_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_627_650_Open_Text><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;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[j];<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img id=Codehighlighter1_668_690_Open_Image onclick="this.style.display='none'; Codehighlighter1_668_690_Open_Text.style.display='none'; Codehighlighter1_668_690_Closed_Image.style.display='inline'; Codehighlighter1_668_690_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_668_690_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_668_690_Closed_Text.style.display='none'; Codehighlighter1_668_690_Open_Image.style.display='inline'; Codehighlighter1_668_690_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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">sum)</span><span id=Codehighlighter1_668_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_668_690_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">44</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;sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp;<br></span><span style="COLOR: #008080">45</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;}</span></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;&nbsp;&nbsp;&nbsp;c[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sum;<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&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 src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">11000000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img id=Codehighlighter1_758_889_Open_Image onclick="this.style.display='none'; Codehighlighter1_758_889_Open_Text.style.display='none'; Codehighlighter1_758_889_Closed_Image.style.display='inline'; Codehighlighter1_758_889_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_758_889_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_758_889_Closed_Text.style.display='none'; Codehighlighter1_758_889_Open_Image.style.display='inline'; Codehighlighter1_758_889_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&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">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">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_758_889_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_758_889_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img id=Codehighlighter1_784_883_Open_Image onclick="this.style.display='none'; Codehighlighter1_784_883_Open_Text.style.display='none'; Codehighlighter1_784_883_Closed_Image.style.display='inline'; Codehighlighter1_784_883_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_784_883_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_784_883_Closed_Text.style.display='none'; Codehighlighter1_784_883_Open_Image.style.display='inline'; Codehighlighter1_784_883_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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(b[j]</span><span style="COLOR: #000000">+</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">&gt;</span><span style="COLOR: #000000">max)</span><span id=Codehighlighter1_784_883_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_784_883_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">51</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;max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">b[j]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">c[j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</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>&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">cout&lt;&lt;"bj"&lt;&lt;b[j]&lt;&lt;"&nbsp;";<br></span><span style="COLOR: #008080">53</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;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">cout&lt;&lt;"c[j+1]"&lt;&lt;c[j+1]&lt;&lt;endl;</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/ExpandedSubBlockEnd.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><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;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,max);<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;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">system("pause");</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">58</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;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">59</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><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;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">fin.close();</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">61</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;</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">62</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">65</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">67</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/zhangzm/aggbug/118013.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhangzm/" target="_blank">bluedream</a> 2010-06-16 11:10 <a href="http://www.cppblog.com/zhangzm/archive/2010/06/16/118013.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> [转] HDU 动态规划（46道题目）倾情奉献~ 只提供思路与状态转移方程</title><link>http://www.cppblog.com/zhangzm/archive/2010/06/07/117345.html</link><dc:creator>bluedream</dc:creator><author>bluedream</author><pubDate>Mon, 07 Jun 2010 14:05:00 GMT</pubDate><guid>http://www.cppblog.com/zhangzm/archive/2010/06/07/117345.html</guid><wfw:comment>http://www.cppblog.com/zhangzm/comments/117345.html</wfw:comment><comments>http://www.cppblog.com/zhangzm/archive/2010/06/07/117345.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhangzm/comments/commentRss/117345.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhangzm/services/trackbacks/117345.html</trackback:ping><description><![CDATA[<div class=shareUser>转载自 <a href="http://hi.baidu.com/%CC%DA%D4%C6%BB%AA%CF%C4" target=blank>腾云华夏</a></div>
<table style="TABLE-LAYOUT: fixed; WIDTH: 100%">
    <tbody>
        <tr>
            <td>
            <div class=cnt id=blog_text>
            <div>Robberies <a href="http://acm.hdu.edu.cn/showproblem.php?pid=2955"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=2955</font></a> <br>背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱 最脑残的是把总的概率以为是抢N家银行的概率之和&#8230; 把状态转移方程写成了f[j]=max{f[j],f[j-q[i].v]+q[i].money}(f[j]表示在概率j之下能抢的大洋);</div>
            <p><br>正确的方程是:f[j]=max(f[j],f[j-q[i].money]*q[i].v) 其中,f[j]表示抢j块大洋的最大的逃脱概率,条件是f[j-q[i].money]可达,也就是之前抢劫过; <br>始化为:f[0]=1,其余初始化为-1 (抢0块大洋肯定不被抓嘛)</p>
            <p>最大报销额 <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1864"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1864</font></a> <br>又一个背包问题,对于每张发票,要么报销,要么不报销,0-1背包,张数即为背包; <br>转移方程:f[j]=max(f[j],f[j-1]+v[i]); <br>恶心地方:有这样的输入数据 3 A:100 A:200 A:300<br><br></p>
            <p>最大连续子序列 <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1231"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1231</font></a> <br>状态方程:sum[i]=max(sum[i-1]+a[i],a[i]);最后从头到尾扫一边 <br>也可以写成: <br>Max=a[0]; <br>Current=0; <br>for(i=0;i&lt;n;i++) { <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(Current&lt;0) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current=a[i]; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current+=a[i]; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(Current&gt;Max) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Max=Current; <br>}</p>
            <p>&nbsp;</p>
            <p>max sum <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1003"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1003</font></a> <br>同上,最大连续子序列</p>
            <p>Largest Rectangle <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1506"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1506</font></a> <br>对于每一块木板,Area=height[i]*(j-k+1) 其中,j&lt;=x&lt;=k,height[x]&gt;=height[i];找j,k成为关键,一般方法肯定超时,利用动态规划,如果它左边高度大于等于它本身,那么它左边的左边界一定满足这个性质,再从这个边界的左边迭代下去<br>&nbsp;for(i=1;i&lt;=n;i++){ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(a[l[i]-1]&gt;=a[i]) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l[i]=l[l[i]-1];</p>
            <p>}</p>
            <p>for(i=n;i&gt;=1;i&#8211;) <br>{ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(a[r[i]+1]&gt;=a[i]) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r[i]=r[r[i]+1]; <br>}</p>
            <p>&nbsp;</p>
            <p>City Game <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1505"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1505</font></a> <br>1506的加强版,把2维转换化成以每一行底,组成的最大面积;(注意处理连续与间断的情况);</p>
            <p>&nbsp;</p>
            <p>Bone Collector <a href="http://acm.hdu.edu.cn/showproblem.php?pid=2602"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=2602</font></a> <br>简单0-1背包,状态方程:f[j]=max(f[j],f[j-v[i]]+w[i])</p>
            <p>&nbsp;</p>
            <p>Super Jumping <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1087"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1087</font></a> <br>最大递增子段和,状态方程:sum[j]=max{sum[i]}+a[j]; 其中,0&lt;=i&lt;=j,a[i]&lt;a[j]</p>
            <p>命运http://acm.hdu.edu.cn/showproblem.php?pid=2571 <br>状态方程:sum[i][j]=max{sum[i-1][j],sum[i][k]}+v[i][j];其中1&lt;=k&lt;=j-1,且k是j的因子</p>
            <p>&nbsp;</p>
            <p>Monkey And Banana <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1069"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1069</font></a> <br>状态方程:f[j]=max{f[i]}+v[j];其中,0&lt;=i&lt;=j,w[i]&lt;w[j],h[i]&lt;h[j]</p>
            <p>&nbsp;</p>
            <p>Big Event in HDU <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1171"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1171</font></a> <br>一维背包,逐个考虑每个物品带来的影响,对于第i个物品:if(f[j-v[i]]==0) f[j]=0; <br>其中,j为逆序循环,且j&gt;=v[i]</p>
            <p>数塔http://acm.hdu.edu.cn/showproblem.php?pid=2084 <br>自底向上:dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+v[i][j];</p>
            <p>免费馅饼http://acm.hdu.edu.cn/showproblem.php?pid=1176 <br>简单数塔 <br>自底向上计算:dp[i][j]=max(dp[i+1][j-1],dp[i+1][j],dp[i+1][j+1])+v[i][j];处理边界</p>
            <p>&nbsp;</p>
            <p>I Need A Offer <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1203"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1203</font></a> <br>简单0-1背包,题目要求的是至少收到一份Offer的最大概率,我们得到得不到的最小概率即可,状态转移方程:f[j]=min(f[j],f[j-v[i]]*w[i]);其中,w[i]表示得不到的概率,(1-f[j])为花费j元得到Offer的最大概率</p>
            <p>&nbsp;</p>
            <p>FATE <a href="http://acm.hdu.edu.cn/showproblem.php?pid=2159"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=2159</font></a> <br>二维完全背包,第二层跟第三层的要顺序循环;(0-1背包逆序循环);状态可理解为,在背包属性为 {m(忍耐度), s(杀怪个数)} 里最多能得到的经验值,之前的背包牺牲体积,这个背包牺牲忍耐度跟个数 <br>注意: 最后扫的时候 外层循环为忍耐度,内层循环为杀怪个数,因为题目要求出剩余忍耐度最大,没有约束杀怪个数,一旦找到经验加满的即为最优解; <br>状态转移方程为: f[j][k]=max(f[j][k],f[j-v[i]][k-1]+w[i]); w[i]表示杀死第i个怪所得的经验值,v[i]表示消耗的忍耐度</p>
            <p>&nbsp;</p>
            <p>How To Type <a href="http://acm.hdu.edu.cn/showproblem.php?pid=2577"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=2577</font></a> <br>用两个a,b数组分别记录Caps Lock开与关时打印第i个字母的最少操作步骤; <br>而对于第i个字母的大小写还要分开讨论: <br>Ch[i]为小写: a[i]=min(a[i-1]+1,b[i-1]+2);不开灯直接字母,开灯则先关灯再按字母,最后保持不开灯; b[i]=min(a[i-1]+2,b[i-1]+2);不开灯则先按字母再开灯,开灯则Shift+字母(比关灯,按字母再开灯节省步数),最后保持开灯; <br>Ch[i]为大写: a[i]=min(a[i-1]+2,b[i-1]+2); b[i]=min(a[i-1]+2,b[i-1]+1)</p>
            <p>最后,b[len-1]++,关灯嘛O(&#8745;_&#8745;)O~</p>
            <p>&nbsp;</p>
            <p>Coins <a href="http://acm.hdu.edu.cn/showproblem.php?pid=2844"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=2844</font></a> <br>类似于HDU1171 Big Event In HDU,一维DP,可达可不达</p>
            <p>&nbsp;</p>
            <p>Beans <a href="http://acm.hdu.edu.cn/showproblem.php?pid=2845"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=2845</font></a> <br>横竖分别求一下不连续的最大子段和; <br>状态方程: Sum[i]=max(sum[j])+a[i];其中,0&lt;=j&lt;i-1;</p>
            <p>&nbsp;</p>
            <p>Largest Submatrix <a href="http://acm.hdu.edu.cn/showproblem.php?pid=2870"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=2870</font></a> <br>枚举a,b,c 最大完全子矩阵,类似于HDU1505 1506</p>
            <p>&nbsp;</p>
            <p>Matrix Swapping II <a href="http://acm.hdu.edu.cn/showproblem.php?pid=2830"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=2830</font></a> <br>最大完全子矩阵,以第i行为底,可以构成的最大矩阵,因为该题可以任意移动列,所以只要大于等于height[i]的都可以移动到一起,求出height&gt;=height[i]的个数即可,这里用hash+滚动,先求出height[i]出现的次数,然后逆序扫一遍hash[i]+=hash[i+1];</p>
            <p>最少拦截系统http://acm.hdu.edu.cn/showproblem.php?pid=1257 <br>两种做法,一是贪心,从后往前贪;二是DP; <br>if(v[i]&gt;max{dp[j]}) (0&lt;=j&lt;len) <br>dp[len++]=v[i];</p>
            <p>&nbsp;</p>
            <p>Common Subsequence <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1159"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1159</font></a> <br>经典DP,最长公共子序列 <br>Len[i][j]={len[i-1][j-1]+1,(a[i]==b[j]); max(len[i-1][j],len[i][j-1])} <br>初始化的优化: <br>for(i=0;i&lt;a;i++) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(j=0;j&lt;b;j++) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len[i][j]=0; <br>for(i=1;i&lt;=a;i++) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(j=1;j&lt;=b;j++) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(ch1[i-1]==ch2[j-1]) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len[i][j]=len[i-1][j-1]+1; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len[i][j]=max(len[i-1][j],len[i][j-1]);</p>
            <p>&nbsp;</p>
            <p>★ 搬寝室<a href="http://acm.hdu.edu.cn/showproblem.php?pid=1421" target=_blank><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1421</font></a> <br>状态Dp[i][j]为前i件物品选j对的最优解 <br>当i=j*2时,只有一种选择即 Dp[i-2][j-1]+(w[i]-w[i-1])^2 <br>当i&gt;j*2时,Dp[i][j] = min(Dp[i-1][j],Dp[i-2][j-1]+(w[j]-w[j-1])^2)</p>
            <p>&nbsp;</p>
            <p>★ Humble Numbers <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1058"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1058</font></a> <br>如果一个数是Humble Number,那么它的2倍,3倍,5倍,7倍仍然是Humble Number <br>定义F[i]为第i个Humble Number <br>F[n]=min(2*f[i],3*f[j],5*f[k],7*f[L]), i,j,k,L在被选择后相互移动 <br>(通过此题理解到数组有序特性)</p>
            <p>&nbsp;</p>
            <p>★ Doing Homework Again <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1789"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1789</font></a> <br>这题为贪心,经典题; <br>切题角度,对于每个任务要么在截至日期前完成要么被扣分;所以考虑每个人物的完成情况即可;由于每天只能完成一个任务,所以优先考虑分值较大的任务,看看该任务能不能完成,只要能完成,即使提前完成,占了其他任务的完成日期也没关系,因为当前任务的分值最大嘛,而对于能完成的任务能拖多久就拖多久,以便腾出更多时间完成其他任务;</p>
            <p>&nbsp;</p>
            <p>How Many Ways <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1978"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1978</font></a> <br>两种D法,一是对于当前的点,那些点可达;二是当前点可达那些点; <br>明显第二种方法高,因为第一种方法有一些没必要的尝试; <br>Dp[i][j]+=Dp[ii][jj]; (map[ii][jj]&gt;=两点的曼哈顿距离) <br>值得优化的地方,每两点的曼哈顿距离可能不止求一次,所以预处理一下直接读取</p>
            <p>&nbsp;</p>
            <p>珍惜现在 感恩生活<a href="http://acm.hdu.edu.cn/showproblem.php?pid=2191" target=_blank><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=2191</font></a> <br>每个物品最多可取n件,多重背包; <br>利用二进制思想,把每种物品转化为几件物品,然后就成为了0-1背包</p>
            <p>&nbsp;</p>
            <p>Piggy-Bank <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1114"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1114</font></a> <br>完全背包;常规背包是求最大值,这题求最小值; <br>只需要修改一下初始化,f[0]=0,其他赋值为+&#8734;即可; <br>状态转移方程:f[i][V]=max{f[i-1][V],f[i-1][V-k*v[i]]+k*w[i]},其中0&lt;=k*v[i]&lt;=V</p>
            <p>&nbsp;</p>
            <p>★ Max Sum Plus Plus <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1024"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1024</font></a> <br>1. 对于前n个数, 以v[n]为底取m段: <br>当n==m时,Sum[m][n]=Sum[m-1][n-1]+v[n],第n个数独立成段; <br>当n&gt;m时, Sum[m][n]=max{Sum[m-1][k],Sum[m][n-1]}+v[n]; 其中,m-1&lt;=k&lt;j,解释为,v[n]要么加在Sum[m][n-1],段数不变,要么独立成段接在前n-1个数取m-1段所能构成的最大值后面 <br>2. 空间的优化: <br>通过状态方程可以看出,取m段时,只与取m-1段有关,所以用滚动数组来节省空间</p>
            <p>&nbsp;</p>
            <p>FatMouse&#8217;s Speed <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1160"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1160</font></a> <br>要求:体重严格递增,速度严格递减,原始顺序不定 <br>按体重或者速度排序,即顺数固定后转化为最长上升子序列问题 <br>Dp[i]表示为以第i项为底构成的最长子序列,Dp[i]=max(dp[j])+1,其中0&lt;=j&lt;i , w[i]&gt;w[j]&amp;&amp;s[i]&lt;s[j] 用一个index数组构造最优解:记录每一项接在哪一项后面,最后用max找出最大的dp[0&#8230;n],dex记录下标,回溯输出即可</p>
            <p>&nbsp;</p>
            <p>Cstructing Roads <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1025"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1025</font></a> <br>以p或者r按升序排列以后,问题转化为最长上升子序列 <br>题目数据量比较大,只能采取二分查找,n*log(n)的算法 <br>用一个数组记录dp[]记录最长的子序列,len表示长度,如果a[i]&gt;dp[len], 则接在后面,len++; 否则在dp[]中找到最大的j,满足dp[j]&lt;a[i],把a[i]接在dp[j]后面;</p>
            <p>&nbsp;</p>
            <p>FatMouse Chees <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1078"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1078</font></a> <br>Dp思想,用记忆化搜索;简单题,处理好边界;</p>
            <p>&nbsp;</p>
            <p>To the Max <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1081"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1081</font></a> <br>最大子矩阵 <br>把多维转化为一维的最大连续子序列;(HDU1003)</p>
            <p>龟兔赛跑http://acm.hdu.edu.cn/showproblem.php?pid=2059 <br>未总结</p>
            <p>&nbsp;</p>
            <p>★ Employment Planning <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1158"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1158</font></a> <br>状态表示: Dp[i][j]为前i个月的留j个人的最优解;Num[i]&lt;=j&lt;=Max{Num[i]}; <br>j&gt;Max{Num[i]}之后无意义,无谓的浪费 记Max_n=Max{Num[i]}; <br>Dp[i-1]中的每一项都可能影响到Dp[i],即使Num[i-1]&lt;&lt;Num[i] <br>所以利用Dp[i-1]中的所有项去求Dp[i]; <br>对于Num[i]&lt;=k&lt;=Max_n, 当k&lt;j时, 招聘; <br>当k&gt;j时, 解雇 然后求出最小值 <br>Dp[i][j]=min{Dp[i-1][k&#8230;Max_n]+(招聘,解雇,工资);</p>
            <p>&nbsp;</p>
            <p>Dividing <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1059"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1059</font></a> <br>一维Dp Sum为偶数的时候判断Dp[sum/2]可不可达</p>
            <p>&nbsp;</p>
            <p>Human Gene Factions <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1080"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1080</font></a> <br>状态转移方程: <br>f[i][j]=Max(f[i-1][j-1]+r[a[i]][b[j]], f[i][j-1]+r[&#8216;-&#8216;][b[j]],f[i-1][j]+r[a[i]][&#8216;-&#8216;]);Pearls</p>
            <p>&nbsp;</p>
            <p><a href="http://acm.hdu.edu.cn/showproblem.php?pid=1300"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1300</font></a> <br>Dp[i]=min{Dp[j]+V}, 0&lt;=j&lt;i, V为第j+1类珠宝到第i类全部以i类买入的价值;</p>
            <p>&nbsp;</p>
            <p>Zipper <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1501"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1501</font></a> <br>Dp[i][j]=</p>
            <p>&nbsp;</p>
            <p>★Fast Food <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1227"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1227</font></a> <br>这里需要一个常识:在i到j取一点使它到区间每一点的距离之和最小,这一点为(i+j)/2用图形即可证明; <br>Dp[i][j]=max{Dp[i-1][k]+cost[k+1][j] 其中,(i-1)&lt;=k&lt;j状态为前j个position建i个depots</p>
            <p>&nbsp;</p>
            <p>Warcraft <a href="http://acm.hdu.edu.cn/showproblem.php?pid=3008"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=3008</font></a> <br>比赛的时候这道DP卡到我网络中心停电!!! 卧槽~ <br>因为你没有回血效应,所以你挂掉的时间是一定的; <br>用Dp[i][j]表示第i秒剩余j个单位的MP时怪物所剩的血量; 注意必须是剩余,也就是说,初始化的时候,DP[0][100]=100; 其他Dp[0]状态都不合法,因为没有开战的时候你的MP是满的 <br>以前的Dp都是利用前面得到的最优解来解决,而这题的麻烦点是MP在攻击过后要自动恢复x个单位;用当前的状态的状态推下一状态,仔细想想也未尝不可;状态转移方程为: <br>Dp[i+1][j-sk[k].mp+x]=min(Dp[i+1][j-sk[k].mp+x],Dp[i][j]+sk[k].at; 释放第K种技能,物理攻击可以看成是at=1,mp=0 的魔法;</p>
            <p>&nbsp;</p>
            <p>Regular Words <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1502"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1502</font></a> <br>F[a][b][c]=F[a-1][b][c]+F[a][b-1][c]+F[a][b][c-1]; <br>a&gt;=b&gt;=c;</p>
            <p>&nbsp;</p>
            <p>Advanced Fruits <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1503"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1503</font></a> <br>最长公共子序列的加强版</p>
            <p>&nbsp;</p>
            <p>★ Doing Homework <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1074"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1074</font></a> <br>这题用到位压缩; <br>那么任务所有的状态有2^n-1种 <br>状态方程为:Dp[next]=min{Dp[k]+i的罚时} 其中,next=k+(1&lt;&lt;i),k要取完满足条件的值 k&gt;&gt;i的奇偶性决定状态k <br>具体实现为: 对每种状态遍历n项任务,如果第i项没有完成,则计算出Dp[next]的最优解</p>
            <p>&nbsp;</p>
            <p>Free DIY Tour <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1224"><font color=#a91b33>http://acm.hdu.edu.cn/showproblem.php?pid=1224</font></a> <br>简单的数塔Dp,考察的是细节的处理; <br>Dp[i]=Max{Dp[j]}+v[i] 其中j-&gt;i为通路; <br>v[n+1]有没有初始化,Dp数组有没有初始化 <br>这题不能用想当然的&#8221;最长路&#8221;来解决,这好像是个NP问题 解决不了的</p>
            <p>重温世界杯http://acm.hdu.edu.cn/showproblem.php?pid=1422 <br>这题的状态不难理解,状态表示为,如果上一个城市剩下的钱不为负,也就是没有被赶回杭电,则再考虑它对下一个城市的影响;如果上一个城市剩下的前加上当前城市的前大于当前城市的生活费,那么Dp[i]=Dp[i-1]+1; <br>值得注意的而是这题的数据为100000;不可能以每个城市为起点来一次Dp,时间复杂度为n^2;足已超时; <br>我是这样处理的,在保存的数据后面再接上1&#8230;n的数据,这样扫描一遍的复杂度为n;再加一个优化,当Dp[i]==</p>
            <p>n时,也就是能全部游完所有城市的时候,直接break;</p>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.cppblog.com/zhangzm/aggbug/117345.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhangzm/" target="_blank">bluedream</a> 2010-06-07 22:05 <a href="http://www.cppblog.com/zhangzm/archive/2010/06/07/117345.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>矩阵连乘问题</title><link>http://www.cppblog.com/zhangzm/archive/2010/06/06/117280.html</link><dc:creator>bluedream</dc:creator><author>bluedream</author><pubDate>Sun, 06 Jun 2010 12:12:00 GMT</pubDate><guid>http://www.cppblog.com/zhangzm/archive/2010/06/06/117280.html</guid><wfw:comment>http://www.cppblog.com/zhangzm/comments/117280.html</wfw:comment><comments>http://www.cppblog.com/zhangzm/archive/2010/06/06/117280.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhangzm/comments/commentRss/117280.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhangzm/services/trackbacks/117280.html</trackback:ping><description><![CDATA[给定n个矩阵，给这n个矩阵加若干个括号，使得这n个矩阵所用的乘法次数最少。<br><br>动态规划的解法：<br>&nbsp;m[i][j]=min{ m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]}<br><br>其中 i&lt;=k&lt;j<br>code:<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>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</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">fstream</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">int</span><span style="COLOR: #000000">&nbsp;m[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">],s[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">100</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;p[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_135_473_Open_Image onclick="this.style.display='none'; Codehighlighter1_135_473_Open_Text.style.display='none'; Codehighlighter1_135_473_Closed_Image.style.display='inline'; Codehighlighter1_135_473_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_135_473_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_135_473_Closed_Text.style.display='none'; Codehighlighter1_135_473_Open_Image.style.display='inline'; Codehighlighter1_135_473_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sovle(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)</span><span id=Codehighlighter1_135_473_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_473_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;i,j,k,r;<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">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;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)m[i][i]</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 id=Codehighlighter1_198_424_Open_Image onclick="this.style.display='none'; Codehighlighter1_198_424_Open_Text.style.display='none'; Codehighlighter1_198_424_Closed_Image.style.display='inline'; Codehighlighter1_198_424_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_198_424_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_198_424_Closed_Text.style.display='none'; Codehighlighter1_198_424_Open_Image.style.display='inline'; Codehighlighter1_198_424_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;r</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;r</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_198_424_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_198_424_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_223_421_Open_Image onclick="this.style.display='none'; Codehighlighter1_223_421_Open_Text.style.display='none'; Codehighlighter1_223_421_Closed_Image.style.display='inline'; Codehighlighter1_223_421_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_223_421_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_223_421_Closed_Text.style.display='none'; Codehighlighter1_223_421_Open_Image.style.display='inline'; Codehighlighter1_223_421_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 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">r</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_223_421_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_223_421_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;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</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/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">cout&lt;&lt;"j=&nbsp;"&lt;&lt;j&lt;&lt;endl;&nbsp;</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/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">int</span><span style="COLOR: #000000">&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">m[i][i]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">m[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">p[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">p[i]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p[j];<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img id=Codehighlighter1_331_403_Open_Image onclick="this.style.display='none'; Codehighlighter1_331_403_Open_Text.style.display='none'; Codehighlighter1_331_403_Closed_Image.style.display='inline'; Codehighlighter1_331_403_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_331_403_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_331_403_Closed_Text.style.display='none'; Codehighlighter1_331_403_Open_Image.style.display='inline'; Codehighlighter1_331_403_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 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">j;k</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_331_403_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_331_403_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;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;mt</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">m[i][k]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">m[k</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">p[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">p[k]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p[j];<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img id=Codehighlighter1_392_398_Open_Image onclick="this.style.display='none'; Codehighlighter1_392_398_Open_Text.style.display='none'; Codehighlighter1_392_398_Closed_Image.style.display='inline'; Codehighlighter1_392_398_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_392_398_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_392_398_Closed_Text.style.display='none'; Codehighlighter1_392_398_Open_Image.style.display='inline'; Codehighlighter1_392_398_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 style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">mt)</span><span id=Codehighlighter1_392_398_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_392_398_Open_Text><span style="COLOR: #000000">{t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mt;}</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/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">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;m[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t;<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/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;cout</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">m[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][n</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<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;m[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][n];<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">&nbsp;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_497_680_Open_Image onclick="this.style.display='none'; Codehighlighter1_497_680_Open_Text.style.display='none'; Codehighlighter1_497_680_Closed_Image.style.display='inline'; Codehighlighter1_497_680_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_497_680_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_497_680_Closed_Text.style.display='none'; Codehighlighter1_497_680_Open_Image.style.display='inline'; Codehighlighter1_497_680_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()</span><span id=Codehighlighter1_497_680_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_497_680_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;fstream&nbsp;fin(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">matrix.txt</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;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n;<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;fin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n;<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img id=Codehighlighter1_573_595_Open_Image onclick="this.style.display='none'; Codehighlighter1_573_595_Open_Text.style.display='none'; Codehighlighter1_573_595_Closed_Image.style.display='inline'; Codehighlighter1_573_595_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_573_595_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_573_595_Closed_Text.style.display='none'; Codehighlighter1_573_595_Open_Image.style.display='inline'; Codehighlighter1_573_595_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&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">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_573_595_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_573_595_Open_Text><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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">p[i];<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;}</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;<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;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">sovle(n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><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;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&nbsp;<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;fin.close();<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;</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><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">43</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/zhangzm/aggbug/117280.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhangzm/" target="_blank">bluedream</a> 2010-06-06 20:12 <a href="http://www.cppblog.com/zhangzm/archive/2010/06/06/117280.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最小m段和问题</title><link>http://www.cppblog.com/zhangzm/archive/2010/05/31/116786.html</link><dc:creator>bluedream</dc:creator><author>bluedream</author><pubDate>Mon, 31 May 2010 03:10:00 GMT</pubDate><guid>http://www.cppblog.com/zhangzm/archive/2010/05/31/116786.html</guid><wfw:comment>http://www.cppblog.com/zhangzm/comments/116786.html</wfw:comment><comments>http://www.cppblog.com/zhangzm/archive/2010/05/31/116786.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhangzm/comments/commentRss/116786.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhangzm/services/trackbacks/116786.html</trackback:ping><description><![CDATA[<h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="FONT-SIZE: 10pt"><span style="FONT-SIZE: 12pt">最小m段和问题<br></span></span><br><span style="FONT-SIZE: 8pt">给定 n 个整数组成的序列，现在要求将序列分割为 m 段，每段子序列中<br>的数在原序列中连续排列。如何分割才能使这m段子序列的和的最大值达<br>到最小？</span> </h1>
<h1><span style="FONT-SIZE: 12pt">动态规划的解法：<o:p></o:p></span></h1>
<p>1.&nbsp;&nbsp;&nbsp; 阶段数为m.<o:p></o:p></p>
<p>2.&nbsp;&nbsp;&nbsp; 状态转移方程：<o:p></o:p></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; f[i][j]=min max{ f[i][1]-f[k][1],f[k][j-1]}<br><br><o:p></o:p></p>
<p>&nbsp;&nbsp;其中1&lt;=k&lt;i；j&lt;= i &lt;=n； 1&lt;= j &lt;= m;<o:p></o:p></p>
<p>&nbsp;&nbsp;初态：f[i][1]=f[i-1][1]+a[i]<o:p></o:p></p>
<p>&nbsp;&nbsp;最优值为f[n][m]。<o:p></o:p></p>
<p>3.&nbsp;&nbsp;&nbsp; 对于第j阶段中的每个 i, (j&lt;=i &lt;=n)，考虑将1到i的序列分为j段子序列,且第j段序列从k开始到i，<br>由最优子结构性质，只需比较第j段序列和第j-1阶段的最优值即可。<o:p></o:p></p>
<p>&nbsp;&nbsp;<o:p></o:p></p>
<p>&nbsp; 决策变量：第j段序列分配i-k个元素<o:p></o:p></p>
<p><o:p>&nbsp;</o:p></p>
<p>源代码：<br></p>
<p><o: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">fstream</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><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img id=Codehighlighter1_71_642_Open_Image onclick="this.style.display='none'; Codehighlighter1_71_642_Open_Text.style.display='none'; Codehighlighter1_71_642_Closed_Image.style.display='inline'; Codehighlighter1_71_642_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_71_642_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_71_642_Closed_Text.style.display='none'; Codehighlighter1_71_642_Open_Image.style.display='inline'; Codehighlighter1_71_642_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()</span><span id=Codehighlighter1_71_642_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_71_642_Open_Text><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;n,m;<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;ifstream&nbsp;fin(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sum1.in</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_118_193_Open_Image onclick="this.style.display='none'; Codehighlighter1_118_193_Open_Text.style.display='none'; Codehighlighter1_118_193_Closed_Image.style.display='inline'; Codehighlighter1_118_193_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_118_193_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_118_193_Closed_Text.style.display='none'; Codehighlighter1_118_193_Open_Image.style.display='inline'; Codehighlighter1_118_193_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&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">fin)</span><span id=Codehighlighter1_118_193_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_118_193_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;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">can&nbsp;not&nbsp;open&nbsp;the&nbsp;file&nbsp;!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<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;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</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/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">12</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">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<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;fin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">m;<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;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n;<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;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a[n</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 id=Codehighlighter1_250_266_Open_Image onclick="this.style.display='none'; Codehighlighter1_250_266_Open_Text.style.display='none'; Codehighlighter1_250_266_Closed_Image.style.display='inline'; Codehighlighter1_250_266_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_250_266_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_250_266_Closed_Text.style.display='none'; Codehighlighter1_250_266_Open_Image.style.display='inline'; Codehighlighter1_250_266_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.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;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_250_266_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_250_266_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;fin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">a[i];<br></span><span style="COLOR: #008080">19</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">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;fin.close();<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">int</span><span style="COLOR: #000000">&nbsp;f[n</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][m</span><span style="COLOR: #000000">+</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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">初始化第一阶段信息&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">23</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;f[</span><span style="COLOR: #000000">0</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">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img id=Codehighlighter1_348_377_Open_Image onclick="this.style.display='none'; Codehighlighter1_348_377_Open_Text.style.display='none'; Codehighlighter1_348_377_Closed_Image.style.display='inline'; Codehighlighter1_348_377_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_348_377_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_348_377_Closed_Text.style.display='none'; Codehighlighter1_348_377_Open_Image.style.display='inline'; Codehighlighter1_348_377_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&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">n;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_348_377_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_348_377_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;f[i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">f[i</span><span style="COLOR: #000000">-</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">a[i];<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;}</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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tmp,maxN</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_418_587_Open_Image onclick="this.style.display='none'; Codehighlighter1_418_587_Open_Text.style.display='none'; Codehighlighter1_418_587_Closed_Image.style.display='inline'; Codehighlighter1_418_587_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_418_587_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_418_587_Closed_Text.style.display='none'; Codehighlighter1_418_587_Open_Image.style.display='inline'; Codehighlighter1_418_587_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&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">2</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">m;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_418_587_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_418_587_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img id=Codehighlighter1_443_584_Open_Image onclick="this.style.display='none'; Codehighlighter1_443_584_Open_Text.style.display='none'; Codehighlighter1_443_584_Closed_Image.style.display='inline'; Codehighlighter1_443_584_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_443_584_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_443_584_Closed_Text.style.display='none'; Codehighlighter1_443_584_Open_Image.style.display='inline'; Codehighlighter1_443_584_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 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">j;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_443_584_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_443_584_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;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1000000</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img id=Codehighlighter1_484_562_Open_Image onclick="this.style.display='none'; Codehighlighter1_484_562_Open_Text.style.display='none'; Codehighlighter1_484_562_Closed_Image.style.display='inline'; Codehighlighter1_484_562_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_484_562_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_484_562_Closed_Text.style.display='none'; Codehighlighter1_484_562_Open_Image.style.display='inline'; Codehighlighter1_484_562_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 style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;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">i;k</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id=Codehighlighter1_484_562_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_484_562_Open_Text><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;maxN</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">std::max(f[i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">f[k][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">],f[k][j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])&nbsp;;<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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">maxN)tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">maxN;<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;&nbsp;&nbsp;&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp;<br></span><span style="COLOR: #008080">36</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">37</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">38</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">f[n][m]</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 src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</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;&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">41</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">42</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<p>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;<br>测试：<br>9 3<br>9 8 7 6 5 4 3 2 1<br><br>输出<br>17<br></p>
</o:p>
<p>
<p>2010年5月31日11:08:38&nbsp;</p>
<p>&nbsp;</p>
<img src ="http://www.cppblog.com/zhangzm/aggbug/116786.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhangzm/" target="_blank">bluedream</a> 2010-05-31 11:10 <a href="http://www.cppblog.com/zhangzm/archive/2010/05/31/116786.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>