﻿<?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++博客-yzhw@ujs code my life~-随笔分类-DP</title><link>http://www.cppblog.com/yzhw/category/15161.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 09 Feb 2012 12:49:56 GMT</lastBuildDate><pubDate>Thu, 09 Feb 2012 12:49:56 GMT</pubDate><ttl>60</ttl><item><title>pku3903 最长递增字串的单调性优化</title><link>http://www.cppblog.com/yzhw/archive/2012/02/09/165233.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Thu, 09 Feb 2012 11:33:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2012/02/09/165233.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/165233.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2012/02/09/165233.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/165233.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/165233.html</trackback:ping><description><![CDATA[好久不动题目了，今天动了一题，忒背运，死都TLE，想了想，算法复杂度没问题啊，n=100000，nlogn的算法拼1S的RP咋崩溃的。。后来想起了无敌的POJ超级慢速set，无奈，不想改，到处搜有这题的OJ，TOJ上跑了下，10MS。。汗。。下载数据数据来看了看，就是12组，就是本地debug也不会TLE啊。。没办法。。<br />不发牢骚了，说说这题的方法吧。<br />一般的最长递增字串用n^2做很好做，就是dp[i]=max{dp[j]+1,height[j]&lt;height[i]}，这题可以用单调队列做优化，但是和普通的单调队列不同，要借助平衡树<br />首先，我们知道，要维护这样一个单调队列，当height[i]&lt;height[j]时，要有dp[i]&lt;dp[j]，否则的话，status{(j,height[j])}这个状态以后不会推得最优解，可以删除。这题麻烦就麻烦再height不是个单调的函数，随着i的增大（就是沿着DP方向计算）时，height不能保证也是递增的，木有办法，只能维护一个平衡树那样的东西。<br />那么更新过的DP方程就是<br />dp[i]=dp[j]+1,j为满足height[j]&lt;height[i]的最后一个元素<br />然后更新单调队列的策略是把当前决策加入单调队列里，然后往后删除dp[i]&gt;=dp[j]并且height[i]&lt;height[j]的statue{j}。<br />每个元素最多入队一次，出队一次，所以总得复杂度o(nlogn)<br />我是全部用set实现的，轻松好省<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;#&nbsp;include&nbsp;&lt;cstdio&gt;<br /><span style="color: #008080; ">&nbsp;2</span>&nbsp;#&nbsp;include&nbsp;&lt;<span style="color: #0000FF; ">set</span>&gt;<br /><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #0000FF; ">using</span>&nbsp;<span style="color: #0000FF; ">namespace</span>&nbsp;std;<br /><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #0000FF; ">struct</span>&nbsp;node<br /><span style="color: #008080; ">&nbsp;5</span>&nbsp;{<br /><span style="color: #008080; ">&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;height,id;<br /><span style="color: #008080; ">&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp;node(<span style="color: #0000FF; ">int</span>&nbsp;h,<span style="color: #0000FF; ">int</span>&nbsp;i):height(h),id(i){}<br /><span style="color: #008080; ">&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">bool</span>&nbsp;<span style="color: #0000FF; ">operator</span>&lt;(<span style="color: #0000FF; ">const</span>&nbsp;node&nbsp;&amp;pos)&nbsp;<span style="color: #0000FF; ">const</span><br /><span style="color: #008080; ">&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp;{<br /><span style="color: #008080; ">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(height!=pos.height)&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;height&lt;pos.height;<br /><span style="color: #008080; ">11</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;id&lt;pos.id;<br /><span style="color: #008080; ">12</span>&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">13</span>&nbsp;};<br /><span style="color: #008080; ">14</span>&nbsp;<span style="color: #0000FF; ">set</span>&lt;node&gt;&nbsp;q;<br /><span style="color: #008080; ">15</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;dp[100005];<br /><span style="color: #008080; ">16</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;main()<br /><span style="color: #008080; ">17</span>&nbsp;{<br /><span style="color: #008080; ">18</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;n;<br /><span style="color: #008080; ">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(scanf("%d",&amp;n)!=EOF)<br /><span style="color: #008080; ">20</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /><span style="color: #008080; ">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.clear();<br /><span style="color: #008080; ">22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;i=0;i&lt;n;i++)<br /><span style="color: #008080; ">23</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /><span style="color: #008080; ">24</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;t;<br /><span style="color: #008080; ">25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&amp;t);<br /><span style="color: #008080; ">26</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">set</span>&lt;node&gt;::iterator&nbsp;it=q.lower_bound(node(t,-1));<br /><span style="color: #008080; ">27</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;ans;<br /><span style="color: #008080; ">28</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(it==q.begin())<br /><span style="color: #008080; ">29</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans=1;<br /><span style="color: #008080; ">30</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span><br /><span style="color: #008080; ">31</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans=dp[(--it)-&gt;id]+1;<br /><span style="color: #008080; ">32</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it=q.lower_bound(node(t,-1));<br /><span style="color: #008080; ">33</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(it!=q.end()&amp;&amp;it-&gt;height==t)&nbsp;<br /><span style="color: #008080; ">34</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it=q.erase(it);<br /><span style="color: #008080; ">35</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(it!=q.end()&amp;&amp;dp[it-&gt;id]&lt;=ans)&nbsp;<br /><span style="color: #008080; ">36</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it=q.erase(it);<br /><span style="color: #008080; ">37</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.insert(node(t,i));&nbsp;<br /><span style="color: #008080; ">38</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i]=ans;<br /><span style="color: #008080; ">39</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">40</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n",dp[q.rbegin()-&gt;id]);<br /><span style="color: #008080; ">41</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">42</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br /><span style="color: #008080; ">43</span>&nbsp;}<br /><span style="color: #008080; ">44</span>&nbsp;</div><img src ="http://www.cppblog.com/yzhw/aggbug/165233.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2012-02-09 19:33 <a href="http://www.cppblog.com/yzhw/archive/2012/02/09/165233.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 3998 Land Division DP斜率优化</title><link>http://www.cppblog.com/yzhw/archive/2011/10/18/158598.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Mon, 17 Oct 2011 18:17:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/10/18/158598.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/158598.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/10/18/158598.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/158598.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/158598.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 题意：n个点，用水平线或者竖直线划分成k条，要求平均差最小，平均差为每条中点的个数减去n/k的绝对值。解法：首先一看就是个DP，与处理不说了，用水平扫描线、竖直扫描线将同一直线上的点压缩成一个带权值的点，然后DP这题重要的是DP降维观察DP方程dp[i]=min{dp[j]+|sum[i]-sum[j]-average|}这里就要分成两部分讨论1、当sum[i]-sum[j]&gt;average...&nbsp;&nbsp;<a href='http://www.cppblog.com/yzhw/archive/2011/10/18/158598.html'>阅读全文</a><img src ="http://www.cppblog.com/yzhw/aggbug/158598.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-10-18 02:17 <a href="http://www.cppblog.com/yzhw/archive/2011/10/18/158598.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>The 36th ACM/ICPC Asia Regional Dalian Online Contest  大连2011ICPC网络赛 个人题解</title><link>http://www.cppblog.com/yzhw/archive/2011/09/04/155042.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sun, 04 Sep 2011 02:42:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/09/04/155042.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/155042.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/09/04/155042.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/155042.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/155042.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 好久不写题，外加上在准备考研，这次有点悲剧，比赛时A了5题，还有一题有点小BUG，比赛后才A。。今年要考南京大学~也没时间准备ICPC了，希望我们队里的那个小孩子能够快快成长起来，我这个队长也可以安心的卸任了，明年将最后一次比赛机会放在NJU吧~恩，顺便去看下高中膜拜过的蒋炎研同学~江苏大学加油！第一题：其实就是构建一个DAG，然后再DAG上求最长路径。要注意构图时候避免环，特别考虑当两个木块长宽...&nbsp;&nbsp;<a href='http://www.cppblog.com/yzhw/archive/2011/09/04/155042.html'>阅读全文</a><img src ="http://www.cppblog.com/yzhw/aggbug/155042.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-09-04 10:42 <a href="http://www.cppblog.com/yzhw/archive/2011/09/04/155042.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku3124 The Bookcase 扩展背包好题</title><link>http://www.cppblog.com/yzhw/archive/2011/07/31/152170.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sun, 31 Jul 2011 13:20:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/07/31/152170.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/152170.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/07/31/152170.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/152170.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/152170.html</trackback:ping><description><![CDATA[题意：<br />一堆书，每本书都有厚度和高度，管理员试图将这些书放到三层的书架上，每层都不能为空，求书架最小体积（书架深度显然为所有书深度最大值，所以与排放方案无关，故取1）.<br />体积公式：<img src="http://poj.org/images/3124_2.jpg" alt="" /><br />解题思想：<br />大概思路是DP，这个要先定下来<br />设v=(f(s1)+f(s2)+f(s3))*max{g(s1),g(s2),g(s3)}<br />其中g(s3)=SUM-g(s1)-g(s2)<br />f(s3)=max(h<sub>i</sub>)<br />就说说，我们有4个决策变量<br />一个变量留给DP决策，另一个变量利用DP状态设计来消除（这就需要对初始数据排序，保证决策过程中h<sub>i</sub>&lt;=h<sub>p2</sub>,i属于s2,p2为当前决策点）。<br />这个问题就转化为在一个有序队列上选择两个分割点p1,p2，h<sub>p1</sub>&lt;h<sub>p2</sub>,使得max{hi,i属于s1}&lt;p1,max{hi,i属于s2}&lt;p2,并且s1,s2,s3均不为空<br />状态可以设计为<br />F(i,t<sub>1</sub>,t<sub>2</sub>)<br />即考虑到第i个元素，s1的总厚度为t1,s2的总厚度为t2时p1所在元素的高度的最小值。<br />F（i,t1,t2)=min(F(i-1,t1,t2-data[i].t),data[i].h(当F(i-1,t1-data[i].t,t2)合法时),F(i-1,t1,t2))，显然，三个转移意义分别为将当前元素分配给s2、s1、s3<br />当F(i,t1,t2)由F(i-1,t1,t2-data[i].t)转移过来的时候试图更新全局最优解ans=min(ans,(data[i].h+F(i,t1,t2)+data[n-1].h)*t1*t2*(t<sub>total</sub>-t1-t2))。注意，这里说F(i,t1,t2)由F(i-1,t1,t2-data[i].t)转移过来指的是F(i-1,t1,t2-data[i].t)&lt;=min(data[i].h(当F(i-1,t1-data[i].t,t2)合法时),F(i-1,t1,t2)),等于不能丢掉（原因？只有此时才能更新全局最优解）<br />代码如下<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_212128" onclick="this.style.display='none'; Code_Closed_Text_212128.style.display='none'; Code_Open_Image_212128.style.display='inline'; Code_Open_Text_212128.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_212128" style="display: none" onclick="this.style.display='none'; Code_Open_Text_212128.style.display='none'; Code_Closed_Image_212128.style.display='inline'; Code_Closed_Text_212128.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_212128" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"></span><span id="Code_Open_Text_212128" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #000000; ">Source&nbsp;Code<br /></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #000000; ">Problem:&nbsp;</span><span style="color: #000000; ">3124</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User:&nbsp;yzhw<br /></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #000000; ">Memory:&nbsp;17744K&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time:&nbsp;2188MS<br /></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #000000; ">Language:&nbsp;G</span><span style="color: #000000; ">++</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result:&nbsp;Accepted<br /></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #000000; ">Source&nbsp;Code<br /></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #000000; ">#&nbsp;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;8</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">utility</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">functional</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">cstring</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">11</span>&nbsp;<span style="color: #000000; ">#&nbsp;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; ">12</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">algorithm</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">13</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; ">14</span>&nbsp;<span style="color: #000000; ">#&nbsp;define&nbsp;M&nbsp;</span><span style="color: #000000; ">2105</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #000000; ">#&nbsp;define&nbsp;N&nbsp;</span><span style="color: #000000; ">100</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;dp[M][M],n,total,ans,sum[N];<br /></span><span style="color: #008080; ">17</span>&nbsp;<span style="color: #000000; ">pair</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;data[N];<br /></span><span style="color: #008080; ">18</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">bool</span><span style="color: #000000; ">&nbsp;cmp(</span><span style="color: #0000FF; ">const</span><span style="color: #000000; ">&nbsp;pair</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">a,</span><span style="color: #0000FF; ">const</span><span style="color: #000000; ">&nbsp;pair</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">b)<br /></span><span style="color: #008080; ">19</span>&nbsp;<span style="color: #000000; ">{<br /></span><span style="color: #008080; ">20</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;a.first</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">b.first;<br /></span><span style="color: #008080; ">21</span>&nbsp;<span style="color: #000000; ">}<br /></span><span style="color: #008080; ">22</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">23</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; ">24</span>&nbsp;<span style="color: #000000; ">{<br /></span><span style="color: #008080; ">25</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">freopen("bookcase.in","r",stdin);</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">26</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;test;<br /></span><span style="color: #008080; ">27</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">test);<br /></span><span style="color: #008080; ">28</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;t</span><span style="color: #000000; ">=</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;t</span><span style="color: #000000; ">&lt;=</span><span style="color: #000000; ">test;t</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">29</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">30</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">n);<br /></span><span style="color: #008080; ">31</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">32</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0xfffffff</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">33</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;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; ">)<br /></span><span style="color: #008080; ">34</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">35</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d%d</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">data[i].first,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">data[i].second);<br /></span><span style="color: #008080; ">36</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total</span><span style="color: #000000; ">+=</span><span style="color: #000000; ">data[i].second;<br /></span><span style="color: #008080; ">37</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">38</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(data</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">,data</span><span style="color: #000000; ">+</span><span style="color: #000000; ">n</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">,cmp);<br /></span><span style="color: #008080; ">39</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">40</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;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; ">)<br /></span><span style="color: #008080; ">41</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum[i]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">sum[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; ">data[i].second;<br /></span><span style="color: #008080; ">42</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(dp));<br /></span><span style="color: #008080; ">43</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">44</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;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; ">)<br /></span><span style="color: #008080; ">45</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">46</span>&nbsp;<span style="color: #000000; ">&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;j</span><span style="color: #000000; ">=</span><span style="color: #000000; ">sum[i</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">];j</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;j</span><span style="color: #000000; ">--</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">47</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: #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; ">sum[i</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">];k</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;k</span><span style="color: #000000; ">--</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">48</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(dp[j][k]</span><span style="color: #000000; ">!=-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">49</span>&nbsp;<span style="color: #000000; ">&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; ">50</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(dp[j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i].second][k]</span><span style="color: #000000; ">==-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">||</span><span style="color: #000000; ">dp[j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i].second][k]</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">data[i].first)<br /></span><span style="color: #008080; ">51</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i].second][k]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">data[i].first;<br /></span><span style="color: #008080; ">52</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(dp[j][k</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i].second]</span><span style="color: #000000; ">==-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">||</span><span style="color: #000000; ">dp[j][k</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i].second]</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">dp[j][k])<br /></span><span style="color: #008080; ">53</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&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; ">54</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j][k</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i].second]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">dp[j][k];<br /></span><span style="color: #008080; ">55</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(j</span><span style="color: #000000; ">&amp;&amp;</span><span style="color: #000000; ">j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">k</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i].second</span><span style="color: #000000; ">!=</span><span style="color: #000000; ">total)<br /></span><span style="color: #008080; ">56</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="color: #000000; ">=</span><span style="color: #000000; ">min(ans,(dp[j][k]</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i].first</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[n].first)</span><span style="color: #000000; ">*</span><span style="color: #000000; ">max(j,max(k</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i].second,total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">j</span><span style="color: #000000; ">-</span><span style="color: #000000; ">k</span><span style="color: #000000; ">-</span><span style="color: #000000; ">data[i].second)));<br /></span><span style="color: #008080; ">57</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&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; ">58</span>&nbsp;<span style="color: #000000; ">&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; ">59</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">60</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,ans);<br /></span><span style="color: #008080; ">61</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">62</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">63</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; ">64</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">65</span>&nbsp;<span style="color: #000000; "></span></span></div><img src ="http://www.cppblog.com/yzhw/aggbug/152170.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-07-31 21:20 <a href="http://www.cppblog.com/yzhw/archive/2011/07/31/152170.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1202 Family DAG图上的概率DP</title><link>http://www.cppblog.com/yzhw/archive/2011/02/05/139749.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sat, 05 Feb 2011 12:59:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/02/05/139749.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/139749.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/02/05/139749.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/139749.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/139749.html</trackback:ping><description><![CDATA[<p>题意：<br>给出N个怪物的家谱树，求M对怪物间的相关度。怪物可能一夫多妻或一妻多夫，也可以隔代交配。<br><br>给力条件：DAG<br><br>解法：<br>众所周知，DP有两种推理方法：第i个状态能推出哪些状态以及第i个状态可以由哪些状态得出，本题必须使用第二种方案<br>dp[pos][i],i=1..n为第pos个节点与其前趋（包括间接）节点间的相关度。<br>状态转移即为dp[pos][i]=sum(dp[p][i]*0.5),p为pos的直接前驱趋节点。<br>这题POJ好诡异，死都过不去，但是在小poj(poj.grids.cn)，和zju上都没问题。可能将递归改成拓扑序上的迭代就可以了。不过我懒，不想动- -<br><br>代码：<br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.util.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.math.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img id=Codehighlighter1_76_1648_Open_Image onclick="this.style.display='none'; Codehighlighter1_76_1648_Open_Text.style.display='none'; Codehighlighter1_76_1648_Closed_Image.style.display='inline'; Codehighlighter1_76_1648_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_76_1648_Closed_Image onclick="this.style.display='none'; Codehighlighter1_76_1648_Closed_Text.style.display='none'; Codehighlighter1_76_1648_Open_Image.style.display='inline'; Codehighlighter1_76_1648_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Main&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_76_1648_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_76_1648_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;nxt[][]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">305</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">305</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;BigDecimal&nbsp;dp[][]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;BigDecimal[</span><span style="COLOR: #000000">305</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">305</span><span style="COLOR: #000000">],two</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">BigDecimal.ONE.add(BigDecimal.ONE);<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,m</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">boolean</span><span style="COLOR: #000000">&nbsp;used[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">boolean</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">305</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;StreamTokenizer&nbsp;in</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;StreamTokenizer(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;BufferedReader(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;InputStreamReader(System.in)));<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;nextInt()&nbsp;</span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000">&nbsp;IOException<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_435_494_Open_Image onclick="this.style.display='none'; Codehighlighter1_435_494_Open_Text.style.display='none'; Codehighlighter1_435_494_Closed_Image.style.display='inline'; Codehighlighter1_435_494_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_435_494_Closed_Image onclick="this.style.display='none'; Codehighlighter1_435_494_Closed_Text.style.display='none'; Codehighlighter1_435_494_Open_Image.style.display='inline'; Codehighlighter1_435_494_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_435_494_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_435_494_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in.nextToken();<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">)in.nval;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;dfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;pos)<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img id=Codehighlighter1_529_857_Open_Image onclick="this.style.display='none'; Codehighlighter1_529_857_Open_Text.style.display='none'; Codehighlighter1_529_857_Closed_Image.style.display='inline'; Codehighlighter1_529_857_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_529_857_Closed_Image onclick="this.style.display='none'; Codehighlighter1_529_857_Closed_Text.style.display='none'; Codehighlighter1_529_857_Open_Image.style.display='inline'; Codehighlighter1_529_857_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_529_857_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_529_857_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(used[pos])&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;used[pos]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">2</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">nxt[pos][j]</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">)<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img id=Codehighlighter1_648_814_Open_Image onclick="this.style.display='none'; Codehighlighter1_648_814_Open_Text.style.display='none'; Codehighlighter1_648_814_Closed_Image.style.display='inline'; Codehighlighter1_648_814_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_648_814_Closed_Image onclick="this.style.display='none'; Codehighlighter1_648_814_Closed_Text.style.display='none'; Codehighlighter1_648_814_Open_Image.style.display='inline'; Codehighlighter1_648_814_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_648_814_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_648_814_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nxt[pos][j];<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(p);<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img id=Codehighlighter1_733_804_Open_Image onclick="this.style.display='none'; Codehighlighter1_733_804_Open_Text.style.display='none'; Codehighlighter1_733_804_Closed_Image.style.display='inline'; Codehighlighter1_733_804_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_733_804_Closed_Image onclick="this.style.display='none'; Codehighlighter1_733_804_Closed_Text.style.display='none'; Codehighlighter1_733_804_Open_Image.style.display='inline'; Codehighlighter1_733_804_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">)&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_733_804_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_733_804_Open_Text><span style="COLOR: #000000">{dp[pos][i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[pos][i].add(dp[p][i].divide(two));dp[i][pos]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[pos][i];}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[pos][pos]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">BigDecimal.ONE;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_917_1646_Open_Image onclick="this.style.display='none'; Codehighlighter1_917_1646_Open_Text.style.display='none'; Codehighlighter1_917_1646_Closed_Image.style.display='inline'; Codehighlighter1_917_1646_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_917_1646_Closed_Image onclick="this.style.display='none'; Codehighlighter1_917_1646_Closed_Text.style.display='none'; Codehighlighter1_917_1646_Open_Image.style.display='inline'; Codehighlighter1_917_1646_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000">&nbsp;IOException</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_917_1646_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_917_1646_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nextInt();<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nextInt();<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nxt</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[n</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;used</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">boolean</span><span style="COLOR: #000000">[n</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;BigDecimal[n</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][n</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">)<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img id=Codehighlighter1_1091_1185_Open_Image onclick="this.style.display='none'; Codehighlighter1_1091_1185_Open_Text.style.display='none'; Codehighlighter1_1091_1185_Closed_Image.style.display='inline'; Codehighlighter1_1091_1185_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1091_1185_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1091_1185_Closed_Text.style.display='none'; Codehighlighter1_1091_1185_Open_Image.style.display='inline'; Codehighlighter1_1091_1185_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1091_1185_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1091_1185_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Arrays.fill(dp[i],BigDecimal.ZERO);<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Arrays.fill(nxt[i],</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Arrays.fill(used,&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">m;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img id=Codehighlighter1_1258_1371_Open_Image onclick="this.style.display='none'; Codehighlighter1_1258_1371_Open_Text.style.display='none'; Codehighlighter1_1258_1371_Closed_Image.style.display='inline'; Codehighlighter1_1258_1371_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1258_1371_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1258_1371_Closed_Text.style.display='none'; Codehighlighter1_1258_1371_Open_Image.style.display='inline'; Codehighlighter1_1258_1371_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1258_1371_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1258_1371_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nextInt(),b</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nextInt(),c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nextInt();<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nxt[a][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">b;<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nxt[a][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c;<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">)<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(i);<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nextInt();<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&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">m;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img id=Codehighlighter1_1478_1640_Open_Image onclick="this.style.display='none'; Codehighlighter1_1478_1640_Open_Text.style.display='none'; Codehighlighter1_1478_1640_Closed_Image.style.display='inline'; Codehighlighter1_1478_1640_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1478_1640_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1478_1640_Closed_Text.style.display='none'; Codehighlighter1_1478_1640_Open_Image.style.display='inline'; Codehighlighter1_1478_1640_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1478_1640_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1478_1640_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nextInt(),b</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nextInt();<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(dp[a][b].multiply(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;BigDecimal(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)).stripTrailingZeros().toPlainString()</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/139749.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-02-05 20:59 <a href="http://www.cppblog.com/yzhw/archive/2011/02/05/139749.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1946 Cow Cycling 非常好的DP</title><link>http://www.cppblog.com/yzhw/archive/2011/02/05/139736.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Fri, 04 Feb 2011 17:13:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/02/05/139736.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/139736.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/02/05/139736.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/139736.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/139736.html</trackback:ping><description><![CDATA[题意：<br>n头奶牛跑步，一个领跑，另外的跟跑。总距离是k，如果某一单位时间速度为v，则领队奶牛消耗v^2能量，其他奶牛消耗v能量。每头奶牛初始的能量相同。问奶牛们到达终点的最短时间。<br><br>解法:<br>其他不说了，重要的是第i个牛在哪几个时刻领队并不重要，重要的是领了多少个时间单位的队，下面状态就不难划分了吧？<br><br>代码：<br><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stdio.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">string</span><span style="color: #000000; ">.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stdlib.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;dp[</span><span style="color: #000000; ">21</span><span style="color: #000000; ">][</span><span style="color: #000000; ">101</span><span style="color: #000000; ">][</span><span style="color: #000000; ">101</span><span style="color: #000000; ">],n,e,d;<br></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;solve(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;used,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;e1,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;e2)<br></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #000000; ">{<br></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(e1</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">||</span><span style="color: #000000; ">e2</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">)&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #000000; ">&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; ">(used</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">n)&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #000000; ">&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; ">(e</span><span style="color: #000000; ">-</span><span style="color: #000000; ">e2</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">d)&nbsp;<br></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #000000; ">&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; ">11</span>&nbsp;<span style="color: #000000; ">&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; ">(dp[used][e1][e2]</span><span style="color: #000000; ">!=-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">)&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;dp[used][e1][e2]</span><span style="color: #000000; ">==-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">?-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">:dp[used][e1][e2];<br></span><span style="color: #008080; ">12</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br></span><span style="color: #008080; ">13</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080; ">14</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i;<br></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[used][e1][e2]</span><span style="color: #000000; ">=-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #000000; ">&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; ">;e1</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i</span><span style="color: #000000; ">*</span><span style="color: #000000; ">i</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">17</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(solve(used,e1</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i</span><span style="color: #000000; ">*</span><span style="color: #000000; ">i,e2</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; ">&amp;&amp;</span><span style="color: #000000; ">(dp[used][e1][e2]</span><span style="color: #000000; ">==-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">||</span><span style="color: #000000; ">solve(used,e1</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i</span><span style="color: #000000; ">*</span><span style="color: #000000; ">i,e2</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; ">&lt;</span><span style="color: #000000; ">dp[used][e1][e2]))<br></span><span style="color: #008080; ">18</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[used][e1][e2]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">solve(used,e1</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i</span><span style="color: #000000; ">*</span><span style="color: #000000; ">i,e2</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i)</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">19</span>&nbsp;<span style="color: #000000; ">&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; ">;e2</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i</span><span style="color: #000000; ">*</span><span style="color: #000000; ">i</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">20</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(solve(used</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">,e2</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i</span><span style="color: #000000; ">*</span><span style="color: #000000; ">i,e2</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; ">&amp;&amp;</span><span style="color: #000000; ">(dp[used][e1][e2]</span><span style="color: #000000; ">==-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">||</span><span style="color: #000000; ">solve(used</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">,e2</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i</span><span style="color: #000000; ">*</span><span style="color: #000000; ">i,e2</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; ">&lt;</span><span style="color: #000000; ">dp[used][e1][e2]))<br></span><span style="color: #008080; ">21</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[used][e1][e2]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">solve(used</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">,e2</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i</span><span style="color: #000000; ">*</span><span style="color: #000000; ">i,e2</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i)</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">22</span>&nbsp;<span style="color: #000000; "><br></span><span style="color: #008080; ">23</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;dp[used][e1][e2]</span><span style="color: #000000; ">==-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">?-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">:dp[used][e1][e2];<br></span><span style="color: #008080; ">24</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080; ">25</span>&nbsp;<span style="color: #000000; ">}<br></span><span style="color: #008080; ">26</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; ">27</span>&nbsp;<span style="color: #000000; ">{<br></span><span style="color: #008080; ">28</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d%d%d</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">n,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">e,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">d);<br></span><span style="color: #008080; ">29</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(dp));<br></span><span style="color: #008080; ">30</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,solve(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,e,e)</span><span style="color: #000000; ">==-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">?</span><span style="color: #000000; ">0</span><span style="color: #000000; ">:solve(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,e,e));<br></span><span style="color: #008080; ">31</span>&nbsp;<span style="color: #000000; ">&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; ">32</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; ">0</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">33</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080; ">34</span>&nbsp;<span style="color: #000000; ">}</span></div><img src ="http://www.cppblog.com/yzhw/aggbug/139736.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-02-05 01:13 <a href="http://www.cppblog.com/yzhw/archive/2011/02/05/139736.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1948  Triangular Pastures DP+枚举。海伦公式</title><link>http://www.cppblog.com/yzhw/archive/2011/02/05/139735.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Fri, 04 Feb 2011 17:02:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/02/05/139735.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/139735.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/02/05/139735.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/139735.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/139735.html</trackback:ping><description><![CDATA[
题意<br>给出一些木棒，问能否用这些木棒拼成三角形，如果可能求最大的面积。<br><br>解法：<br>先给出一个定理：海伦公式<br><img src="http://imgsrc.baidu.com/baike/abpic/item/b258f5c4664154e98226ace2.jpg" id="" vspace="0" hspace="0" border="" align="baseline" alt="" longdesc=""><br>下面就是枚举边长。首先枚举最长边c，由于两边之和大于第三边，c的长度必须小于周长的一半，这是一个重要的剪枝。如果还要剪枝，可以先一次DP求得由木棍组合可以达到的长度。<br>确定c以后再枚举a，然后再次可以用DP验证木棒能否组合成a、c的长度。这里可以再一次剪枝，枚举c的时候建议从周长的一半向下枚举，原因很简单，尽量构造一个较优的合法解，根据海伦公式(p-a)(p-b)(p-c)，当c确定时最大值的位置也清楚了（b和c相等的位置，如果在最优位置（上限，可能达不到）都不能比最优解优，就直接cut）。然后下面就没什么难度了。<br><br>代码：<br><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #000000; ">#&nbsp;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>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">cstring</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">cstdlib</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">cmath</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #008080; ">&nbsp;5</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;6</span>&nbsp;<span style="color: #000000; ">#&nbsp;include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">algorithm</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">bool</span><span style="color: #000000; ">&nbsp;dp[</span><span style="color: #000000; ">2</span><span style="color: #000000; ">][</span><span style="color: #000000; ">855</span><span style="color: #000000; ">][</span><span style="color: #000000; ">855</span><span style="color: #000000; ">];<br></span><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;plen[</span><span style="color: #000000; ">1605</span><span style="color: #000000; ">],pc</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;n,len[</span><span style="color: #000000; ">41</span><span style="color: #000000; ">];<br></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #000000; "><br></span><span style="color: #008080; ">11</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; ">12</span>&nbsp;<span style="color: #000000; ">{<br></span><span style="color: #008080; ">13</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;total</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">14</span>&nbsp;<span style="color: #000000; ">&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; ">15</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; ">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; ">)<br></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080; ">17</span>&nbsp;<span style="color: #000000; ">&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; ">,len</span><span style="color: #000000; ">+</span><span style="color: #000000; ">i);<br></span><span style="color: #008080; ">18</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total</span><span style="color: #000000; ">+=</span><span style="color: #000000; ">len[i];<br></span><span style="color: #008080; ">19</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080; ">20</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;sort(len,len</span><span style="color: #000000; ">+</span><span style="color: #000000; ">n);<br></span><span style="color: #008080; ">21</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">bool</span><span style="color: #000000; ">&nbsp;tmp[</span><span style="color: #000000; ">1606</span><span style="color: #000000; ">];<br></span><span style="color: #008080; ">22</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;memset(tmp,</span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(tmp));<br></span><span style="color: #008080; ">23</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;memset(dp[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">],</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(dp[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]));<br></span><span style="color: #008080; ">24</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">25</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; ">n;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">26</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080; ">27</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp[i</span><span style="color: #000000; ">%</span><span style="color: #000000; ">2</span><span style="color: #000000; ">],</span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(dp[i</span><span style="color: #000000; ">%</span><span style="color: #000000; ">2</span><span style="color: #000000; ">]));<br></span><span style="color: #008080; ">28</span>&nbsp;<span style="color: #000000; ">&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; ">total</span><span style="color: #000000; ">/</span><span style="color: #000000; ">2</span><span style="color: #000000; ">+</span><span style="color: #000000; ">40</span><span style="color: #000000; ">;j</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">29</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;k</span><span style="color: #000000; ">=</span><span style="color: #000000; ">j;k</span><span style="color: #000000; ">&lt;=</span><span style="color: #000000; ">total</span><span style="color: #000000; ">/</span><span style="color: #000000; ">2</span><span style="color: #000000; ">+</span><span style="color: #000000; ">40</span><span style="color: #000000; ">;k</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">30</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(j</span><span style="color: #000000; ">-</span><span style="color: #000000; ">len[i</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; ">0</span><span style="color: #000000; ">&amp;&amp;</span><span style="color: #000000; ">dp[(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; ">2</span><span style="color: #000000; ">][min(j</span><span style="color: #000000; ">-</span><span style="color: #000000; ">len[i</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">],k)][max(j</span><span style="color: #000000; ">-</span><span style="color: #000000; ">len[i</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">],k)]</span><span style="color: #000000; ">||</span><span style="color: #000000; "><br></span><span style="color: #008080; ">31</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="color: #000000; ">-</span><span style="color: #000000; ">len[i</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; ">0</span><span style="color: #000000; ">&amp;&amp;</span><span style="color: #000000; ">dp[(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; ">2</span><span style="color: #000000; ">][min(j,k</span><span style="color: #000000; ">-</span><span style="color: #000000; ">len[i</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">])][max(k</span><span style="color: #000000; ">-</span><span style="color: #000000; ">len[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; "><br></span><span style="color: #008080; ">32</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[(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; ">2</span><span style="color: #000000; ">][j][k])<br></span><span style="color: #008080; ">33</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="color: #000000; ">%</span><span style="color: #000000; ">2</span><span style="color: #000000; ">][j][k]</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">34</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080; ">35</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080; ">36</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;tmp[len[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]]</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">37</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; ">n;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">38</span>&nbsp;<span style="color: #000000; ">&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; ">total;j</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">39</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[j]</span><span style="color: #000000; ">&amp;&amp;</span><span style="color: #000000; ">j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">len[i]</span><span style="color: #000000; ">&lt;=</span><span style="color: #000000; ">total)<br></span><span style="color: #008080; ">40</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp[j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">len[i]]</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">41</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; ">0</span><span style="color: #000000; ">;i</span><span style="color: #000000; ">&lt;=</span><span style="color: #000000; ">total;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">42</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(tmp[i])<br></span><span style="color: #008080; ">43</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;plen[pc</span><span style="color: #000000; ">++</span><span style="color: #000000; ">]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">i;<br></span><span style="color: #008080; ">44</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;best</span><span style="color: #000000; ">=-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">45</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;c</span><span style="color: #000000; ">=</span><span style="color: #000000; ">lower_bound(plen,plen</span><span style="color: #000000; ">+</span><span style="color: #000000; ">pc,total</span><span style="color: #000000; ">/</span><span style="color: #000000; ">2</span><span style="color: #000000; ">)</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;c</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;c</span><span style="color: #000000; ">--</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">46</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080; ">47</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;a</span><span style="color: #000000; ">=</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen[c])</span><span style="color: #000000; ">/</span><span style="color: #000000; ">2</span><span style="color: #000000; ">,b</span><span style="color: #000000; ">=</span><span style="color: #000000; ">total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen[c]</span><span style="color: #000000; ">-</span><span style="color: #000000; ">a;<br></span><span style="color: #008080; ">48</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(best</span><span style="color: #000000; ">!=-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">&amp;&amp;</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">plen[c])</span><span style="color: #000000; ">*</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">a)</span><span style="color: #000000; ">*</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">b)</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">best)&nbsp;</span><span style="color: #0000FF; ">continue</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">49</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(a</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;plen[a]</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen[c];a</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">50</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(plen[c]</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">plen[a]</span><span style="color: #000000; ">&amp;&amp;</span><span style="color: #000000; ">plen[c]</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen[a]</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen[c]</span><span style="color: #000000; ">&amp;&amp;</span><span style="color: #000000; ">dp[n</span><span style="color: #000000; ">%</span><span style="color: #000000; ">2</span><span style="color: #000000; ">][plen[a]][plen[c]]</span><span style="color: #000000; ">&amp;&amp;</span><span style="color: #000000; ">(best</span><span style="color: #000000; ">==-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">||</span><span style="color: #000000; ">best</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">plen[c])</span><span style="color: #000000; ">*</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">plen[a])</span><span style="color: #000000; ">*</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen[a]</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen[c]))))<br></span><span style="color: #008080; ">51</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;best</span><span style="color: #000000; ">=</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">plen[c])</span><span style="color: #000000; ">*</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">plen[a])</span><span style="color: #000000; ">*</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">*</span><span style="color: #000000; ">(total</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen[a]</span><span style="color: #000000; ">-</span><span style="color: #000000; ">plen[c]));<br></span><span style="color: #008080; ">52</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080; ">53</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(best</span><span style="color: #000000; ">==-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">)&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">-1\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br></span><span style="color: #008080; ">54</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; ">55</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080; ">56</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;best</span><span style="color: #000000; ">=</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">)(sqrt(best</span><span style="color: #000000; ">*</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">double</span><span style="color: #000000; ">)total)</span><span style="color: #000000; ">*</span><span style="color: #000000; ">25</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1e</span><span style="color: #000000; ">-</span><span style="color: #000000; ">6</span><span style="color: #000000; ">);<br></span><span style="color: #008080; ">57</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,best);<br></span><span style="color: #008080; ">58</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080; ">59</span>&nbsp;<span style="color: #000000; ">&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; ">60</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; ">0</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">61</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080; ">62</span>&nbsp;<span style="color: #000000; ">}<br></span><span style="color: #008080; ">63</span>&nbsp;<span style="color: #000000; "></span></div><img src ="http://www.cppblog.com/yzhw/aggbug/139735.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-02-05 01:02 <a href="http://www.cppblog.com/yzhw/archive/2011/02/05/139735.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1335 Digital Onion 递归</title><link>http://www.cppblog.com/yzhw/archive/2011/01/25/139251.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Mon, 24 Jan 2011 17:01:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/25/139251.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/139251.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/25/139251.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/139251.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/139251.html</trackback:ping><description><![CDATA[题意：<br>定义括号序列：<br><span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium Simsun; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="FONT-FAMILY: 'Times New Roman', Times, serif; FONT-SIZE: 16px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class=Apple-style-span>Definition: Null parenthesis is a DO. Especially, we call this the Null DO.<span class=Apple-converted-space>&nbsp;</span><br>Definition: "( )" is a DO. Especially, we call this the primitive DO.<span class=Apple-converted-space>&nbsp;</span><br>Definition: If both A and B are DOs, then the combined form of "(A)B" is also a DO, where we call A the inside of the DO and B the outside of the DO.<span class=Apple-converted-space>&nbsp;<br>定义排序规则：<br><span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium Simsun; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="FONT-FAMILY: 'Times New Roman', Times, serif; FONT-SIZE: 16px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class=Apple-style-span>[Rule1]: The more weight, the more expensive.<span class=Apple-converted-space>&nbsp;</span><br>[Rule2]: If the weights of two DOs are equal, then the price depends on the inside DO of the two DOs.<span class=Apple-converted-space>&nbsp;</span><br>[Rule3]: If the weights of two DOs are the same and the prices of the inside DOs are equal, then the price depends on the outside DO of the two DOs.<span class=Apple-converted-space>&nbsp;<br>给出一个符号序列，求这个之后的一个符号序列是什么<br><br>思路：<br>首先要确定最小的符号序列为：（）（）（）...<br>然后就是转化方法：<br>对于一个符号序列(inside)outside，首先看outside能否+1，不行的话看inside能否+1，同时将outside置为最小值形式，再不行的话当outside不为空的时候将inside的weight+1,outside's weight-1，将两部分都转化为最小值形式。<br>以上描述的是一个递归过程~当然，考虑一种特殊情况，如果整个式子无法进行+1操作，只能将整个式子的weight+1,然后化为最小值形式。<br><br>发现java的string竟然是值传递，非常的蛋疼。。我喜欢java的String，没办法，只好用C++的string。。。这种字符串处理的题目string是非常给力的~<br><br>代码：<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;str;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;make(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;s,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;e,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;type)<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_125_299_Open_Image onclick="this.style.display='none'; Codehighlighter1_125_299_Open_Text.style.display='none'; Codehighlighter1_125_299_Closed_Image.style.display='inline'; Codehighlighter1_125_299_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_125_299_Closed_Image onclick="this.style.display='none'; Codehighlighter1_125_299_Closed_Text.style.display='none'; Codehighlighter1_125_299_Open_Image.style.display='inline'; Codehighlighter1_125_299_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_125_299_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_125_299_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">s;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">e;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(str[i]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">type;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;res</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">""</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</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">c;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;res</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">()</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;str</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">str.substr(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,s)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">res</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">str.substr(e,str.length()</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">e);<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;solve(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;s,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;e)<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img id=Codehighlighter1_325_692_Open_Image onclick="this.style.display='none'; Codehighlighter1_325_692_Open_Text.style.display='none'; Codehighlighter1_325_692_Closed_Image.style.display='inline'; Codehighlighter1_325_692_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_325_692_Closed_Image onclick="this.style.display='none'; Codehighlighter1_325_692_Closed_Text.style.display='none'; Codehighlighter1_325_692_Open_Image.style.display='inline'; Codehighlighter1_325_692_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_325_692_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_325_692_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(s</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">e)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img id=Codehighlighter1_358_690_Open_Image onclick="this.style.display='none'; Codehighlighter1_358_690_Open_Text.style.display='none'; Codehighlighter1_358_690_Closed_Image.style.display='inline'; Codehighlighter1_358_690_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_358_690_Closed_Image onclick="this.style.display='none'; Codehighlighter1_358_690_Closed_Text.style.display='none'; Codehighlighter1_358_690_Open_Image.style.display='inline'; Codehighlighter1_358_690_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_358_690_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_358_690_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;c</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,end;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(end</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;end</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">e</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">c;end</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000">(str[end])<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img id=Codehighlighter1_429_480_Open_Image onclick="this.style.display='none'; Codehighlighter1_429_480_Open_Text.style.display='none'; Codehighlighter1_429_480_Closed_Image.style.display='inline'; Codehighlighter1_429_480_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_429_480_Closed_Image onclick="this.style.display='none'; Codehighlighter1_429_480_Closed_Text.style.display='none'; Codehighlighter1_429_480_Open_Image.style.display='inline'; Codehighlighter1_429_480_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_429_480_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_429_480_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">:c</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">:c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(solve(end,e))&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(solve(s</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,end</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img id=Codehighlighter1_545_583_Open_Image onclick="this.style.display='none'; Codehighlighter1_545_583_Open_Text.style.display='none'; Codehighlighter1_545_583_Closed_Image.style.display='inline'; Codehighlighter1_545_583_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_545_583_Closed_Image onclick="this.style.display='none'; Codehighlighter1_545_583_Closed_Text.style.display='none'; Codehighlighter1_545_583_Open_Image.style.display='inline'; Codehighlighter1_545_583_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_545_583_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_545_583_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;make(end,e,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(end</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">e)<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img id=Codehighlighter1_605_666_Open_Image onclick="this.style.display='none'; Codehighlighter1_605_666_Open_Text.style.display='none'; Codehighlighter1_605_666_Closed_Image.style.display='inline'; Codehighlighter1_605_666_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_605_666_Closed_Image onclick="this.style.display='none'; Codehighlighter1_605_666_Closed_Text.style.display='none'; Codehighlighter1_605_666_Open_Image.style.display='inline'; Codehighlighter1_605_666_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_605_666_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_605_666_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;make(end,e,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;make(s</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,end</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">);<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img id=Codehighlighter1_705_991_Open_Image onclick="this.style.display='none'; Codehighlighter1_705_991_Open_Text.style.display='none'; Codehighlighter1_705_991_Closed_Image.style.display='inline'; Codehighlighter1_705_991_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_705_991_Closed_Image onclick="this.style.display='none'; Codehighlighter1_705_991_Closed_Text.style.display='none'; Codehighlighter1_705_991_Open_Image.style.display='inline'; Codehighlighter1_705_991_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_705_991_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_705_991_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;test;<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">test;<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(test</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img id=Codehighlighter1_746_978_Open_Image onclick="this.style.display='none'; Codehighlighter1_746_978_Open_Text.style.display='none'; Codehighlighter1_746_978_Closed_Image.style.display='inline'; Codehighlighter1_746_978_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_746_978_Closed_Image onclick="this.style.display='none'; Codehighlighter1_746_978_Closed_Text.style.display='none'; Codehighlighter1_746_978_Open_Image.style.display='inline'; Codehighlighter1_746_978_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_746_978_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_746_978_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str.clear();<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img id=Codehighlighter1_779_847_Open_Image onclick="this.style.display='none'; Codehighlighter1_779_847_Open_Text.style.display='none'; Codehighlighter1_779_847_Closed_Image.style.display='inline'; Codehighlighter1_779_847_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_779_847_Closed_Image onclick="this.style.display='none'; Codehighlighter1_779_847_Closed_Text.style.display='none'; Codehighlighter1_779_847_Open_Image.style.display='inline'; Codehighlighter1_779_847_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_779_847_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_779_847_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;tmp;<br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">tmp;<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">$</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">tmp;<br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">solve(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,str.length()))&nbsp;make(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,str.length(),</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">str.length();i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">str[i]</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">61</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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">endl;<br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<br></span></span></span></span></span></span>
<img src ="http://www.cppblog.com/yzhw/aggbug/139251.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-25 01:01 <a href="http://www.cppblog.com/yzhw/archive/2011/01/25/139251.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1332 Finding Liars DP 经典好题</title><link>http://www.cppblog.com/yzhw/archive/2011/01/24/139199.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sun, 23 Jan 2011 17:01:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/24/139199.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/139199.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/24/139199.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/139199.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/139199.html</trackback:ping><description><![CDATA[题意：<br>
<table style="WIDTH: 628px; BORDER-COLLAPSE: collapse; HEIGHT: 255px" border=1 cellSpacing=0 cellPadding=3>
    <tbody>
        <tr>
            <td><img border=0 alt="" src="http://poj.org/images/1332_1.jpg" width=260 height=228></td>
            <td vAlign=center align=middle><span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium Simsun; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="FONT-FAMILY: 'Times New Roman', Times, serif; FONT-SIZE: 16px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class=Apple-style-span>
            <table border=1 align=right>
                <tbody>
                    <tr>
                        <td>i (tester)</td>
                        <td>j (testee)/td&gt;</td>
                        <td>test outcome a<sub>i,j</sub></td>
                    </tr>
                    <tr>
                        <td>truth-teller</td>
                        <td>truth-teller</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>truth-teller</td>
                        <td>liar</td>
                        <td>1</td>
                    </tr>
                    <tr>
                        <td>liar</td>
                        <td>truth-teller</td>
                        <td style="WIDTH: 107px; HEIGHT: 24px">0 or 1</td>
                    </tr>
                    <tr>
                        <td>liar</td>
                        <td>liar</td>
                        <td>0 or 1</td>
                    </tr>
                </tbody>
            </table>
            <p></span></span><br class=Apple-interchange-newline>&nbsp;</p>
            </td>
        </tr>
    </tbody>
</table>
不用说了吧<br><br>解法：<br>这题我开始往2-sat的方向去想，搞死了搞不出来，今天洗澡的时候忽然发现。。这题原来是一个水了不能再水的DP<br>首先，设置DP状态(pos,left,jud)，即决策到第pos个人时还允许有left个人是骗子，并且第pos个人是诚实的/骗子时的状态。<br>状态是一个集合，我用的bitset。<br>关于状态转移就很简单了<br>分2种情况：<br>1、第pos个人是骗子，那么第pos+1个人是诚实的或者骗子都可以<br>2、第pos个人是诚实的，那么第pos+1个人只能根据第pos个人说的话来决定<br>第一个人的情况要枚举，并且最后特别判断下<br>还有就是，集合初始化为全1，然后状态的运算是与运算。注意滚动数组，不然MLE到你哭~<br><br>代码：<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#&nbsp;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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">bitset</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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;define&nbsp;N&nbsp;</span><span style="COLOR: #000000">1005</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">bitset</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">N</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;dp[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][N][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">],one,zero;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;data[N],n,maxnum;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img id=Codehighlighter1_160_1668_Open_Image onclick="this.style.display='none'; Codehighlighter1_160_1668_Open_Text.style.display='none'; Codehighlighter1_160_1668_Closed_Image.style.display='inline'; Codehighlighter1_160_1668_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_160_1668_Closed_Image onclick="this.style.display='none'; Codehighlighter1_160_1668_Closed_Text.style.display='none'; Codehighlighter1_160_1668_Open_Image.style.display='inline'; Codehighlighter1_160_1668_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_160_1668_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_160_1668_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;test;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">test);<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;one.</span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000">();<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;zero.reset();<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(test</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img id=Codehighlighter1_236_1655_Open_Image onclick="this.style.display='none'; Codehighlighter1_236_1655_Open_Text.style.display='none'; Codehighlighter1_236_1655_Closed_Image.style.display='inline'; Codehighlighter1_236_1655_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_236_1655_Closed_Image onclick="this.style.display='none'; Codehighlighter1_236_1655_Closed_Text.style.display='none'; Codehighlighter1_236_1655_Open_Image.style.display='inline'; Codehighlighter1_236_1655_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_236_1655_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_236_1655_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;one.</span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000">();<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zero.reset();<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">maxnum);<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #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">)<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,data</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i);<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitset</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">N</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;res</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">one;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">maxnum;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">zero,dp[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">one;<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">)<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img id=Codehighlighter1_457_846_Open_Image onclick="this.style.display='none'; Codehighlighter1_457_846_Open_Text.style.display='none'; Codehighlighter1_457_846_Closed_Image.style.display='inline'; Codehighlighter1_457_846_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_457_846_Closed_Image onclick="this.style.display='none'; Codehighlighter1_457_846_Closed_Text.style.display='none'; Codehighlighter1_457_846_Open_Image.style.display='inline'; Codehighlighter1_457_846_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_457_846_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_457_846_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">one;<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[(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">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[(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">2</span><span style="COLOR: #000000">][j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">].</span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000">(i);<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">data[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[(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">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[(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">2</span><span style="COLOR: #000000">][j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">].</span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000">(i);<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">maxnum;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[n</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">maxnum;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">one,dp[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">zero.</span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][maxnum][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][maxnum][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">one;<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">)<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img id=Codehighlighter1_1040_1429_Open_Image onclick="this.style.display='none'; Codehighlighter1_1040_1429_Open_Text.style.display='none'; Codehighlighter1_1040_1429_Closed_Image.style.display='inline'; Codehighlighter1_1040_1429_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1040_1429_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1040_1429_Closed_Text.style.display='none'; Codehighlighter1_1040_1429_Open_Image.style.display='inline'; Codehighlighter1_1040_1429_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1040_1429_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1040_1429_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">one;<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[(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">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[(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">2</span><span style="COLOR: #000000">][j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">].</span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000">(i);<br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">data[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])<br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[(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">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">maxnum;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][j][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[(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">2</span><span style="COLOR: #000000">][j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">].</span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000">(i);<br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">maxnum;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">61</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res</span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000">dp[n</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(res.count()</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">0&nbsp;0\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">65</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img id=Codehighlighter1_1532_1652_Open_Image onclick="this.style.display='none'; Codehighlighter1_1532_1652_Open_Text.style.display='none'; Codehighlighter1_1532_1652_Closed_Image.style.display='inline'; Codehighlighter1_1532_1652_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1532_1652_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1532_1652_Closed_Text.style.display='none'; Codehighlighter1_1532_1652_Open_Image.style.display='inline'; Codehighlighter1_1532_1652_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1532_1652_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1532_1652_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">67</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;pos</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">pos</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">)<br></span><span style="COLOR: #008080">69</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(res[i])<br></span><span style="COLOR: #008080">70</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">71</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d&nbsp;%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,res.count(),pos</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">72</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">73</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">74</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">75</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<br>
<img src ="http://www.cppblog.com/yzhw/aggbug/139199.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-24 01:01 <a href="http://www.cppblog.com/yzhw/archive/2011/01/24/139199.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1337 A Lazy Worker 很诡异的DP</title><link>http://www.cppblog.com/yzhw/archive/2011/01/23/139143.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sat, 22 Jan 2011 18:44:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/23/139143.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/139143.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/23/139143.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/139143.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/139143.html</trackback:ping><description><![CDATA[题意：<br>很多人这题题目读不懂，我详细解释下：一个懒工人，他想工作的时间尽量少。有N个任务，每个任务有开始时间和deadline，工人完成这个工作需要ti时间。如果某个时刻有工作可以做（这里是说，如果从这个时刻开始某个工作，在deadline之前能够完成)，那么这个工人必须做，但是如果这个时刻存在着多余1件工作可以做，工人可以选择；假设这个时刻没有工作可以做了，工人就可以偷懒直到有新的任务到来。<br><br>解法：<br>这题初看觉得很麻烦，但是题目中有句话太给力了！<meta http-equiv="content-type" content="text/html; charset=utf-8"><span  style="font-family: 'Times New Roman', Times, serif; font-size: 16px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&nbsp;You should note that for each job i, the length of Ii, di - ai, is greater than or equal to ti, but less than 2*ti<br><meta http-equiv="content-type" content="text/html; charset=utf-8"><span  style="font-family: 'AR PL UKai CN'; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; font-size: medium; ">这句话把后效性全部消除了，即不可能出现这种情况，在t1这个决策点出现了第k个工作，在t1后的某个决策点t2又出现了第k个工作，明白了？<br>DP方程：<br>dp[t]=min(work[i]+dp[t+work[i]]) work指在第t个时间点能够可以开始做的工作<br>如果在第t个时刻没有任务可做，那么dp[t]=dp[t+1]<br><br>代码：<br><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">============================================================================<br></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #008000; "></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;pku1337.cpp<br></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #008000; "></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;yzhw<br></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #008000; "></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:<br></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #008000; "></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Copyright&nbsp;&nbsp;&nbsp;:&nbsp;yzhw<br></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #008000; "></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Description&nbsp;:&nbsp;Hello&nbsp;World&nbsp;in&nbsp;C++,&nbsp;Ansi-style<br></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #008000; "></span><span style="color: #008000; ">//</span><span style="color: #008000; ">============================================================================</span><span style="color: #008000; "><br></span><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; "><br></span><span style="color: #008080; ">&nbsp;9</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; ">10</span>&nbsp;<span style="color: #000000; ">#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">algorithm</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #008080; ">11</span>&nbsp;<span style="color: #000000; ">#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; ">12</span>&nbsp;<span style="color: #000000; ">#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">cstring</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br></span><span style="color: #008080; ">13</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; ">14</span>&nbsp;<span style="color: #000000; ">vector</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;work[</span><span style="color: #000000; ">251</span><span style="color: #000000; ">];<br></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;data[</span><span style="color: #000000; ">101</span><span style="color: #000000; ">][</span><span style="color: #000000; ">3</span><span style="color: #000000; ">],n,minnum,maxnum,dp[</span><span style="color: #000000; ">300</span><span style="color: #000000; ">];<br></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;getdp(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;t)<br></span><span style="color: #008080; ">17</span>&nbsp;<span style="color: #000000; ">{<br></span><span style="color: #008080; ">18</span>&nbsp;<span style="color: #000000; ">&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; ">maxnum)&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; ">19</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;dp[t];<br></span><span style="color: #008080; ">20</span>&nbsp;<span style="color: #000000; ">}<br></span><span style="color: #008080; ">21</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main()&nbsp;{<br></span><span style="color: #008080; ">22</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;test;<br></span><span style="color: #008080; ">23</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000; ">&gt;&gt;</span><span style="color: #000000; ">test;<br></span><span style="color: #008080; ">24</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(test</span><span style="color: #000000; ">--</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">25</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080; ">26</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000; ">&gt;&gt;</span><span style="color: #000000; ">n;<br></span><span style="color: #008080; ">27</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minnum</span><span style="color: #000000; ">=</span><span style="color: #000000; ">300</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">28</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxnum</span><span style="color: #000000; ">=-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br></span><span style="color: #008080; ">29</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;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; ">250</span><span style="color: #000000; ">;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">30</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;work[i].clear();<br></span><span style="color: #008080; ">31</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;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; ">)<br></span><span style="color: #008080; ">32</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080; ">33</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000; ">&gt;&gt;</span><span style="color: #000000; ">data[i][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]</span><span style="color: #000000; ">&gt;&gt;</span><span style="color: #000000; ">data[i][</span><span style="color: #000000; ">1</span><span style="color: #000000; ">]</span><span style="color: #000000; ">&gt;&gt;</span><span style="color: #000000; ">data[i][</span><span style="color: #000000; ">2</span><span style="color: #000000; ">];<br></span><span style="color: #008080; ">34</span>&nbsp;<span style="color: #000000; ">&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;j</span><span style="color: #000000; ">=</span><span style="color: #000000; ">data[i][</span><span style="color: #000000; ">1</span><span style="color: #000000; ">];j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">data[i][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]</span><span style="color: #000000; ">&lt;=</span><span style="color: #000000; ">data[i][</span><span style="color: #000000; ">2</span><span style="color: #000000; ">];j</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">35</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;work[j].push_back(i);<br></span><span style="color: #008080; ">36</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minnum</span><span style="color: #000000; ">=</span><span style="color: #000000; ">min(minnum,data[i][</span><span style="color: #000000; ">1</span><span style="color: #000000; ">]);<br></span><span style="color: #008080; ">37</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxnum</span><span style="color: #000000; ">=</span><span style="color: #000000; ">max(maxnum,data[i][</span><span style="color: #000000; ">2</span><span style="color: #000000; ">]</span><span style="color: #000000; ">-</span><span style="color: #000000; ">data[i][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]);<br></span><span style="color: #008080; ">38</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080; ">39</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(dp));<br></span><span style="color: #008080; ">40</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;t</span><span style="color: #000000; ">=</span><span style="color: #000000; ">maxnum;t</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">minnum;t</span><span style="color: #000000; ">--</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">41</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(work[t].empty())<br></span><span style="color: #008080; ">42</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[t]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">dp[t</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">];<br></span><span style="color: #008080; ">43</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br></span><span style="color: #008080; ">44</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080; ">45</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[t]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">data[work[t][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]</span><span style="color: #000000; ">+</span><span style="color: #000000; ">getdp(data[work[t][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]</span><span style="color: #000000; ">+</span><span style="color: #000000; ">t);<br></span><span style="color: #008080; ">46</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</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; ">work[t].size();i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br></span><span style="color: #008080; ">47</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[t]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">min(dp[t],data[work[t][i]][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]</span><span style="color: #000000; ">+</span><span style="color: #000000; ">getdp(data[work[t][i]][</span><span style="color: #000000; ">0</span><span style="color: #000000; ">]</span><span style="color: #000000; ">+</span><span style="color: #000000; ">t));<br></span><span style="color: #008080; ">48</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080; ">49</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; ">dp[minnum]</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br></span><span style="color: #008080; ">50</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080; ">51</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; ">52</span>&nbsp;<span style="color: #000000; ">}<br></span><span style="color: #008080; ">53</span>&nbsp;<span style="color: #000000; "></span></div></span></span><img src ="http://www.cppblog.com/yzhw/aggbug/139143.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-23 02:44 <a href="http://www.cppblog.com/yzhw/archive/2011/01/23/139143.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1241 Knockout Tournament 树形DP，很好的题目</title><link>http://www.cppblog.com/yzhw/archive/2011/01/20/138937.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Wed, 19 Jan 2011 17:44:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/20/138937.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/138937.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/20/138937.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/138937.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/138937.html</trackback:ping><description><![CDATA[题意：<br><img border=0 hspace=0 alt="" align=baseline src="http://poj.org/images/1241_1.jpg" longDesc=""><br>看图说话，一棵锦标赛树（具体不描述，看DS书去），问每个队员的最好名次和最坏名次<br><br>分析：<br>求最好名次应该从上向下划分状态，而求最坏名次从下向上划分状态，举个例子吧，看第三排第一个1，它的最坏名次是第四排第一个1的最坏名次加上它的1^n-右孩子的最坏名次-1，而它的最好名次则是第二排第一个1的最好名次，它旁边那个3的最好名次就是3的父节点（1）的最好名次+1，大概明白什么意思了吧？看代码吧～。<br><br>代码：<br>
<div style="PADDING-BOTTOM: 4px; BACKGROUND-COLOR: rgb(238,238,238); PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; BORDER-LEFT-COLOR: rgb(204,204,204); WORD-BREAK: break-all; PADDING-TOP: 4px"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #000000">#&nbsp;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>&nbsp;<span style="COLOR: #000000">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</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;4</span>&nbsp;<span style="COLOR: #000000">#&nbsp;define&nbsp;max(a,b)&nbsp;((a)</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">(b)</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">(a):(b))<br></span><span style="COLOR: #008080">&nbsp;5</span>&nbsp;<span style="COLOR: #000000">#&nbsp;define&nbsp;min(a,b)&nbsp;((a)</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">(b)</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">(a):(b))<br></span><span style="COLOR: #008080">&nbsp;6</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tree[</span><span style="COLOR: #000000">1024</span><span style="COLOR: #000000">],n;<br></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;worst[</span><span style="COLOR: #000000">1024</span><span style="COLOR: #000000">],best[</span><span style="COLOR: #000000">1024</span><span style="COLOR: #000000">],high[</span><span style="COLOR: #000000">1024</span><span style="COLOR: #000000">],low[</span><span style="COLOR: #000000">1024</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;8</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">&nbsp;9</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">n)&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(n</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">)<br></span><span style="COLOR: #008080">15</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n),worst[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n;<br></span><span style="COLOR: #008080">16</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;t</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">;t</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;t</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">17</span>&nbsp;<span style="COLOR: #000000">&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;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">t;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(t</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">)<br></span><span style="COLOR: #008080">18</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="COLOR: #008080">19</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,tree</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i);<br></span><span style="COLOR: #008080">20</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;worst[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(tree[i]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">tree[i</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">worst[i</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">worst[(i</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]:worst[(i</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">worst[i</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">21</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">22</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;best[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(n</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">)<br></span><span style="COLOR: #008080">24</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;best[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(tree[i]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">tree[i</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">best[i</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]:best[i</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">25</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;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">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(n</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">)<br></span><span style="COLOR: #008080">26</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;high[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">low[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n;<br></span><span style="COLOR: #008080">27</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;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">1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">(n</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">)<br></span><span style="COLOR: #008080">28</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="COLOR: #008080">29</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;high[tree[i]]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(high[tree[i]],best[i]);<br></span><span style="COLOR: #008080">30</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;low[tree[i]]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(low[tree[i]],worst[i]);<br></span><span style="COLOR: #008080">31</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">32</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br></span><span style="COLOR: #008080">33</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">(n</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">34</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="COLOR: #008080">35</span>&nbsp;<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;<br></span><span style="COLOR: #008080">36</span>&nbsp;<span style="COLOR: #000000">&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">t);<br></span><span style="COLOR: #008080">37</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Player&nbsp;%d&nbsp;can&nbsp;be&nbsp;ranked&nbsp;as&nbsp;high&nbsp;as&nbsp;%d&nbsp;or&nbsp;as&nbsp;low&nbsp;as&nbsp;%d.\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,t,high[t],low[t]);<br></span><span style="COLOR: #008080">38</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">39</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">40</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">41</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">42</span>&nbsp;<span style="COLOR: #000000">}</span></div>
<br><br>
<img src ="http://www.cppblog.com/yzhw/aggbug/138937.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-20 01:44 <a href="http://www.cppblog.com/yzhw/archive/2011/01/20/138937.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1243 One Person 想法很牛的一道DP</title><link>http://www.cppblog.com/yzhw/archive/2011/01/20/138936.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Wed, 19 Jan 2011 17:36:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/20/138936.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/138936.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/20/138936.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/138936.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/138936.html</trackback:ping><description><![CDATA[想了半天，没想出来，么办法，直接百度，先原样引用我参考的一篇文章<br><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><span  style="line-height: 25px; color: rgb(93, 104, 129); font-family: Verdana, Arial, Helvetica, 宋体, sans-serif; font-size: 14px; "><p style="line-height: 25px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; ">【题目大意】猜数字的游戏，可以猜N次，且只能猜大L次，求在一定的策略下，能猜到的最大的数。</p><p style="line-height: 25px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; ">----------------------------------------------------------<br style="line-height: 25px; ">【分析】：很诡异的动态规划，看别人的分析：</p><p style="line-height: 25px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; ">1.先考虑L=0的情况，由于一次都不能猜错，因此只能从1开始，逐个向上猜，总数不能超过G；<br style="line-height: 25px; ">&nbsp;&nbsp;&nbsp; 2.若G&lt;=L，等价于G=L的情况，因为此时允许猜的次数限制了最大的数，总数为2^G<br style="line-height: 25px; ">&nbsp;&nbsp;&nbsp; 3.一般的情况，比如L=1时，可以设想，只能猜错一次的话，这个数不能太大，否则猜错了一点作用也没有，因此要确保即使本次猜错也能在以后的G-1次猜中，这就转化成了(G-1,0)的情况，也就是说必胜的策略应当为先猜G，如果错，则从1~G-1逐次的猜，如果猜对了就转化成了(G-1,1)的情况，他应当再猜G+G-1&#8230;&#8230;对于G=i,L=j的情况也是一样的考虑，可以列出状态转移方程：<br style="line-height: 25px; ">opt[i][0]= i;<br style="line-height: 25px; ">opt[i][i]= 2^i-1;<br style="line-height: 25px; ">opt[i][j]= opt[i-1][j]+ opt[i-1][j-1]+ 1;<br style="line-height: 25px; "></p><p align="center" style="line-height: 25px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font color="#333399" size="4" style="line-height: 32px; ">Source Code</font></p><p style="line-height: 25px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "></p><table align="center" style="line-height: 23px; color: rgb(93, 104, 129); font: normal normal normal 12px/normal Verdana, Arial, Helvetica, 宋体, sans-serif; font-size: 10pt; "><tbody style="line-height: 23px; "><tr style="line-height: 23px; "><td style="line-height: 23px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><strong style="line-height: 23px; font-weight: bold; ">Problem:</strong>&nbsp;<a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1243" style="line-height: 21px; color: rgb(93, 104, 129); font: normal normal normal 12px/normal Verdana, Arial, Helvetica, 宋体, sans-serif; text-decoration: underline; ">1243</a></td><td style="line-height: 23px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "></td><td style="line-height: 23px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><strong style="line-height: 23px; font-weight: bold; ">User:</strong>&nbsp;<a href="http://acm.pku.edu.cn/JudgeOnline/userstatus?user_id=Sasuke_SCUT" style="line-height: 21px; color: rgb(93, 104, 129); font: normal normal normal 12px/normal Verdana, Arial, Helvetica, 宋体, sans-serif; text-decoration: underline; ">Sasuke_SCUT</a></td></tr><tr style="line-height: 23px; "><td style="line-height: 23px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><strong style="line-height: 23px; font-weight: bold; ">Memory:</strong>&nbsp;380K</td><td style="line-height: 23px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "></td><td style="line-height: 23px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><strong style="line-height: 23px; font-weight: bold; ">Time:</strong>&nbsp;0MS</td></tr><tr style="line-height: 23px; "><td style="line-height: 23px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><strong style="line-height: 23px; font-weight: bold; ">Language:</strong>&nbsp;G++</td><td style="line-height: 23px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "></td><td style="line-height: 23px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><strong style="line-height: 23px; font-weight: bold; ">Result:</strong>&nbsp;<font color="blue" style="line-height: 23px; ">Accepted</font></td></tr></tbody></table><p style="line-height: 25px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "></p><ul style="line-height: 25px; "><li style="line-height: 25px; border-top-color: rgb(168, 190, 239) !important; border-right-color: rgb(168, 190, 239) !important; border-bottom-color: rgb(168, 190, 239) !important; border-left-color: rgb(168, 190, 239) !important; "><font color="#333399" size="5" style="line-height: 43px; ">Source Code</font><pre style="line-height: 25px; font-family: 'Courier New', Courier, monospace; "><span style="line-height: 25px; ">#include</span><span style="line-height: 25px; ">&lt;cstdio&gt;</span>
<span style="line-height: 25px; ">#include</span><span style="line-height: 25px; ">&lt;string.h&gt;</span>
<span style="line-height: 25px; ">#include</span><span style="line-height: 25px; ">&lt;math.h&gt;</span>
<span style="line-height: 25px; ">int</span> <span style="line-height: 25px; ">main</span><span style="line-height: 25px; ">()</span>
<span style="line-height: 25px; ">{</span>
	<span style="line-height: 25px; ">int</span> G<span style="line-height: 25px; ">,</span>L<span style="line-height: 25px; ">,</span>i<span style="line-height: 25px; ">,</span>j<span style="line-height: 25px; ">,</span>T<span style="line-height: 25px; ">,</span>dp<span style="line-height: 25px; ">[</span><span style="line-height: 25px; ">31</span><span style="line-height: 25px; ">][</span><span style="line-height: 25px; ">31</span><span style="line-height: 25px; ">];</span>
	T<span style="line-height: 25px; ">=</span><span style="line-height: 25px; ">1</span><span style="line-height: 25px; ">;</span>
	<span style="line-height: 25px; ">while</span><span style="line-height: 25px; ">(</span><span style="line-height: 25px; ">scanf</span><span style="line-height: 25px; ">(</span><span style="line-height: 25px; ">"%d %d"</span><span style="line-height: 25px; ">,&amp;</span>G<span style="line-height: 25px; ">,&amp;</span>L<span style="line-height: 25px; ">)</span> <span style="line-height: 25px; ">&amp;&amp;</span> G<span style="line-height: 25px; ">)</span>
	<span style="line-height: 25px; ">{</span>
		<span style="line-height: 25px; ">memset</span><span style="line-height: 25px; ">(</span>dp<span style="line-height: 25px; ">,</span><span style="line-height: 25px; ">0</span><span style="line-height: 25px; ">,</span><span style="line-height: 25px; ">sizeof</span> dp<span style="line-height: 25px; ">);</span>
		dp<span style="line-height: 25px; ">[</span><span style="line-height: 25px; ">0</span><span style="line-height: 25px; ">][</span><span style="line-height: 25px; ">0</span><span style="line-height: 25px; ">]=</span><span style="line-height: 25px; ">1</span><span style="line-height: 25px; ">;</span>
		<span style="line-height: 25px; ">for</span><span style="line-height: 25px; ">(</span>i<span style="line-height: 25px; ">=</span><span style="line-height: 25px; ">1</span><span style="line-height: 25px; ">;</span>i<span style="line-height: 25px; ">&lt;=</span>G<span style="line-height: 25px; ">;</span>i<span style="line-height: 25px; ">++)</span>
		<span style="line-height: 25px; ">{</span>
			dp<span style="line-height: 25px; ">[</span>i<span style="line-height: 25px; ">][</span><span style="line-height: 25px; ">0</span><span style="line-height: 25px; ">]=</span>i<span style="line-height: 25px; ">;</span>
			<span style="line-height: 25px; ">for</span><span style="line-height: 25px; ">(</span>j<span style="line-height: 25px; ">=</span><span style="line-height: 25px; ">1</span><span style="line-height: 25px; ">;</span>j<span style="line-height: 25px; ">&lt;=</span>L<span style="line-height: 25px; ">;</span>j<span style="line-height: 25px; ">++)</span>
				<span style="line-height: 25px; ">if</span><span style="line-height: 25px; ">(</span>j<span style="line-height: 25px; ">&gt;=</span>i<span style="line-height: 25px; ">)</span>
					dp<span style="line-height: 25px; ">[</span>i<span style="line-height: 25px; ">][</span>j<span style="line-height: 25px; ">]=</span><span style="line-height: 25px; ">pow</span><span style="line-height: 25px; ">(</span><span style="line-height: 25px; ">2.0</span><span style="line-height: 25px; ">,</span>i<span style="line-height: 25px; ">)-</span><span style="line-height: 25px; ">1</span><span style="line-height: 25px; ">;</span>
				<span style="line-height: 25px; ">else</span>
					dp<span style="line-height: 25px; ">[</span>i<span style="line-height: 25px; ">][</span>j<span style="line-height: 25px; ">]=</span>dp<span style="line-height: 25px; ">[</span>i<span style="line-height: 25px; ">-1</span><span style="line-height: 25px; ">][</span>j<span style="line-height: 25px; ">]+</span><span style="line-height: 25px; ">1</span><span style="line-height: 25px; ">+</span>dp<span style="line-height: 25px; ">[</span>i<span style="line-height: 25px; ">-1</span><span style="line-height: 25px; ">][</span>j<span style="line-height: 25px; ">-1</span><span style="line-height: 25px; ">];</span>
		<span style="line-height: 25px; ">}</span>
		<span style="line-height: 25px; ">printf</span><span style="line-height: 25px; ">(</span><span style="line-height: 25px; ">"Case %d: %d</span><span style="line-height: 25px; ">\n</span><span style="line-height: 25px; ">"</span><span style="line-height: 25px; ">,</span>T<span style="line-height: 25px; ">++,</span>dp<span style="line-height: 25px; ">[</span>G<span style="line-height: 25px; ">][</span>L<span style="line-height: 25px; ">]);</span>
	<span style="line-height: 25px; ">}</span>
	<span style="line-height: 25px; ">return</span> <span style="line-height: 25px; ">0</span><span style="line-height: 25px; ">;</span>
<span style="line-height: 25px; ">}</span></pre></li></ul></span></div><img src ="http://www.cppblog.com/yzhw/aggbug/138936.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-20 01:36 <a href="http://www.cppblog.com/yzhw/archive/2011/01/20/138936.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1205 Water Treatment Plants 递推（说DP也可以把。。）</title><link>http://www.cppblog.com/yzhw/archive/2011/01/15/138575.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sat, 15 Jan 2011 09:22:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/15/138575.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/138575.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/15/138575.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/138575.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/138575.html</trackback:ping><description><![CDATA[题意：<br>一个污水处理系统吗，n个城市，每个城市可以选择<br>1、将左边城市过来的污水和右边城市过来的污水连同本身的污水排到河里<br>2、将左边来的污水连同自己的污水排到右边<br>3、将右边来的污水连同自己的污水排到左边<br><img border=0 alt="" src="http://poj.org/images/1205_1.jpg" width=241 height=128><br><br>解法：<br>设状态dp[i][0]为第i个城市选择将污水传到左边的方案数，dp[i][1]为第i个城市选择将污水排入河道的方案数，dp[i][2]为选择将污水排到右边城市的方案数<br>然后有递推式<br>dp[i][2]=dp[i][1]=sum(dp[i-1][j]),j=0,1,2<br>dp[i][0]=dp[i-1][0]+dp[i-1][1]<br>这个应该不难理解吧？<br>如果最后一个城市选择后两种方案，那么前面城市怎么连都无所谓<br>而最后一个城市选择第一个方案，那么第n-1个城市不能选择将污水排到第n个城市<br>注意初始条件，dp[1][0]=0,dp[1][1]=dp[1][2]=1;<br>然后就是java&nbsp; BigInteger ，嘻嘻<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.math.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img id=Codehighlighter1_56_664_Open_Image onclick="this.style.display='none'; Codehighlighter1_56_664_Open_Text.style.display='none'; Codehighlighter1_56_664_Closed_Image.style.display='inline'; Codehighlighter1_56_664_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_56_664_Closed_Image onclick="this.style.display='none'; Codehighlighter1_56_664_Closed_Text.style.display='none'; Codehighlighter1_56_664_Open_Image.style.display='inline'; Codehighlighter1_56_664_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Main&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_56_664_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_56_664_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img id=Codehighlighter1_60_83_Open_Image onclick="this.style.display='none'; Codehighlighter1_60_83_Open_Text.style.display='none'; Codehighlighter1_60_83_Closed_Image.style.display='inline'; Codehighlighter1_60_83_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_60_83_Closed_Image onclick="this.style.display='none'; Codehighlighter1_60_83_Closed_Text.style.display='none'; Codehighlighter1_60_83_Open_Image.style.display='inline'; Codehighlighter1_60_83_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_60_83_Closed_Text>/**&nbsp;*/</span><span id=Codehighlighter1_60_83_Open_Text><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000">&nbsp;args<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_143_658_Open_Image onclick="this.style.display='none'; Codehighlighter1_143_658_Open_Text.style.display='none'; Codehighlighter1_143_658_Closed_Image.style.display='inline'; Codehighlighter1_143_658_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_143_658_Closed_Image onclick="this.style.display='none'; Codehighlighter1_143_658_Closed_Text.style.display='none'; Codehighlighter1_143_658_Open_Image.style.display='inline'; Codehighlighter1_143_658_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000">&nbsp;IOException</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_143_658_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_143_658_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StreamTokenizer&nbsp;in</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;StreamTokenizer(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;BufferedReader(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;InputStreamReader(System.in)));<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BigInteger&nbsp;dp[][]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;BigInteger[</span><span style="COLOR: #000000">101</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">BigInteger.ZERO;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</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">BigInteger.ONE;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">BigInteger.ONE;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img id=Codehighlighter1_395_551_Open_Image onclick="this.style.display='none'; Codehighlighter1_395_551_Open_Text.style.display='none'; Codehighlighter1_395_551_Closed_Image.style.display='inline'; Codehighlighter1_395_551_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_395_551_Closed_Image onclick="this.style.display='none'; Codehighlighter1_395_551_Closed_Text.style.display='none'; Codehighlighter1_395_551_Open_Image.style.display='inline'; Codehighlighter1_395_551_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_395_551_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_395_551_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">].add(dp[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">].add(dp[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]));<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">].add(dp[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">]);<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">].add(dp[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">].add(dp[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]));<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(in.nextToken()</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">in.TT_EOF)<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(dp[(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">)in.nval][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">].add(dp[(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">)in.nval][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]));<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/138575.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-15 17:22 <a href="http://www.cppblog.com/yzhw/archive/2011/01/15/138575.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1625 Censored! 自动机经典题，手写高精度WA了5次。。。。。</title><link>http://www.cppblog.com/yzhw/archive/2011/01/13/138432.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Thu, 13 Jan 2011 01:57:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/13/138432.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/138432.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/13/138432.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/138432.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/138432.html</trackback:ping><description><![CDATA[题意：<br>问不包含n个子串的长度为m的字符串的构造个数。<br><br>解法：<br>构造trie图，然后DP求长度为m的合法串个数<br>以前高精度都靠java，这次手写，各种错误。。。唉。。<br><br>代码：<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<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;">#&nbsp;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;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">cstring</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;&nbsp;3</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;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;BigInteger<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;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;bit[</span><span style="color: #000000;">100</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;init;<br></span><span style="color: #008080;">&nbsp;&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;BigInteger()<br></span><span style="color: #008080;">&nbsp;&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(bit,</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(bit));<br></span><span style="color: #008080;">&nbsp;11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;BigInteger&nbsp;</span><span style="color: #0000ff;">operator</span><span style="color: #000000;">+</span><span style="color: #000000;">(</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;BigInteger&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">pos)<br></span><span style="color: #008080;">&nbsp;14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BigInteger&nbsp;res;<br></span><span style="color: #008080;">&nbsp;16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.init</span><span style="color: #000000;">=</span><span style="color: #000000;">init;<br></span><span style="color: #008080;">&nbsp;17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;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;">99</span><span style="color: #000000;">;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.bit[i]</span><span style="color: #000000;">+=</span><span style="color: #000000;">bit[i]</span><span style="color: #000000;">+</span><span style="color: #000000;">pos.bit[i];<br></span><span style="color: #008080;">&nbsp;20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.bit[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;">res.bit[i]</span><span style="color: #000000;">/</span><span style="color: #000000;">10</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.bit[i]</span><span style="color: #000000;">%=</span><span style="color: #000000;">10</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;res;<br></span><span style="color: #008080;">&nbsp;24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;print()<br></span><span style="color: #008080;">&nbsp;26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i;<br></span><span style="color: #008080;">&nbsp;28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(i</span><span style="color: #000000;">=</span><span style="color: #000000;">99</span><span style="color: #000000;">;i</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">0</span><span style="color: #000000;">&amp;&amp;!</span><span style="color: #000000;">bit[i];i</span><span style="color: #000000;">--</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;j</span><span style="color: #000000;">=</span><span style="color: #000000;">i;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;">)<br></span><span style="color: #008080;">&nbsp;30</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;">bit[j];<br></span><span style="color: #008080;">&nbsp;31</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;">endl;<br></span><span style="color: #008080;">&nbsp;32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;33</span>&nbsp;<span style="color: #000000;">};<br></span><span style="color: #008080;">&nbsp;34</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;node<br></span><span style="color: #008080;">&nbsp;35</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;36</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">nxt[</span><span style="color: #000000;">51</span><span style="color: #000000;">],</span><span style="color: #000000;">*</span><span style="color: #000000;">pre;<br></span><span style="color: #008080;">&nbsp;37</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;end;<br></span><span style="color: #008080;">&nbsp;38</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;clear()<br></span><span style="color: #008080;">&nbsp;39</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;40</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(nxt,NULL,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(nxt));<br></span><span style="color: #008080;">&nbsp;41</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end</span><span style="color: #000000;">=</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;42</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;">NULL;<br></span><span style="color: #008080;">&nbsp;43</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;44</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;node()<br></span><span style="color: #008080;">&nbsp;45</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;46</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clear();<br></span><span style="color: #008080;">&nbsp;47</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;48</span>&nbsp;<span style="color: #000000;">}buffer[</span><span style="color: #000000;">200</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;49</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;map[</span><span style="color: #000000;">255</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;50</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;c</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">,n,m,num;<br></span><span style="color: #008080;">&nbsp;51</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;insert(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">str)<br></span><span style="color: #008080;">&nbsp;52</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;53</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">p</span><span style="color: #000000;">=</span><span style="color: #000000;">buffer;<br></span><span style="color: #008080;">&nbsp;54</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;">0</span><span style="color: #000000;">;str[i]</span><span style="color: #000000;">!=</span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;55</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;56</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;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">(p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[map[str[i]]]))<br></span><span style="color: #008080;">&nbsp;57</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[map[str[i]]]</span><span style="color: #000000;">=&amp;</span><span style="color: #000000;">buffer[c</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;58</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">=</span><span style="color: #000000;">p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[map[str[i]]];<br></span><span style="color: #008080;">&nbsp;59</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;60</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">end</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;61</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;62</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;make_per()<br></span><span style="color: #008080;">&nbsp;63</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;64</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;s</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">,e</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;65</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">q[</span><span style="color: #000000;">200</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;66</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">p</span><span style="color: #000000;">=</span><span style="color: #000000;">buffer;<br></span><span style="color: #008080;">&nbsp;67</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;">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;">)<br></span><span style="color: #008080;">&nbsp;68</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;">(p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i])<br></span><span style="color: #008080;">&nbsp;69</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;70</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i]</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">pre</span><span style="color: #000000;">=</span><span style="color: #000000;">p;<br></span><span style="color: #008080;">&nbsp;71</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q[</span><span style="color: #000000;">++</span><span style="color: #000000;">e]</span><span style="color: #000000;">=</span><span style="color: #000000;">p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i];<br></span><span style="color: #008080;">&nbsp;72</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;73</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;74</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">p;<br></span><span style="color: #008080;">&nbsp;75</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(s</span><span style="color: #000000;">!=</span><span style="color: #000000;">e)<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;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">=</span><span style="color: #000000;">q[</span><span style="color: #000000;">++</span><span style="color: #000000;">s];<br></span><span style="color: #008080;">&nbsp;78</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;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;">)<br></span><span style="color: #008080;">&nbsp;79</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;80</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">pre</span><span style="color: #000000;">=</span><span style="color: #000000;">p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">pre;<br></span><span style="color: #008080;">&nbsp;81</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;">(pre</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i]</span><span style="color: #000000;">==</span><span style="color: #000000;">NULL)&nbsp;pre</span><span style="color: #000000;">=</span><span style="color: #000000;">pre</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">pre;<br></span><span style="color: #008080;">&nbsp;82</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;">(p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i])<br></span><span style="color: #008080;">&nbsp;83</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;84</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i]</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">pre</span><span style="color: #000000;">=</span><span style="color: #000000;">pre</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i];<br></span><span style="color: #008080;">&nbsp;85</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i]</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">end</span><span style="color: #000000;">=</span><span style="color: #000000;">(p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i]</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">pre</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">end</span><span style="color: #000000;">||</span><span style="color: #000000;">p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i]</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">end);<br></span><span style="color: #008080;">&nbsp;86</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q[</span><span style="color: #000000;">++</span><span style="color: #000000;">e]</span><span style="color: #000000;">=</span><span style="color: #000000;">p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i];<br></span><span style="color: #008080;">&nbsp;87</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;88</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;89</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">pre</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i];<br></span><span style="color: #008080;">&nbsp;90</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;91</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;92</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;93</span>&nbsp;<span style="color: #000000;">BigInteger&nbsp;dp[</span><span style="color: #000000;">200</span><span style="color: #000000;">][</span><span style="color: #000000;">55</span><span style="color: #000000;">],zero,one;<br></span><span style="color: #008080;">&nbsp;94</span>&nbsp;<span style="color: #000000;">BigInteger&nbsp;solve(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;s,node&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">p)<br></span><span style="color: #008080;">&nbsp;95</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;96</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">end)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;zero;<br></span><span style="color: #008080;">&nbsp;97</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(s</span><span style="color: #000000;">==</span><span style="color: #000000;">m)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;one;<br></span><span style="color: #008080;">&nbsp;98</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">(dp[p</span><span style="color: #000000;">-</span><span style="color: #000000;">buffer][s].init))&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;dp[p</span><span style="color: #000000;">-</span><span style="color: #000000;">buffer][s];<br></span><span style="color: #008080;">&nbsp;99</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;">100</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">101</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[p</span><span style="color: #000000;">-</span><span style="color: #000000;">buffer][s].init</span><span style="color: #000000;">=</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br></span><span style="color: #008080;">102</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;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;">)<br></span><span style="color: #008080;">103</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<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;dp[p</span><span style="color: #000000;">-</span><span style="color: #000000;">buffer][s]</span><span style="color: #000000;">=</span><span style="color: #000000;">dp[p</span><span style="color: #000000;">-</span><span style="color: #000000;">buffer][s]</span><span style="color: #000000;">+</span><span style="color: #000000;">solve(s</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,p</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">nxt[i]);<br></span><span style="color: #008080;">105</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">106</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;dp[p</span><span style="color: #000000;">-</span><span style="color: #000000;">buffer][s];<br></span><span style="color: #008080;">107</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">108</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">109</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">110</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;">111</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">112</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cin</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</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">num;<br></span><span style="color: #008080;">113</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;str[</span><span style="color: #000000;">128</span><span style="color: #000000;">];<br></span><span style="color: #008080;">114</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">str;<br></span><span style="color: #008080;">115</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;tc</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">116</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;">0</span><span style="color: #000000;">;str[i]</span><span style="color: #000000;">!=</span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">117</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map[str[i]]</span><span style="color: #000000;">=</span><span style="color: #000000;">tc</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">118</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(num</span><span style="color: #000000;">--</span><span style="color: #000000;">)<br></span><span style="color: #008080;">119</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">120</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">str;<br></span><span style="color: #008080;">121</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(str);<br></span><span style="color: #008080;">122</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">123</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;make_per();<br></span><span style="color: #008080;">124</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;one.bit[</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">;;<br></span><span style="color: #008080;">125</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;solve(</span><span style="color: #000000;">0</span><span style="color: #000000;">,buffer);<br></span><span style="color: #008080;">126</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="color: #000000;">0</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">].print();<br></span><span style="color: #008080;">127</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;">128</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">129</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br><img src ="http://www.cppblog.com/yzhw/aggbug/138432.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-13 09:57 <a href="http://www.cppblog.com/yzhw/archive/2011/01/13/138432.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku3691 DNA repair 自动机+DP</title><link>http://www.cppblog.com/yzhw/archive/2011/01/12/138396.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Wed, 12 Jan 2011 05:21:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/12/138396.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/138396.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/12/138396.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/138396.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/138396.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 题意：给出一个DNA序列，和若干病毒DNA片段，问最少修改DNA的次数使得DNA序列中不包含病毒解法：首先构造DFA（再次犯了和福州现场赛一样的错误，没有+结束标记状态的转移。。。。WA了N久），然后就是在状态机上的DP了令dp[i]][j]为当前在自动机第i个节点上时suffix(j)成为合法子串需要修改的最少次数。下面状态转移就简单了。。（这种状态转移通过方程和难描述，大家看程序吧，我写的很清...&nbsp;&nbsp;<a href='http://www.cppblog.com/yzhw/archive/2011/01/12/138396.html'>阅读全文</a><img src ="http://www.cppblog.com/yzhw/aggbug/138396.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-12 13:21 <a href="http://www.cppblog.com/yzhw/archive/2011/01/12/138396.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1180 Batch Scheduling 经典斜率优化</title><link>http://www.cppblog.com/yzhw/archive/2011/01/03/137929.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Mon, 03 Jan 2011 13:52:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/03/137929.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/137929.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/03/137929.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/137929.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/137929.html</trackback:ping><description><![CDATA[<p>题意:<br>N个任务，每个任务有两个参数，完成需要的时间t以及因子f，现在将这些任务分组，加工每个分组前机器需要冷却时间start，完成一组任务耗时为 t + Start + (T<sub>x</sub> + T<sub>x+1</sub> + ... + T<sub>x+k</sub>)，问最少耗费的时间。<br>解法：<br>首先先解决这个问题，在这种分组数不确定的问题中，根据背包九讲中无限背包的策略，外层循环只要一层即可完成。<br>关键是内重循环还要枚举本组的起始位置，如果暴力的做就要N2了。<br>下面试图对初始的DP方程进行优化<br>考虑两种规划方向，第一种规划方向是从前到后<br>dp[i]=min{dp[k]+(dp[k]+sumt(i)-sumt(k)+start)*(sumf(i)-sumf(k))} 无比复杂的一个式子，推了N小时，在这个式子上下手似乎非常困难。。。（3个决策变量）<br>但是，如果换种思路，从后向前，将sumt以及sumf重新定义为最后一个任务到第i个任务的时间和和参数和，这样就简单多了。<br>dp[i]=min{dp[k]+(sumt(i)-sumt(k)+start)*sumf(i)}<br>然后很轻易能发现这个满足斜率优化的条件（2个待定决策变量，sumt(k)和dp(k)）。<br>关于斜率优化，一般有代数和几何两种方法。先说几何方法<br>令g=dp[k]+(sumt(i)-sumt(k)+start)*sumf(i)}，y=dp[k],x=sumt[k]<br>现在要使g最小<br>将式子整理下<br>y=sumf(i)*x+f-(start+sumt(i))*sumf(i)<br>将这个看做一个线性函数，目标要使得截距最小<br>再观察一下，斜率sumf(i)恒为正，且是随着i单调递减的，换句话说，是随着规划的过程单调递增的，而自变量x也是随着规划的过程单调递增的。我们画个图比划下<br><img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/yzhw/pic1.jpg" width=382 height=354><br>首先，我们要维护的是一个上凹线，因为凹线内部的点不会被斜率为正的直线切到。再者，我们发现最优决策就在凹线的左端。我们来观察蓝色的线，假设这个是第i阶段的决策线，灰色凹线与蓝色线切点左侧的绿色部分显然是可以丢弃的。应为在切点处截距达到最小。并且在以后的决策中（浅蓝色的线），该段同样不会有任何作用。根据以上观察，我们可以使用栈队列（很多人简称为单调队列）的数据结构。这个东西网上介绍的比较多，我就不细说了。<br>第二种方法就是代数法。<br>我看网上有一个牛写的不错，就贴过来吧- -<br>
<fieldset><legend>引自<a href="http://hi.baidu.com/wangkun_zhen/blog/item/5e85d0cc4fb887590fb345dd.html">http://hi.baidu.com/wangkun_zhen/blog/item/5e85d0cc4fb887590fb345dd.html</a></legend>&nbsp;&nbsp;&nbsp;&nbsp; dp[i] = min{dp[j] + (S + sumT[i] - sumT[j]) * sumF[i]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { i &lt; j &lt;= n + 1 }&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span twffan="done">边界条件</span><font color=#000000><span twffan="done"><font face="Times New Roman"> dp[n+1] = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sumT[n+1]=sumF[n+1]=0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></span><span twffan="done">可以清楚的看到，此种</span><span twffan="done"><font face="Times New Roman">DP</font></span><span twffan="done">方法的时间复杂度是</span><span twffan="done"><font face="Times New Roman">O(n^2)</font></span><span twffan="done">的，那么如何降低复杂度呢？</span></font><span twffan="done"><br><br></span><span twffan="done"><font color=#000000>经典的优化分析：</font></span><span twffan="done"><br><font color=#000000 face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></span><font color=#000000><span twffan="done">我们考虑在计算</span><span twffan="done"><font face="Times New Roman">dp[i]</font></span><span twffan="done">时，对于</span><span twffan="done"><font face="Times New Roman">i &lt; j &lt; k</font></span><span twffan="done">来说，</span><font face="Times New Roman"> </font><span twffan="done">如果保证决策</span><span twffan="done"><font face="Times New Roman">k</font></span><span twffan="done">比决策</span><span twffan="done"><font face="Times New Roman">j</font></span><span twffan="done">大的条件是：</span></font><span twffan="done"><br style="mso-special-character: line-break"><br style="mso-special-character: line-break"></span>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span twffan="done"><font color=#000000 face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dp[j] + (S + sumT[i] - sumT[j]) * sumF[i] &lt; dp[k] + (S + sumT[i] - sumT[k]) * sumF[i]</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span twffan="done"><br></span><span twffan="done"><font color=#000000>通过移项整理，可以化简为：</font></span><span twffan="done"><br><br><font color=#000000 face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (dp[j] - dp[k]) / (sumT[j] - sumT[k]) &lt; sumF[i]<br><br></font></span><span twffan="done"><font color=#000000>这个类似于斜率的东西又怎么来优化？</font></span><span twffan="done"><br></span><span twffan="done"><font color=#000000>为了证明和更好的说明接下来的优化，我们定义这么几个变量：</font></span><span twffan="done"><br></span><span twffan="done"><font color=#000000>设：</font></span><font color=#000000><span twffan="done"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d[i, x] = dp[x] + (S + sumT[i] - sumT[x]) * sumF[i] ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g[j, k] = (dp[j] - dp[k]) / (sumT[j] - sumT[k])<br><br></font></span><span twffan="done">那么我们可以总结一下上面推出来的式子：</span></font><span twffan="done"><br></span><span twffan="done"><font color=#000000>根据上面有：</font></span><span twffan="done"><br><font color=#000000 face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i&lt;j&lt;k<br></font></span><font color=#000000><span twffan="done">同理可证：</span><span twffan="done"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d[i, j] &lt; d[i, k]&nbsp;&nbsp;&nbsp; &lt;=&gt;&nbsp;&nbsp; g[j, k] &lt; sumF[i]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></span><span twffan="done">决策</span><span twffan="done"><font face="Times New Roman">j</font></span><span twffan="done">比决策</span><span twffan="done"><font face="Times New Roman">k</font></span><span twffan="done">小</span></font><span twffan="done"><br><br></span><font color=#000000><span twffan="done">进而我们发现这么一个问题，当</span><span twffan="done"><font face="Times New Roman">i &lt; j &lt; k &lt; l</font></span><span twffan="done">时，如果有</span><span twffan="done"><font face="Times New Roman">g[j, k] &lt; g[k, l]</font></span><span twffan="done">，那么</span><span twffan="done"><font face="Times New Roman">k</font></span><span twffan="done">永远都不会成为计算</span><span twffan="done"><font face="Times New Roman">dp[i]</font></span><span twffan="done">时的决策点。</span></font><span twffan="done"><br><br></span><span twffan="done"><font color=#000000>证明：</font></span><span twffan="done"><br></span><font color=#000000><span twffan="done">如果</span><span twffan="done"><font face="Times New Roman">g[j, k] &lt; g[k, l]</font></span><span twffan="done">，那么我们可以分两个方面考虑</span><span twffan="done"><font face="Times New Roman">g[j, k]</font></span><span twffan="done">与</span><span twffan="done"><font face="Times New Roman">sumF[i]</font></span><span twffan="done">的关系：</span></font><span twffan="done"><br><font color=#000000 face="Times New Roman">(1)</font></span><font color=#000000><span twffan="done">如果</span><span twffan="done"><font face="Times New Roman">g[k, l] &gt;= sumF[i]</font></span><span twffan="done">，那么决策</span><span twffan="done"><font face="Times New Roman">k</font></span><span twffan="done">大于等于决策</span><span twffan="done"><font face="Times New Roman">l</font></span><span twffan="done">，也就说决策</span><span twffan="done"><font face="Times New Roman">k</font></span><span twffan="done">不可能是决策点</span></font><span twffan="done"><br><font color=#000000 face="Times New Roman">(2)</font></span><font color=#000000><span twffan="done">如果</span><span twffan="done"><font face="Times New Roman">g[j, k] &lt; sumF[i],</font></span><span twffan="done">那么决策</span><span twffan="done"><font face="Times New Roman">k</font></span><span twffan="done">要大于决策</span><span twffan="done"><font face="Times New Roman">j</font></span><span twffan="done">，所以</span><span twffan="done"><font face="Times New Roman">k</font></span><span twffan="done">也不可能是决策点</span></font><span twffan="done"><br><br></span><font color=#000000><span twffan="done">根据上面的结论和一些特性，我们可以考虑维护一个斜率的双向队列来优化整个</span><span twffan="done"><font face="Times New Roman">DP</font></span><span twffan="done">过程：</span></font><span twffan="done"><br><br><font color=#000000 face="Times New Roman">(1)</font></span><font color=#000000><span twffan="done">假设</span><span twffan="done"><font face="Times New Roman">i(</font></span><span twffan="done">马上要入队的元素</span><span twffan="done"><font face="Times New Roman">) &lt;j&lt; k</font></span><span twffan="done">依次是队列尾部的元素，那么我们就要考虑</span><span twffan="done"><font face="Times New Roman">g[i, j]</font></span><span twffan="done">是否大于</span><span twffan="done"><font face="Times New Roman">g[j, k]</font></span><span twffan="done">，如果</span><span twffan="done"><font face="Times New Roman">g[i, j] &lt; g[j, k]</font></span><span twffan="done">，那么可以肯定</span><span twffan="done"><font face="Times New Roman">j</font></span><span twffan="done">一定不会是决策点，所以我们可以从队列中将</span><span twffan="done"><font face="Times New Roman">j</font></span><span twffan="done">去掉，然后依次向前推，直到找到一个队列元素少于</span><span twffan="done"><font face="Times New Roman">3</font></span><span twffan="done">个或者</span><span twffan="done"><font face="Times New Roman">g[i j] &gt;= g[j, k]</font></span><span twffan="done">的点才停止。</span></font><span twffan="done"><br><font color=#000000 face="Times New Roman">(2)</font></span><font color=#000000><span twffan="done">假设</span><span twffan="done"><font face="Times New Roman">a&gt;b(a</font></span><span twffan="done">是头元素</span><span twffan="done"><font face="Times New Roman">)</font></span><span twffan="done">是依次是队列头部的元素，那么我们知道，如果</span><span twffan="done"><font face="Times New Roman">g[b, a] &lt; sumF[i]</font></span><span twffan="done">的话，那么对于</span><span twffan="done"><font face="Times New Roman">i</font></span><span twffan="done">来说决策点</span><span twffan="done"><font face="Times New Roman">b</font></span><span twffan="done">肯定优于决策点</span><span twffan="done"><font face="Times New Roman">a</font></span><span twffan="done">，又由于</span><span twffan="done"><font face="Times New Roman">sumF[i]</font></span><span twffan="done">是随着</span><span twffan="done"><font face="Times New Roman">i</font></span><span twffan="done">减少而递增的（这个就是为什么倒推的原因），所以当</span><span twffan="done"><font face="Times New Roman">g[a, b] &lt; sumF[i]</font></span><span twffan="done">时，就一定有</span><span twffan="done"><font face="Times New Roman">g[a, b] &lt; sumF[i-1]</font></span><span twffan="done">，因此当前的决策点</span><span twffan="done"><font face="Times New Roman">a</font></span><span twffan="done">不仅仅在考虑</span><span twffan="done"><font face="Times New Roman">dp[i]</font></span><span twffan="done">时不会是最佳决策点，而且在后面的</span><span twffan="done"><font face="Times New Roman">DP</font></span><span twffan="done">中也一定不会是最佳决策点，所以我们可以把</span><span twffan="done"><font face="Times New Roman">a</font></span><span twffan="done">从队列</span><font face="Times New Roman"> </font><span twffan="done">的头部删除，依次往后如此操作，直到队列元素小于</span><span twffan="done"><font face="Times New Roman">2</font></span><span twffan="done">或者</span><span twffan="done"><font face="Times New Roman">g[b, a] &gt;= sumF[i]</font></span><span twffan="done">。</span></font><span twffan="done"><br><font color=#000000 face="Times New Roman">(3)</font></span><font color=#000000><span twffan="done">对于</span><span twffan="done"><font face="Times New Roman">i</font></span><span twffan="done">的更新，一定是队列头部的决策点最好，所以</span><span twffan="done"><font face="Times New Roman">O(1)</font></span><span twffan="done">即可转移。</span></font><span twffan="done"><br></span></p>
</fieldset><br>初始化的时候要注意，<span style="COLOR: red">必须在n+1的位置处加一个虚拟的状态，不能将第一个状态（即i=n的状态）作为初始状态</span>，因为可能将所有任务仅分为一段。<br>代码：<br>
<p>&#160;</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;define&nbsp;N&nbsp;</span><span style="COLOR: #000000">10005</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,start,st[N],sf[N];<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;q[N],s</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dp[N];<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cmp(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;b,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;k)<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_137_281_Open_Image onclick="this.style.display='none'; Codehighlighter1_137_281_Open_Text.style.display='none'; Codehighlighter1_137_281_Closed_Image.style.display='inline'; Codehighlighter1_137_281_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_137_281_Closed_Image onclick="this.style.display='none'; Codehighlighter1_137_281_Closed_Text.style.display='none'; Codehighlighter1_137_281_Open_Image.style.display='inline'; Codehighlighter1_137_281_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_137_281_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_137_281_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(dp[b]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">dp[a]</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)k</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(st[b]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">st[a]))&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">((</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)dp[b]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">dp[a]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)k</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(st[b]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">st[a]))&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">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&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">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cmp1(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;y1,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x1,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;y2,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x2,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;y3,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x3)<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_371_534_Open_Image onclick="this.style.display='none'; Codehighlighter1_371_534_Open_Text.style.display='none'; Codehighlighter1_371_534_Closed_Image.style.display='inline'; Codehighlighter1_371_534_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_371_534_Closed_Image onclick="this.style.display='none'; Codehighlighter1_371_534_Closed_Text.style.display='none'; Codehighlighter1_371_534_Open_Image.style.display='inline'; Codehighlighter1_371_534_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_371_534_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_371_534_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(((</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)y3</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">y2)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(x2</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">x1)</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">((</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)y2</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">y1)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(x3</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">x2))&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(((</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)y3</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">y2)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(x2</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">x1)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">((</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)y2</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">y1)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(x3</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">x2))&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">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&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">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_547_976_Open_Image onclick="this.style.display='none'; Codehighlighter1_547_976_Open_Text.style.display='none'; Codehighlighter1_547_976_Closed_Image.style.display='inline'; Codehighlighter1_547_976_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_547_976_Closed_Image onclick="this.style.display='none'; Codehighlighter1_547_976_Closed_Text.style.display='none'; Codehighlighter1_547_976_Open_Image.style.display='inline'; Codehighlighter1_547_976_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_547_976_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_547_976_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">start);<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,st</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i,sf</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i);<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img id=Codehighlighter1_650_689_Open_Image onclick="this.style.display='none'; Codehighlighter1_650_689_Open_Text.style.display='none'; Codehighlighter1_650_689_Closed_Image.style.display='inline'; Codehighlighter1_650_689_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_650_689_Closed_Image onclick="this.style.display='none'; Codehighlighter1_650_689_Closed_Text.style.display='none'; Codehighlighter1_650_689_Open_Image.style.display='inline'; Codehighlighter1_650_689_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_650_689_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_650_689_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st[i]</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">st[i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sf[i]</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">sf[i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;q[e]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;dp[n]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img id=Codehighlighter1_732_939_Open_Image onclick="this.style.display='none'; Codehighlighter1_732_939_Open_Text.style.display='none'; Codehighlighter1_732_939_Closed_Image.style.display='inline'; Codehighlighter1_732_939_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_732_939_Closed_Image onclick="this.style.display='none'; Codehighlighter1_732_939_Closed_Text.style.display='none'; Codehighlighter1_732_939_Open_Image.style.display='inline'; Codehighlighter1_732_939_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_732_939_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_732_939_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(e</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cmp(q[s</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">],q[s</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">],sf[i])</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;s</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[q[s</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">(start</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">st[i]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">st[q[s</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">sf[i];<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(e</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cmp1(dp[q[e</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]],st[q[e</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]],dp[q[e]],st[q[e]],dp[i],st[i])</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;e</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q[</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">e]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,dp[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/137929.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-03 21:52 <a href="http://www.cppblog.com/yzhw/archive/2011/01/03/137929.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1173 Bar Codes 经典DP，逐位确定rank</title><link>http://www.cppblog.com/yzhw/archive/2011/01/02/137902.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sun, 02 Jan 2011 15:17:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/02/137902.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/137902.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/02/137902.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/137902.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/137902.html</trackback:ping><description><![CDATA[题意：<br>给出条形码系统 <br>BC(n,k,m) is the set of all symbols with k bars that together extend over exactly n units, each bar being at most m units wide. <br>1、给出n,k,m，条形码第一条是黑色。问有多少种不同的划分方式。<br>2、给出条形码，求它的rank（字典序）<br><img border=0 alt="" src="http://poj.org/images/1173_1.jpg" width=182 height=208><br>第一问应该很好解决，BC(n,k,m)=sum(BC(n-i,k-1,m)),i=1,2..m<br>第二问也是用经典的逐位确定的方法，唯一要注意的是，白条要从大到小累加（因为0代表白色，显然，当前条0的位数越多，字典序越小），而黑条要从小到大累加<br><br>代码：<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#&nbsp;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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;c[</span><span style="COLOR: #000000">35</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">35</span><span style="COLOR: #000000">],n,k,m;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_114_1248_Open_Image onclick="this.style.display='none'; Codehighlighter1_114_1248_Open_Text.style.display='none'; Codehighlighter1_114_1248_Closed_Image.style.display='inline'; Codehighlighter1_114_1248_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_114_1248_Closed_Image onclick="this.style.display='none'; Codehighlighter1_114_1248_Closed_Text.style.display='none'; Codehighlighter1_114_1248_Open_Image.style.display='inline'; Codehighlighter1_114_1248_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_114_1248_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_114_1248_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">k,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">m);<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;memset(c,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(c));<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;num</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;num</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">min(n,m);num</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[num][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;num</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;num</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;num</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">k;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">max(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,num</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">m);j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">num;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[num][i]</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">c[j][i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,c[n][k]);<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;num;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">num);<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(num</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img id=Codehighlighter1_452_1230_Open_Image onclick="this.style.display='none'; Codehighlighter1_452_1230_Open_Text.style.display='none'; Codehighlighter1_452_1230_Closed_Image.style.display='inline'; Codehighlighter1_452_1230_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_452_1230_Closed_Image onclick="this.style.display='none'; Codehighlighter1_452_1230_Closed_Text.style.display='none'; Codehighlighter1_452_1230_Open_Image.style.display='inline'; Codehighlighter1_452_1230_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_452_1230_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_452_1230_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;str[</span><span style="COLOR: #000000">50</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;color</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;last</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,rank</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,co</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,str);<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">strlen(str);i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_604_1194_Open_Image onclick="this.style.display='none'; Codehighlighter1_604_1194_Open_Text.style.display='none'; Codehighlighter1_604_1194_Closed_Image.style.display='inline'; Codehighlighter1_604_1194_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_604_1194_Closed_Image onclick="this.style.display='none'; Codehighlighter1_604_1194_Closed_Text.style.display='none'; Codehighlighter1_604_1194_Open_Image.style.display='inline'; Codehighlighter1_604_1194_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_604_1194_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_604_1194_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(str[i]</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">color)<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_648_1184_Open_Image onclick="this.style.display='none'; Codehighlighter1_648_1184_Open_Text.style.display='none'; Codehighlighter1_648_1184_Closed_Image.style.display='inline'; Codehighlighter1_648_1184_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_648_1184_Closed_Image onclick="this.style.display='none'; Codehighlighter1_648_1184_Closed_Text.style.display='none'; Codehighlighter1_648_1184_Open_Image.style.display='inline'; Codehighlighter1_648_1184_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_648_1184_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_648_1184_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;len</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">last;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000">(color)<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img id=Codehighlighter1_725_1125_Open_Image onclick="this.style.display='none'; Codehighlighter1_725_1125_Open_Text.style.display='none'; Codehighlighter1_725_1125_Closed_Image.style.display='inline'; Codehighlighter1_725_1125_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_725_1125_Closed_Image onclick="this.style.display='none'; Codehighlighter1_725_1125_Closed_Text.style.display='none'; Codehighlighter1_725_1125_Open_Image.style.display='inline'; Codehighlighter1_725_1125_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_725_1125_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_725_1125_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">last</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">i;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rank</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">c[n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">j][k</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">co];<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,last</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">m);j</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">i;j</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rank</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">c[n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">j][k</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">co];<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,rank);<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/137902.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-02 23:17 <a href="http://www.cppblog.com/yzhw/archive/2011/01/02/137902.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 3373 Changing Digits 贪心的DP，注意DP方向</title><link>http://www.cppblog.com/yzhw/archive/2011/01/01/137854.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sat, 01 Jan 2011 10:39:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2011/01/01/137854.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/137854.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2011/01/01/137854.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/137854.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/137854.html</trackback:ping><description><![CDATA[题意：<br>给出2个整数N和K，求满足以下条件的整数M<br>1、M与N位数相同<br>2、M能被K整除<br>满足以上两点的情况下，与N不同位数的个数以及M值最小的优先<br>n&lt;10^100<br>k&lt;10000<br>解法：<br>考虑到k不大，状态可以设置为dp[i][k]，即i位的数字中余数为k的需要修改的最小值<br>但是由于最后一个约束条件，即数小的优先，如果从正向DP，不能满足要求<br>应为从状态i-1转移到状态i的过程中，贪心的DP保证了第i位取值最小。可能出现这种状况：<br>假设abcd%k==efgh%k<br>d&lt;h,但a&gt;e。这样就不满足题意了。<br>解决方法是逆向DP，这样既能保证靠近前面的位取值最小。<br><br>代码：<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">Source&nbsp;Code<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Problem:&nbsp;</span><span style="COLOR: #000000">3373</span><span style="COLOR: #000000">&nbsp;&nbsp;User:&nbsp;yzhw&nbsp;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Memory:&nbsp;12180K&nbsp;&nbsp;Time:&nbsp;1235MS&nbsp;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Language:&nbsp;G</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;&nbsp;Result:&nbsp;Accepted&nbsp;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Source&nbsp;Code&nbsp;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;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;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dp[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">10000</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;path[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">10000</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;print(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;start,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;left,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;len)<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img id=Codehighlighter1_261_373_Open_Image onclick="this.style.display='none'; Codehighlighter1_261_373_Open_Text.style.display='none'; Codehighlighter1_261_373_Closed_Image.style.display='inline'; Codehighlighter1_261_373_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_261_373_Closed_Image onclick="this.style.display='none'; Codehighlighter1_261_373_Closed_Text.style.display='none'; Codehighlighter1_261_373_Open_Image.style.display='inline'; Codehighlighter1_261_373_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_261_373_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_261_373_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(start</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">len</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,path[start][left][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;print(start</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,path[start][left][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">],len);<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img id=Codehighlighter1_386_1236_Open_Image onclick="this.style.display='none'; Codehighlighter1_386_1236_Open_Text.style.display='none'; Codehighlighter1_386_1236_Closed_Image.style.display='inline'; Codehighlighter1_386_1236_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_386_1236_Closed_Image onclick="this.style.display='none'; Codehighlighter1_386_1236_Closed_Text.style.display='none'; Codehighlighter1_386_1236_Open_Image.style.display='inline'; Codehighlighter1_386_1236_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_386_1236_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_386_1236_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;str[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;mod[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,str</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">EOF)<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img id=Codehighlighter1_463_1220_Open_Image onclick="this.style.display='none'; Codehighlighter1_463_1220_Open_Text.style.display='none'; Codehighlighter1_463_1220_Closed_Image.style.display='inline'; Codehighlighter1_463_1220_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_463_1220_Closed_Image onclick="this.style.display='none'; Codehighlighter1_463_1220_Closed_Text.style.display='none'; Codehighlighter1_463_1220_Open_Image.style.display='inline'; Codehighlighter1_463_1220_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_463_1220_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_463_1220_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;k;<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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">k);<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mod[strlen(str</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">k;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">strlen(str</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mod[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(mod[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">10</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">k;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[strlen(str</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">0</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">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">strlen(str</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);i</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;p</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">?</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);p</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;p</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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">k;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(dp[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">1</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(dp[i][(j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">mod[i])</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">k]</span><span style="COLOR: #000000">==-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">dp[i][(j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">mod[i])</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">k]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">dp[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</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">str[i]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">48</span><span style="COLOR: #000000">))<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img id=Codehighlighter1_949_1149_Open_Image onclick="this.style.display='none'; Codehighlighter1_949_1149_Open_Text.style.display='none'; Codehighlighter1_949_1149_Closed_Image.style.display='inline'; Codehighlighter1_949_1149_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_949_1149_Closed_Image onclick="this.style.display='none'; Codehighlighter1_949_1149_Closed_Text.style.display='none'; Codehighlighter1_949_1149_Open_Image.style.display='inline'; Codehighlighter1_949_1149_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_949_1149_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_949_1149_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][(j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">mod[i])</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">k]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[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</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">str[i]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">48</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path[i][(j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">mod[i])</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">k][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path[i][(j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">mod[i])</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">k][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">p;<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,strlen(str</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/137854.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2011-01-01 18:39 <a href="http://www.cppblog.com/yzhw/archive/2011/01/01/137854.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1203 Timetable 拆点+DP最短路</title><link>http://www.cppblog.com/yzhw/archive/2010/12/09/136005.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Thu, 09 Dec 2010 13:25:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/12/09/136005.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/136005.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/12/09/136005.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/136005.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/136005.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 题意：TimetableTime Limit: 3000MS&nbsp; Memory Limit: 65536K Total Submissions: 281&nbsp; Accepted: 86 DescriptionYou are the owner of a railway system between n cities, numbered by integers from 1 t...&nbsp;&nbsp;<a href='http://www.cppblog.com/yzhw/archive/2010/12/09/136005.html'>阅读全文</a><img src ="http://www.cppblog.com/yzhw/aggbug/136005.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-12-09 21:25 <a href="http://www.cppblog.com/yzhw/archive/2010/12/09/136005.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>fzu 2006 Farm Game(The 35th ACM/ICPC Asia Regional Fuzhou Site) DAG上的DP</title><link>http://www.cppblog.com/yzhw/archive/2010/12/07/135679.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Tue, 07 Dec 2010 05:08:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/12/07/135679.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/135679.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/12/07/135679.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/135679.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/135679.html</trackback:ping><description><![CDATA[题意：<br>给出一个DAG，每个点上有一定的价值p和数量w,给出一些转换关系：a<sub>i-1</sub> a<sub>i</sub> b<sub>i</sub> ，即a<sub>i-1</sub>可以兑换成b<sub>i</sub>单位a<sub>i</sub> ,然后问这些点的最大价值。<br>解法：<br>由于是DAG，可以用DP来解，dp[pos]=max{p[pos],dp[k]*rate[pos][k]},g[pos][k]=true<br>然后结果就是sum{dp[i]*w[i]}<br>这次第一次参加regional，fzu现场赛时候各种手残脑残，N个能够秒的题都没去看。甚可惜。。。回来看了下题目都不是多困难的，做个5题没问题的说。。。哎，明年再一雪耻辱吧。。<br>代码：<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#&nbsp;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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;N</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10005</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img id=Codehighlighter1_90_159_Open_Image onclick="this.style.display='none'; Codehighlighter1_90_159_Open_Text.style.display='none'; Codehighlighter1_90_159_Closed_Image.style.display='inline'; Codehighlighter1_90_159_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_90_159_Closed_Image onclick="this.style.display='none'; Codehighlighter1_90_159_Closed_Text.style.display='none'; Codehighlighter1_90_159_Open_Image.style.display='inline'; Codehighlighter1_90_159_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_90_159_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_90_159_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;nxt;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;rate;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img id=Codehighlighter1_155_156_Open_Image onclick="this.style.display='none'; Codehighlighter1_155_156_Open_Text.style.display='none'; Codehighlighter1_155_156_Closed_Image.style.display='inline'; Codehighlighter1_155_156_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_155_156_Closed_Image onclick="this.style.display='none'; Codehighlighter1_155_156_Closed_Text.style.display='none'; Codehighlighter1_155_156_Open_Image.style.display='inline'; Codehighlighter1_155_156_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;node(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;r):nxt(n),rate(r)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_155_156_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_155_156_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">node</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;g[N];<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;p[N],w[N];<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;used[N];<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;solve(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;pos)<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img id=Codehighlighter1_240_447_Open_Image onclick="this.style.display='none'; Codehighlighter1_240_447_Open_Text.style.display='none'; Codehighlighter1_240_447_Closed_Image.style.display='inline'; Codehighlighter1_240_447_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_240_447_Closed_Image onclick="this.style.display='none'; Codehighlighter1_240_447_Closed_Text.style.display='none'; Codehighlighter1_240_447_Open_Image.style.display='inline'; Codehighlighter1_240_447_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_240_447_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_240_447_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(used[pos])&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;used[pos]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</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">g[pos].size();i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img id=Codehighlighter1_325_445_Open_Image onclick="this.style.display='none'; Codehighlighter1_325_445_Open_Text.style.display='none'; Codehighlighter1_325_445_Closed_Image.style.display='inline'; Codehighlighter1_325_445_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_325_445_Closed_Image onclick="this.style.display='none'; Codehighlighter1_325_445_Closed_Text.style.display='none'; Codehighlighter1_325_445_Open_Image.style.display='inline'; Codehighlighter1_325_445_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_325_445_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_325_445_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;solve(g[pos][i].nxt);<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(g[pos][i].rate</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p[g[pos][i].nxt]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">p[pos])&nbsp;p[pos]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">g[pos][i].rate</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p[g[pos][i].nxt];<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_460_1145_Open_Image onclick="this.style.display='none'; Codehighlighter1_460_1145_Open_Text.style.display='none'; Codehighlighter1_460_1145_Closed_Image.style.display='inline'; Codehighlighter1_460_1145_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_460_1145_Closed_Image onclick="this.style.display='none'; Codehighlighter1_460_1145_Closed_Text.style.display='none'; Codehighlighter1_460_1145_Open_Image.style.display='inline'; Codehighlighter1_460_1145_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_460_1145_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_460_1145_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_482_1129_Open_Image onclick="this.style.display='none'; Codehighlighter1_482_1129_Open_Text.style.display='none'; Codehighlighter1_482_1129_Closed_Image.style.display='inline'; Codehighlighter1_482_1129_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_482_1129_Closed_Image onclick="this.style.display='none'; Codehighlighter1_482_1129_Closed_Text.style.display='none'; Codehighlighter1_482_1129_Open_Image.style.display='inline'; Codehighlighter1_482_1129_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_482_1129_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_482_1129_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">n)&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">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">)<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img id=Codehighlighter1_560_646_Open_Image onclick="this.style.display='none'; Codehighlighter1_560_646_Open_Text.style.display='none'; Codehighlighter1_560_646_Closed_Image.style.display='inline'; Codehighlighter1_560_646_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_560_646_Closed_Image onclick="this.style.display='none'; Codehighlighter1_560_646_Closed_Text.style.display='none'; Codehighlighter1_560_646_Open_Image.style.display='inline'; Codehighlighter1_560_646_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_560_646_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_560_646_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%lf%lf</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i,w</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i);<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g[i].clear();<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;used[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;m;<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">m);<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(m</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img id=Codehighlighter1_706_974_Open_Image onclick="this.style.display='none'; Codehighlighter1_706_974_Open_Text.style.display='none'; Codehighlighter1_706_974_Closed_Image.style.display='inline'; Codehighlighter1_706_974_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_706_974_Closed_Image onclick="this.style.display='none'; Codehighlighter1_706_974_Closed_Text.style.display='none'; Codehighlighter1_706_974_Open_Image.style.display='inline'; Codehighlighter1_706_974_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_706_974_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_706_974_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;k;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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">k);<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;last,now;<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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">last);<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img id=Codehighlighter1_836_966_Open_Image onclick="this.style.display='none'; Codehighlighter1_836_966_Open_Text.style.display='none'; Codehighlighter1_836_966_Closed_Image.style.display='inline'; Codehighlighter1_836_966_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_836_966_Closed_Image onclick="this.style.display='none'; Codehighlighter1_836_966_Closed_Text.style.display='none'; Codehighlighter1_836_966_Open_Image.style.display='inline'; Codehighlighter1_836_966_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_836_966_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_836_966_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;r;<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%lf%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">r,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">now);<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g[last].push_back(node(now,r));<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">now;<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">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">)<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;solve(i);<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">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">)<br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">p[i]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">w[i];<br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%.2f\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,ans);<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">61</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/135679.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-12-07 13:08 <a href="http://www.cppblog.com/yzhw/archive/2010/12/07/135679.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku3670 Eating Together DP+堆</title><link>http://www.cppblog.com/yzhw/archive/2010/12/06/135547.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sun, 05 Dec 2010 17:54:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/12/06/135547.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/135547.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/12/06/135547.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/135547.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/135547.html</trackback:ping><description><![CDATA[题意：<br>给出一个序列（只包含1、2、3），问需要修改最少元素的个数使得这个序列递增或递减。<br>解法：<br>discuss里有说这题是LIS，没有仔细想，我是自己推DP来做的。。<br>设sum1[i]为将数列前i个全部修正为1需要修改的次数，sum2、sum3亦同。<br>先写修改最少元素使得序列递增的做法<br>dp[j][i]=min(dp[j-1][i],sum[j][i]-sum[j][k]+dp[j-1][k],sum[j][i]);<br>观察第二项<br>sum[j][i]-sum[j][k]+dp[j-1][k]，前一项是与k无关的常量，所以可以用一个堆来记录前i-1项dp[j-1][k]-sum[j][k]的最小值，这样可以省去一重枚举k的复杂度<br>序列递减做法亦同。<br>总复杂度O(nlog<sub>n</sub>)<br><br>再次犯N个NC错误：<br><span style="COLOR: red">1、最佳值应该为dp[n],而不是dp[1..n]中的最小值。。。<br>2、开始写DP方程的时候min里面漏了一项。。不过这个似乎不能怪我，题目理解有点歧义，我原来以为必须要用到1、2、3这3个元素。。所以dp方程里只有一项。。然后看discuss发现我理解错了，修正了下dp方程，结果。。汗。。<br></span><br>代码：<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">queue</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;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;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstdlib</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;define&nbsp;min(a,b)&nbsp;((a)</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">(b)</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">(a):(b))<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;N</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">30005</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;s[</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">][N],</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">sum,n,dp[N],tdp[N];<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;cmp<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_218_330_Open_Image onclick="this.style.display='none'; Codehighlighter1_218_330_Open_Text.style.display='none'; Codehighlighter1_218_330_Closed_Image.style.display='inline'; Codehighlighter1_218_330_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_218_330_Closed_Image onclick="this.style.display='none'; Codehighlighter1_218_330_Closed_Text.style.display='none'; Codehighlighter1_218_330_Open_Image.style.display='inline'; Codehighlighter1_218_330_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_218_330_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_218_330_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">()(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">b)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_275_328_Open_Image onclick="this.style.display='none'; Codehighlighter1_275_328_Open_Text.style.display='none'; Codehighlighter1_275_328_Closed_Image.style.display='inline'; Codehighlighter1_275_328_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_275_328_Closed_Image onclick="this.style.display='none'; Codehighlighter1_275_328_Closed_Text.style.display='none'; Codehighlighter1_275_328_Open_Image.style.display='inline'; Codehighlighter1_275_328_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_275_328_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_275_328_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;dp[a]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(a</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sum[a])</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">dp[b]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(b</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sum[b]);<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">priority_queue</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">,vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">,cmp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;q;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;print()<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_385_472_Open_Image onclick="this.style.display='none'; Codehighlighter1_385_472_Open_Text.style.display='none'; Codehighlighter1_385_472_Closed_Image.style.display='inline'; Codehighlighter1_385_472_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_385_472_Closed_Image onclick="this.style.display='none'; Codehighlighter1_385_472_Closed_Text.style.display='none'; Codehighlighter1_385_472_Open_Image.style.display='inline'; Codehighlighter1_385_472_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_385_472_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_385_472_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">)<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">dp[i]</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">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;cout</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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;system("pause");</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_485_1821_Open_Image onclick="this.style.display='none'; Codehighlighter1_485_1821_Open_Text.style.display='none'; Codehighlighter1_485_1821_Closed_Image.style.display='inline'; Codehighlighter1_485_1821_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_485_1821_Closed_Image onclick="this.style.display='none'; Codehighlighter1_485_1821_Closed_Text.style.display='none'; Codehighlighter1_485_1821_Open_Image.style.display='inline'; Codehighlighter1_485_1821_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_485_1821_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_485_1821_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;memset(s,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(s));<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</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">)<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img id=Codehighlighter1_556_672_Open_Image onclick="this.style.display='none'; Codehighlighter1_556_672_Open_Text.style.display='none'; Codehighlighter1_556_672_Closed_Image.style.display='inline'; Codehighlighter1_556_672_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_556_672_Closed_Image onclick="this.style.display='none'; Codehighlighter1_556_672_Closed_Text.style.display='none'; Codehighlighter1_556_672_Open_Image.style.display='inline'; Codehighlighter1_556_672_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_556_672_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_556_672_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s[j][i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">s[j][i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;t;<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">t;<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s[t</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">;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0xfffffff</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</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">)<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">s[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][i];<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;print();</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">41</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img id=Codehighlighter1_792_1200_Open_Image onclick="this.style.display='none'; Codehighlighter1_792_1200_Open_Text.style.display='none'; Codehighlighter1_792_1200_Closed_Image.style.display='inline'; Codehighlighter1_792_1200_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_792_1200_Closed_Image onclick="this.style.display='none'; Codehighlighter1_792_1200_Closed_Text.style.display='none'; Codehighlighter1_792_1200_Open_Image.style.display='inline'; Codehighlighter1_792_1200_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_792_1200_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_792_1200_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">q.empty())&nbsp;q.pop();<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;memset(tdp,0,sizeof(tdp));</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">45</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">s[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.push(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tdp[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(dp[</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">sum[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">2</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">)<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img id=Codehighlighter1_972_1075_Open_Image onclick="this.style.display='none'; Codehighlighter1_972_1075_Open_Text.style.display='none'; Codehighlighter1_972_1075_Closed_Image.style.display='inline'; Codehighlighter1_972_1075_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_972_1075_Closed_Image onclick="this.style.display='none'; Codehighlighter1_972_1075_Closed_Text.style.display='none'; Codehighlighter1_972_1075_Open_Image.style.display='inline'; Codehighlighter1_972_1075_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_972_1075_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_972_1075_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tdp[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(dp[q.top()]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(q.top()</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sum[q.top()])</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sum[j],dp[j]);<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.push(j);<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">1</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">)<br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tdp[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(tdp[j],j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sum[j]);<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(dp,tdp,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;print();</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">57</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;<br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(ans,dp[n]);<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">)<br></span><span style="COLOR: #008080">61</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">s[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][i];<br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;print();</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">63</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img id=Codehighlighter1_1327_1735_Open_Image onclick="this.style.display='none'; Codehighlighter1_1327_1735_Open_Text.style.display='none'; Codehighlighter1_1327_1735_Closed_Image.style.display='inline'; Codehighlighter1_1327_1735_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1327_1735_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1327_1735_Closed_Text.style.display='none'; Codehighlighter1_1327_1735_Open_Image.style.display='inline'; Codehighlighter1_1327_1735_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1327_1735_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1327_1735_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">65</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">q.empty())&nbsp;q.pop();<br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;memset(tdp,0,sizeof(tdp));</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">67</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">s[</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">i];<br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.push(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">69</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tdp[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(dp[</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">sum[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">70</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">2</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">)<br></span><span style="COLOR: #008080">71</span><span style="COLOR: #000000"><img id=Codehighlighter1_1507_1610_Open_Image onclick="this.style.display='none'; Codehighlighter1_1507_1610_Open_Text.style.display='none'; Codehighlighter1_1507_1610_Closed_Image.style.display='inline'; Codehighlighter1_1507_1610_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1507_1610_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1507_1610_Closed_Text.style.display='none'; Codehighlighter1_1507_1610_Open_Image.style.display='inline'; Codehighlighter1_1507_1610_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1507_1610_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1507_1610_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">72</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tdp[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(dp[j],dp[q.top()]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(q.top()</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sum[q.top()])</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sum[j]);<br></span><span style="COLOR: #008080">73</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.push(j);<br></span><span style="COLOR: #008080">74</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">75</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">1</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">)<br></span><span style="COLOR: #008080">76</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tdp[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(tdp[j],j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sum[j]);<br></span><span style="COLOR: #008080">77</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(dp,tdp,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));<br></span><span style="COLOR: #008080">78</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;print();</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">79</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">80</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(ans,dp[n]);<br></span><span style="COLOR: #008080">81</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">ans</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">82</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;system("pause");</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">83</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></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">84</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">85</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/135547.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-12-06 01:54 <a href="http://www.cppblog.com/yzhw/archive/2010/12/06/135547.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 2411 Mondriaan's Dream 状态压缩DP</title><link>http://www.cppblog.com/yzhw/archive/2010/11/25/134661.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Thu, 25 Nov 2010 11:18:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/11/25/134661.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/134661.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/11/25/134661.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/134661.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/134661.html</trackback:ping><description><![CDATA[简要题意：<br>给出一个n*m的矩形，要求用1*2的矩形拼出来（可以旋转），问总共有多少种拼法。<br><br>题解：<br>将第i行进行状态压缩表示<br>(0,0,0,0..0) 0代表向下没有插头，1代表向下有插头<br>然后向下一行转移方法就是<br>如果当前列有插头，则取消掉插头；<br>如果当前列没有插头：<br>1、如果下一列也没有插头，可以水平放一个小矩形或者竖直放两个小矩形，插头状态不变<br>2、如果下一列有插头，则竖直放一个小矩形，添加两个向下的插头<br>注意要用<span style="color: red;">long long</span><br>代码：<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">#&nbsp;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>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">cstring</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#&nbsp;define&nbsp;getbit(c,pos)&nbsp;(((c)</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">(</span><span style="color: #000000;">1</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">(pos)))</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">0</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&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;5</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;add(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;code,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;now,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;m,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;dp[],</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;num)<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(now</span><span style="color: #000000;">==</span><span style="color: #000000;">m)<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[code]</span><span style="color: #000000;">+=</span><span style="color: #000000;">num;<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">switch</span><span style="color: #000000;">(getbit(code,now))<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">case</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">:<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(code</span><span style="color: #000000;">-</span><span style="color: #000000;">(</span><span style="color: #000000;">1</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">now),now</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,m,dp,num);<br></span><span style="color: #008080;">15</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;">break</span><span style="color: #000000;">;<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">case</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">:<br></span><span style="color: #008080;">17</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;">(now</span><span style="color: #000000;">!=</span><span style="color: #000000;">m</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&amp;&amp;!</span><span style="color: #000000;">getbit(code,now</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">))<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(code,now</span><span style="color: #000000;">+</span><span style="color: #000000;">2</span><span style="color: #000000;">,m,dp,num);<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(code</span><span style="color: #000000;">+</span><span style="color: #000000;">(</span><span style="color: #000000;">1</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">now),now</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,m,dp,num);<br></span><span style="color: #008080;">20</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;">break</span><span style="color: #000000;">;<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">24</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;">25</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n,m;<br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">)<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</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;">30</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">n</span><span style="color: #000000;">&amp;&amp;!</span><span style="color: #000000;">m)&nbsp;</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;dp[</span><span style="color: #000000;">15</span><span style="color: #000000;">][</span><span style="color: #000000;">2048</span><span style="color: #000000;">];<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(dp));<br></span><span style="color: #008080;">33</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="color: #000000;">0</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&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;">)<br></span><span style="color: #008080;">35</span>&nbsp;<span style="color: #000000;">&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;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;">(</span><span style="color: #000000;">1</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">m);j</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">36</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;">(dp[i][j])<br></span><span style="color: #008080;">37</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(j,</span><span style="color: #000000;">0</span><span style="color: #000000;">,m,dp[i</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">],dp[i][j]);<br></span><span style="color: #008080;">38</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">dp[n][</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;<br></span><span style="color: #008080;">39</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">40</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;">41</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">42</span>&nbsp;<span style="color: #000000;"></span></div>
<br><br><br><img src ="http://www.cppblog.com/yzhw/aggbug/134661.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-11-25 19:18 <a href="http://www.cppblog.com/yzhw/archive/2010/11/25/134661.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1935 Journey 树形DP</title><link>http://www.cppblog.com/yzhw/archive/2010/11/16/133740.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Mon, 15 Nov 2010 18:06:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/11/16/133740.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/133740.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/11/16/133740.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/133740.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/133740.html</trackback:ping><description><![CDATA[简明题意：给出一个城市的道路网(是一棵树)，每条路有一定的权值，一个人在第k点，给出一些城市列表，问这个人游览完这些城市最小花费为多少<br>解法：一条最优的路线肯定是这样 <br><img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/yzhw/pic.jpg" width=345 height=218><br>有且仅有一条路线是单向的。<br>下面定义状态:<br>dp[i][0]为游览完以i为根节点的子树（仅仅游览需要游览的城市，如果没有即为0）且最后回到i节点需要的最短长度 <br>dp[i][1]为不需要回到i节点的最短长度 <br>dp[i][0]=sum(dp[j][0]+val[i][j](如果dp[j][0]不为0或者j为需要访问的城市)),j为i的孩子节点 <br>dp[i][1]=dp[i][0]-max(dp[j][0]-dp[j][1]+val[i][j](如果dp[j][0]不为0或者j为需要访问的城市)) <br>最后结果就是dp[start][1] <br>程序如下 <br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#&nbsp;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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;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;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">#&nbsp;include&nbsp;&lt;algorithm&gt;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;define&nbsp;max(a,b)&nbsp;((a)</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">(b)</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">(a):(b))<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dp[</span><span style="COLOR: #000000">50001</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;need[</span><span style="COLOR: #000000">50001</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;g[</span><span style="COLOR: #000000">50001</span><span style="COLOR: #000000">],nxt[</span><span style="COLOR: #000000">100005</span><span style="COLOR: #000000">],val[</span><span style="COLOR: #000000">100005</span><span style="COLOR: #000000">],v[</span><span style="COLOR: #000000">100005</span><span style="COLOR: #000000">],c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">inline&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;insert(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;b,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;p)<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_265_320_Open_Image onclick="this.style.display='none'; Codehighlighter1_265_320_Open_Text.style.display='none'; Codehighlighter1_265_320_Closed_Image.style.display='inline'; Codehighlighter1_265_320_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_265_320_Closed_Image onclick="this.style.display='none'; Codehighlighter1_265_320_Closed_Text.style.display='none'; Codehighlighter1_265_320_Open_Image.style.display='inline'; Codehighlighter1_265_320_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_265_320_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_265_320_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;v[c]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">b;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;val[c]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">p;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;nxt[c]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">g[a];<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;g[a]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;solve(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;pos,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;pre)<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img id=Codehighlighter1_350_701_Open_Image onclick="this.style.display='none'; Codehighlighter1_350_701_Open_Text.style.display='none'; Codehighlighter1_350_701_Closed_Image.style.display='inline'; Codehighlighter1_350_701_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_350_701_Closed_Image onclick="this.style.display='none'; Codehighlighter1_350_701_Closed_Text.style.display='none'; Codehighlighter1_350_701_Open_Image.style.display='inline'; Codehighlighter1_350_701_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_350_701_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_350_701_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;maxnum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[pos][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[pos][</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">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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;p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">g[pos];p</span><span style="COLOR: #000000">!=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">nxt[p])<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(v[p]</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">pre)<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img id=Codehighlighter1_467_664_Open_Image onclick="this.style.display='none'; Codehighlighter1_467_664_Open_Text.style.display='none'; Codehighlighter1_467_664_Closed_Image.style.display='inline'; Codehighlighter1_467_664_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_467_664_Closed_Image onclick="this.style.display='none'; Codehighlighter1_467_664_Closed_Text.style.display='none'; Codehighlighter1_467_664_Open_Image.style.display='inline'; Codehighlighter1_467_664_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_467_664_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_467_664_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;solve(v[p],pos);<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxnum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">max(dp[v[p]][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">dp[v[p]][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">(dp[v[p]][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">need[v[p]]</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">val[p]:</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">),maxnum);<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[pos][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">dp[v[p]][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">(dp[v[p]][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">need[v[p]]</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">val[p]:</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[pos][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[pos][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">maxnum;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img id=Codehighlighter1_714_1191_Open_Image onclick="this.style.display='none'; Codehighlighter1_714_1191_Open_Text.style.display='none'; Codehighlighter1_714_1191_Closed_Image.style.display='inline'; Codehighlighter1_714_1191_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_714_1191_Closed_Image onclick="this.style.display='none'; Codehighlighter1_714_1191_Closed_Text.style.display='none'; Codehighlighter1_714_1191_Open_Image.style.display='inline'; Codehighlighter1_714_1191_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_714_1191_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_714_1191_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,start,num;<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">start);<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;memset(g,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(g));<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;memset(need,</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(need));<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">)<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img id=Codehighlighter1_890_991_Open_Image onclick="this.style.display='none'; Codehighlighter1_890_991_Open_Text.style.display='none'; Codehighlighter1_890_991_Closed_Image.style.display='inline'; Codehighlighter1_890_991_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_890_991_Closed_Image onclick="this.style.display='none'; Codehighlighter1_890_991_Closed_Text.style.display='none'; Codehighlighter1_890_991_Open_Image.style.display='inline'; Codehighlighter1_890_991_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_890_991_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_890_991_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a,b,p;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">b,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">p);<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(a,b,p);<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(b,a,p);<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">num);<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(num</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img id=Codehighlighter1_1036_1097_Open_Image onclick="this.style.display='none'; Codehighlighter1_1036_1097_Open_Text.style.display='none'; Codehighlighter1_1036_1097_Closed_Image.style.display='inline'; Codehighlighter1_1036_1097_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1036_1097_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1036_1097_Closed_Text.style.display='none'; Codehighlighter1_1036_1097_Open_Image.style.display='inline'; Codehighlighter1_1036_1097_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1036_1097_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1036_1097_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;t;<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">t);<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;need[t]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;solve(start,</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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,dp[start][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;system("pause");</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">54</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></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">55</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/133740.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-11-16 02:06 <a href="http://www.cppblog.com/yzhw/archive/2010/11/16/133740.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 2228 Naptime 简单DP，写的很挫，卡常数卡过去了。。。</title><link>http://www.cppblog.com/yzhw/archive/2010/11/07/132825.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sat, 06 Nov 2010 18:48:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/11/07/132825.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/132825.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/11/07/132825.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/132825.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/132825.html</trackback:ping><description><![CDATA[题意大概描述一下<br>有一只牛，在全天N小时内花B小时睡觉，然后每一个时间点都有一个休息值，假设选择在区间[s,e]内休息，则第s个时间点不能够获得休息值。现在请安排这只牛的作息时间，使得他的休息值最大。<br>状态dp[i][k][2]，i表示前i个小时，k表示休息了k个小时，最后一个状态表示最后一个小时是否在休息。然后由于可以形成环（即第N个小时在休息，则第1个小时能够获得能量），需要2个DP。状态转移应该很简单吧~具体看程序。话说这题谁有nlogn的方法?感觉n<sup>2</sup>好悬，常数还不小。。本机上跑了近1秒，用位运算、滚动数组等还有800ms，提交上去只有200ms。。看来poj的服务器很NB<br>贴代码<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">#&nbsp;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>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">cstring</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</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;4</span>&nbsp;<span style="color: #000000;">#&nbsp;define&nbsp;max(a,b)&nbsp;((a)</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(b)</span><span style="color: #000000;">?</span><span style="color: #000000;">(a):(b))<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;data[</span><span style="color: #000000;">3850</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;dp[</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">3850</span><span style="color: #000000;">],dp1[</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">3850</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;7</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;">&nbsp;8</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;freopen("input.txt","r",stdin);<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;freopen("output.txt","w",stdout);</span><span style="color: #008000;"><br></span><span style="color: #008080;">11</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;n,k;<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d%d</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">k);<br></span><span style="color: #008080;">13</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;">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;">)<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&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;">,data</span><span style="color: #000000;">+</span><span style="color: #000000;">i);<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(dp));<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;memset(dp1,</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(dp1));<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="color: #000000;">0</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][</span><span style="color: #000000;">0</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;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;dp[</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;">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;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;dp1[</span><span style="color: #000000;">0</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][</span><span style="color: #000000;">0</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;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;dp1[</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;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">data[</span><span style="color: #000000;">0</span><span style="color: #000000;">];<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;res</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">22</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;">n;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">],</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">]));<br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">],</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">]));<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;j</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">;j</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">k;j</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">29</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;">(dp[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j]</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j]</span><span style="color: #000000;">=</span><span style="color: #000000;">max(dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j],dp[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j]);<br></span><span style="color: #008080;">31</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;">(dp[(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;">2</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;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j]</span><span style="color: #000000;">=</span><span style="color: #000000;">max(dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j],dp[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">1</span><span style="color: #000000;">][j]);<br></span><span style="color: #008080;">33</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;">(dp[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</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;">max(dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">1</span><span style="color: #000000;">][j],dp[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]);<br></span><span style="color: #008080;">35</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;">(dp[(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;">2</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;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">36</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</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;">max(dp[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">1</span><span style="color: #000000;">][j],dp[(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;">2</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;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">+</span><span style="color: #000000;">data[i]);<br></span><span style="color: #008080;">37</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;">(dp1[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j]</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">38</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j]</span><span style="color: #000000;">=</span><span style="color: #000000;">max(dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j],dp1[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j]);<br></span><span style="color: #008080;">39</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;">(dp1[(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;">2</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;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">40</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j]</span><span style="color: #000000;">=</span><span style="color: #000000;">max(dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j],dp1[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">1</span><span style="color: #000000;">][j]);<br></span><span style="color: #008080;">41</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;">(dp1[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">42</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</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;">max(dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">1</span><span style="color: #000000;">][j],dp1[(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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][j</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]);<br></span><span style="color: #008080;">43</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;">(dp1[(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;">2</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;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">44</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</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;">max(dp1[i</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">1</span><span style="color: #000000;">][j],dp1[(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;">2</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;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">+</span><span style="color: #000000;">data[i]);<br></span><span style="color: #008080;">45</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">46</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">47</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;res</span><span style="color: #000000;">=</span><span style="color: #000000;">max(max(res,dp1[(n</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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">1</span><span style="color: #000000;">][k]),max(dp[(n</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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">1</span><span style="color: #000000;">][k],dp[(n</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;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">][k]));<br></span><span style="color: #008080;">48</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,res);<br></span><span style="color: #008080;">49</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;">50</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">51</span>&nbsp;<span style="color: #000000;"></span></div>
<br><br><img src ="http://www.cppblog.com/yzhw/aggbug/132825.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-11-07 02:48 <a href="http://www.cppblog.com/yzhw/archive/2010/11/07/132825.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 2018 Best Cow Fences 斜率优化，注意中间运算结果超int</title><link>http://www.cppblog.com/yzhw/archive/2010/11/07/132818.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sat, 06 Nov 2010 17:53:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/11/07/132818.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/132818.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/11/07/132818.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/132818.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/132818.html</trackback:ping><description><![CDATA[具体分析间NOI论文：<br>浅谈数形结合思想在信息学竞赛中的应用，例题2<br>直接贴代码了。。<br><span style="color: red;">再次注意消除浮点误差时中间运算结果超出int范围！！</span><br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">#&nbsp;define&nbsp;N&nbsp;</span><span style="color: #000000;">100005</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#&nbsp;define&nbsp;less(x1,y1,x2,y2,x3,y3,x4,y4)&nbsp;(y2</span><span style="color: #000000;">-</span><span style="color: #000000;">y1)</span><span style="color: #000000;">*</span><span style="color: #000000;">(x4</span><span style="color: #000000;">-</span><span style="color: #000000;">x3)</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">(y4</span><span style="color: #000000;">-</span><span style="color: #000000;">y3)</span><span style="color: #000000;">*</span><span style="color: #000000;">(x2</span><span style="color: #000000;">-</span><span style="color: #000000;">x1)<br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">#&nbsp;define&nbsp;max(a,b)&nbsp;((a)</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(b)</span><span style="color: #000000;">?</span><span style="color: #000000;">(a):(b))<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;data[N],q[N][</span><span style="color: #000000;">2</span><span style="color: #000000;">],s</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">,e</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n,f;<br></span><span style="color: #008080;">&nbsp;7</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;">&nbsp;8</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d%d</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">f);<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;data[</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&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;">)<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&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;">,data</span><span style="color: #000000;">+</span><span style="color: #000000;">i);<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[i]</span><span style="color: #000000;">+=</span><span style="color: #000000;">data[i</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;e</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;q[e][</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;q[e][</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;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">double</span><span style="color: #000000;">&nbsp;res;<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;y</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">,x</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&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;">)<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">24</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;">(s</span><span style="color: #000000;">+</span><span style="color: #000000;">2</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">e</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">i</span><span style="color: #000000;">-</span><span style="color: #000000;">q[s</span><span style="color: #000000;">+</span><span style="color: #000000;">2</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">f)&nbsp;s</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">25</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;">(e</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">s</span><span style="color: #000000;">+</span><span style="color: #000000;">2</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">less(q[e][</span><span style="color: #000000;">0</span><span style="color: #000000;">],q[e][</span><span style="color: #000000;">1</span><span style="color: #000000;">],i,data[i],q[e</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">],q[e</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;">],q[e][</span><span style="color: #000000;">0</span><span style="color: #000000;">],q[e][</span><span style="color: #000000;">1</span><span style="color: #000000;">]))&nbsp;e</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q[e][</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">i;<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q[e][</span><span style="color: #000000;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">data[i];<br></span><span style="color: #008080;">29</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;">(s</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">e</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">i</span><span style="color: #000000;">-</span><span style="color: #000000;">q[s</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">f)<br></span><span style="color: #008080;">30</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;">(y</span><span style="color: #000000;">==-</span><span style="color: #000000;">1</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">x</span><span style="color: #000000;">==-</span><span style="color: #000000;">1</span><span style="color: #000000;">||</span><span style="color: #000000;">x</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">y</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">((</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">)data[i]</span><span style="color: #000000;">-</span><span style="color: #000000;">q[s</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;">x</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">)y</span><span style="color: #000000;">*</span><span style="color: #000000;">(i</span><span style="color: #000000;">-</span><span style="color: #000000;">q[s</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">]))<br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y</span><span style="color: #000000;">=</span><span style="color: #000000;">data[i]</span><span style="color: #000000;">-</span><span style="color: #000000;">q[s</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;">],x</span><span style="color: #000000;">=</span><span style="color: #000000;">i</span><span style="color: #000000;">-</span><span style="color: #000000;">q[s</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">][</span><span style="color: #000000;">0</span><span style="color: #000000;">];<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">33</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">)(y</span><span style="color: #000000;">*</span><span style="color: #000000;">1000</span><span style="color: #000000;">)</span><span style="color: #000000;">/</span><span style="color: #000000;">x);<br></span><span style="color: #008080;">34</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;">35</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br><img src ="http://www.cppblog.com/yzhw/aggbug/132818.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-11-07 01:53 <a href="http://www.cppblog.com/yzhw/archive/2010/11/07/132818.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku1278 BOAT dp+rmq</title><link>http://www.cppblog.com/yzhw/archive/2010/11/06/132667.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Sat, 06 Nov 2010 07:51:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/11/06/132667.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/132667.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/11/06/132667.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/132667.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/132667.html</trackback:ping><description><![CDATA[题目描述的好含糊<br>是这样的<br>有n个客户要租船，租期为t<sub>i</sub>天。每个用户给供应商若干个选择列表：d<sub>i</sub>,p<sub>i</sub>,即商户在d日期前提供服务的话（这里指提供完服务，即在d<sub>i</sub>-t<sub>i</sub>天前即将船租给用户）就可以支付p单位的钱。<br>租赁必须采取先到先服务的策略（可以选择不租给某个用户），即时间的分配是从前到后按顺序的（这个是划分状态的基础）。<br>好了。说说做法<br>设状态dp[i][j]为已经考虑完第i个用户且第i个用户在第j时间还船产生的最大效益。<br>dp方程就很明了了dp[i][j]=max{dp[i-1][j-t<sub>i</sub>]+<span style="color: red;">val[i]</span>}复杂度为O(n<sub>2</sub>*计算val的复杂度)<br><span style="color: red;">val</span><sub style="color: red;">i</sub>理解为在第i个用户的可选列表中d<sub>i</sub>&gt;=j中支付价格的最大值。这里如果朴素的实现的话就是O(n)复杂度，整个算法就是O(n<sup>3</sup>)，对于n=100这种数据应该没问题了。不过可以用rmq优化到O(logn)。具体实现方法见程序<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<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: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.util.</span><span style="color: #000000;">*</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;&nbsp;2</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;rmq<br></span><span style="color: #008080;">&nbsp;&nbsp;3</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;data[];<br></span><span style="color: #008080;">&nbsp;&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;arr[][];<br></span><span style="color: #008080;">&nbsp;&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;rmq(ArrayList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">choose</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;data)<br></span><span style="color: #008080;">&nbsp;&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.data</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[data.size()];<br></span><span style="color: #008080;">&nbsp;&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;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;">data.size();i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.data[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">data.get(i).mount;<br></span><span style="color: #008080;">&nbsp;11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[data.size()][</span><span style="color: #000000;">20</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init();<br></span><span style="color: #008080;">&nbsp;13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;init()<br></span><span style="color: #008080;">&nbsp;15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;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;">data.length;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i][</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">data[i];<br></span><span style="color: #008080;">&nbsp;18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;len</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">;len</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">data.length;len</span><span style="color: #000000;">&lt;&lt;=</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;19</span>&nbsp;<span style="color: #000000;">&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;i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;">+</span><span style="color: #000000;">(</span><span style="color: #000000;">1</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">len)</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">data.length;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i][len]</span><span style="color: #000000;">=</span><span style="color: #000000;">Math.max(arr[i][len</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;arr[i</span><span style="color: #000000;">+</span><span style="color: #000000;">(</span><span style="color: #000000;">1</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">(len</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">))][len</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]);<br></span><span style="color: #008080;">&nbsp;21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;query(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;s)<br></span><span style="color: #008080;">&nbsp;23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;e</span><span style="color: #000000;">=</span><span style="color: #000000;">data.length</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;len</span><span style="color: #000000;">=</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">)(Math.log(e</span><span style="color: #000000;">-</span><span style="color: #000000;">s</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;">Math.log(</span><span style="color: #000000;">2</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #000000;">1e</span><span style="color: #000000;">-</span><span style="color: #000000;">6</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;Math.max(arr[s][len],arr[e</span><span style="color: #000000;">-</span><span style="color: #000000;">(</span><span style="color: #000000;">1</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">len)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">][len]);<br></span><span style="color: #008080;">&nbsp;27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<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;">class</span><span style="color: #000000;">&nbsp;choose&nbsp;</span><span style="color: #0000ff;">implements</span><span style="color: #000000;">&nbsp;Comparable</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">choose</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;31</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;dead,mount;<br></span><span style="color: #008080;">&nbsp;33</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;choose(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;d,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;m)<br></span><span style="color: #008080;">&nbsp;34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;35</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dead</span><span style="color: #000000;">=</span><span style="color: #000000;">d;<br></span><span style="color: #008080;">&nbsp;36</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mount</span><span style="color: #000000;">=</span><span style="color: #000000;">m;<br></span><span style="color: #008080;">&nbsp;37</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;38</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;compareTo(choose&nbsp;pos)<br></span><span style="color: #008080;">&nbsp;39</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<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;">return</span><span style="color: #000000;">&nbsp;dead</span><span style="color: #000000;">-</span><span style="color: #000000;">pos.dead;<br></span><span style="color: #008080;">&nbsp;41</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;42</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;43</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;44</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Main&nbsp;{<br></span><span style="color: #008080;">&nbsp;45</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;46</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/**</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;47</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="color: #808080;">@param</span><span style="color: #008000;">&nbsp;args<br></span><span style="color: #008080;">&nbsp;48</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;49</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;find(ArrayList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">choose</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;data,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;pos)<br></span><span style="color: #008080;">&nbsp;50</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;51</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;s</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,e</span><span style="color: #000000;">=</span><span style="color: #000000;">data.size()</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;52</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;">(s</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">e)<br></span><span style="color: #008080;">&nbsp;53</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;54</span>&nbsp;<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;mid</span><span style="color: #000000;">=</span><span style="color: #000000;">(s</span><span style="color: #000000;">+</span><span style="color: #000000;">e)</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;55</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;">(data.get(mid).dead</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">pos)<br></span><span style="color: #008080;">&nbsp;56</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e</span><span style="color: #000000;">=</span><span style="color: #000000;">mid</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;57</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&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;">&nbsp;58</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</span><span style="color: #000000;">=</span><span style="color: #000000;">mid</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;59</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;60</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;e</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;61</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;62</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;main(String[]&nbsp;args)&nbsp;{<br></span><span style="color: #008080;">&nbsp;63</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scanner&nbsp;in</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Scanner(System.in);<br></span><span style="color: #008080;">&nbsp;64</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;">(in.hasNext())<br></span><span style="color: #008080;">&nbsp;65</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;66</span>&nbsp;<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;num</span><span style="color: #000000;">=</span><span style="color: #000000;">in.nextInt();<br></span><span style="color: #008080;">&nbsp;67</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ArrayList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">choose</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;list[]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;ArrayList[num</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;68</span>&nbsp;<span style="color: #000000;">&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;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;">list.length;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;69</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list[i]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;ArrayList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">choose</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();<br></span><span style="color: #008080;">&nbsp;70</span>&nbsp;<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;time[]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[num</span><span style="color: #000000;">+</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;">&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;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;72</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">in.nextInt();<br></span><span style="color: #008080;">&nbsp;73</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num</span><span style="color: #000000;">=</span><span style="color: #000000;">in.nextInt();<br></span><span style="color: #008080;">&nbsp;74</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;">((num</span><span style="color: #000000;">--</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;">&nbsp;75</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;76</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: #0000ff;">int</span><span style="color: #000000;">&nbsp;id</span><span style="color: #000000;">=</span><span style="color: #000000;">in.nextInt();<br></span><span style="color: #008080;">&nbsp;77</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list[id].add(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;choose(in.nextInt(),in.nextInt()));<br></span><span style="color: #008080;">&nbsp;78</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;79</span>&nbsp;<span style="color: #000000;">&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;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;">time.length;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;80</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Collections.sort(list[i]);<br></span><span style="color: #008080;">&nbsp;81</span>&nbsp;<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;dp[]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[</span><span style="color: #000000;">250</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;82</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Arrays.fill(dp,</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;83</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;84</span>&nbsp;<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;res</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;85</span>&nbsp;<span style="color: #000000;">&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;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;">time.length;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;86</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;87</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rmq&nbsp;q</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;rmq(list[i]);<br></span><span style="color: #008080;">&nbsp;88</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: #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;">list[i].get(list[i].size()</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">).dead;j</span><span style="color: #000000;">-</span><span style="color: #000000;">time[i]</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;">)<br></span><span style="color: #008080;">&nbsp;89</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;90</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(dp[j</span><span style="color: #000000;">-</span><span style="color: #000000;">time[i]]</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">(dp[j]</span><span style="color: #000000;">==-</span><span style="color: #000000;">1</span><span style="color: #000000;">||</span><span style="color: #000000;">dp[j]</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">dp[j</span><span style="color: #000000;">-</span><span style="color: #000000;">time[i]]</span><span style="color: #000000;">+</span><span style="color: #000000;">q.query(find(list[i],j))))<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j]</span><span style="color: #000000;">=</span><span style="color: #000000;">dp[j</span><span style="color: #000000;">-</span><span style="color: #000000;">time[i]]</span><span style="color: #000000;">+</span><span style="color: #000000;">q.query(find(list[i],j));<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;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;93</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;94</span>&nbsp;<span style="color: #000000;">&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;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;">dp.length;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;95</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res</span><span style="color: #000000;">=</span><span style="color: #000000;">Math.max(dp[i],res);<br></span><span style="color: #008080;">&nbsp;96</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(res);<br></span><span style="color: #008080;">&nbsp;97</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();<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;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">100</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">101</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">102</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">103</span>&nbsp;<span style="color: #000000;"></span></div>
<br><br> <img src ="http://www.cppblog.com/yzhw/aggbug/132667.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-11-06 15:51 <a href="http://www.cppblog.com/yzhw/archive/2010/11/06/132667.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku 1661 Help Jimmy 线段树+阶段图DP</title><link>http://www.cppblog.com/yzhw/archive/2010/11/05/132570.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Fri, 05 Nov 2010 07:47:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/11/05/132570.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/132570.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/11/05/132570.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/132570.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/132570.html</trackback:ping><description><![CDATA[<div class="ptx" lang="zh-CN"> "Help Jimmy" 是在下图所示的场景上完成的游戏。
<br><center><img  src="http://poj.org/images/1661_1.jpg"></center>
<br>场景中包括多个长度和高度各不相同的平台。地面是最低的平台，高度为零，长度无限。
<br>
<br>Jimmy老鼠在时刻0从高于所有平台的某处开始下落，它的下落速度始终为1米/秒。当Jimmy落到某个平台上时，游戏者选择让它向左还是向右
跑，它跑动的速度也是1米/秒。当Jimmy跑到平台的边缘时，开始继续下落。Jimmy每次下落的高度不能超过MAX米，不然就会摔死，游戏也会结束。
<br>
<br>设计一个程序，计算Jimmy到底地面时可能的最早时间。
<br></div>
<p class="pst">Input</p>
<div class="ptx" lang="zh-CN">第一行是测试数据
的组数t（0 &lt;= t &lt;=
20）。每组测试数据的第一行是四个整数N，X，Y，MAX，用空格分隔。N是平台的数目（不包括地面），X和Y是Jimmy开始下落的位置的横竖坐
标，MAX是一次下落的最大高度。接下来的N行每行描述一个平台，包括三个整数，X1[i]，X2[i]和H[i]。H[i]表示平台的高度，X1[i]
和X2[i]表示平台左右端点的横坐标。1 &lt;= N &lt;= 1000，-20000 &lt;= X, X1[i], X2[i]
&lt;= 20000，0 &lt; H[i] &lt; Y &lt;= 20000（i = 1..N）。所有坐标的单位都是米。
<br>
<br>Jimmy的大小和平台的厚度均忽略不计。如果Jimmy恰好落在某个平台的边缘，被视为落在平台上。所有的平台均不重叠或相连。测试数据保证问题一定有解。
<br></div>
<p class="pst">Output</p>
<div class="ptx" lang="zh-CN">对输入的每组测试数据，输出一个整数，Jimmy到底地面时可能的最早时间。</div>
<p class="pst">Sample Input</p>
<pre class="sio">1<br>3 8 17 20<br>0 10 8<br>0 10 13<br>4 14 3</pre>
<p class="pst">Sample Output</p>
<pre class="sio">23</pre>
<p class="pst">解法是这样</p>
<p class="pst">按照y坐标给每个区间排序，以线段端点为节点构图，每个节点最多有2条边，用线段树维护当前线段端点下方的线段。最后求最长路就可以了。</p>
<p class="pst">原来想简化，不要创建s和e节点，结果反而悲剧，各种漏考虑情况。。以后这类题目还是谨慎点吧。。</p>
<p class="pst">
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<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;">#&nbsp;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;&nbsp;2</span>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">algorithm</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;&nbsp;3</span>&nbsp;<span style="color: #000000;">#&nbsp;include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">cstring</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;&nbsp;4</span>&nbsp;<span style="color: #000000;">#&nbsp;define&nbsp;mid(p)&nbsp;((st[p].s</span><span style="color: #000000;">+</span><span style="color: #000000;">st[p].e)</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;&nbsp;5</span>&nbsp;<span style="color: #000000;">#&nbsp;define&nbsp;abs(a)&nbsp;((a)</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">0</span><span style="color: #000000;">?</span><span style="color: #000000;">(a):</span><span style="color: #000000;">-</span><span style="color: #000000;">(a))<br></span><span style="color: #008080;">&nbsp;&nbsp;6</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;7</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;N</span><span style="color: #000000;">=</span><span style="color: #000000;">150000</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;&nbsp;8</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;g[</span><span style="color: #000000;">2005</span><span style="color: #000000;">],nxt[</span><span style="color: #000000;">5005</span><span style="color: #000000;">],val[</span><span style="color: #000000;">5005</span><span style="color: #000000;">],v[</span><span style="color: #000000;">5005</span><span style="color: #000000;">],c,len[</span><span style="color: #000000;">2005</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;&nbsp;9</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">struct</span><span style="color: #000000;"><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;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;s,e,id;<br></span><span style="color: #008080;">&nbsp;12</span>&nbsp;<span style="color: #000000;">}st[N];<br></span><span style="color: #008080;">&nbsp;13</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;node<br></span><span style="color: #008080;">&nbsp;14</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;h,s,e;<br></span><span style="color: #008080;">&nbsp;16</span>&nbsp;<span style="color: #000000;">}data[</span><span style="color: #000000;">1005</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;17</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;solve(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;pos)<br></span><span style="color: #008080;">&nbsp;18</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(len[pos]</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;len[pos];<br></span><span style="color: #008080;">&nbsp;20</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;21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;22</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;">(g[pos]</span><span style="color: #000000;">==-</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;pos</span><span style="color: #000000;">?</span><span style="color: #000000;">0xfffffff</span><span style="color: #000000;">:</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;23</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;24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len[pos]</span><span style="color: #000000;">=</span><span style="color: #000000;">0xfffffff</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;26</span>&nbsp;<span style="color: #000000;">&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;p</span><span style="color: #000000;">=</span><span style="color: #000000;">g[pos];p</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;p</span><span style="color: #000000;">=</span><span style="color: #000000;">nxt[p])<br></span><span style="color: #008080;">&nbsp;27</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;28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len[pos]</span><span style="color: #000000;">=</span><span style="color: #000000;">min(len[pos],solve(v[p])</span><span style="color: #000000;">+</span><span style="color: #000000;">val[p]);<br></span><span style="color: #008080;">&nbsp;29</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;30</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;len[pos];<br></span><span style="color: #008080;">&nbsp;31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;33</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;34</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;cmp(</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;node&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">a,</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;node&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">b)<br></span><span style="color: #008080;">&nbsp;35</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;36</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;a.h</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">b.h;<br></span><span style="color: #008080;">&nbsp;37</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;38</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;init(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;s,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;e,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;pos</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;39</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;40</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;st[pos].s</span><span style="color: #000000;">=</span><span style="color: #000000;">s;<br></span><span style="color: #008080;">&nbsp;41</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;st[pos].e</span><span style="color: #000000;">=</span><span style="color: #000000;">e;<br></span><span style="color: #008080;">&nbsp;42</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;st[pos].id</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;43</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(e</span><span style="color: #000000;">!=</span><span style="color: #000000;">s</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;44</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;45</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init(s,mid(pos),pos</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;46</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init(mid(pos),e,(pos</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;47</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;48</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;49</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;target,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;pos</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">)<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;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(st[pos].id</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;st[pos].id;<br></span><span style="color: #008080;">&nbsp;52</span>&nbsp;<span style="color: #000000;">&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;">(target</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">mid(pos))&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">(target,(pos</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;53</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">(target,(pos</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">1</span><span style="color: #000000;">));<br></span><span style="color: #008080;">&nbsp;54</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;55</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;insert(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;s,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;e,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;id,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;pos</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;56</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;57</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(st[pos].s</span><span style="color: #000000;">==</span><span style="color: #000000;">s</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">st[pos].e</span><span style="color: #000000;">==</span><span style="color: #000000;">e)&nbsp;<br></span><span style="color: #008080;">&nbsp;58</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st[pos].id</span><span style="color: #000000;">=</span><span style="color: #000000;">id;<br></span><span style="color: #008080;">&nbsp;59</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;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;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(st[pos].id</span><span style="color: #000000;">!=-</span><span style="color: #000000;">1</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;{<br></span><span style="color: #008080;">&nbsp;63</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st[pos</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">1</span><span style="color: #000000;">].id</span><span style="color: #000000;">=</span><span style="color: #000000;">st[(pos</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">].id</span><span style="color: #000000;">=</span><span style="color: #000000;">st[pos].id;<br></span><span style="color: #008080;">&nbsp;64</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st[pos].id</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;65</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<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: #0000ff;">if</span><span style="color: #000000;">(s</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">mid(pos))&nbsp;insert(s,e,id,(pos</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;67</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;">(e</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">mid(pos))&nbsp;insert(s,e,id,pos</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;68</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;69</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;70</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(s,mid(pos),id,pos</span><span style="color: #000000;">&lt;&lt;</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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(mid(pos),e,id,(pos</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;72</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;73</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;74</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;75</span>&nbsp;<span style="color: #000000;">inline&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;addedge(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;s,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;e,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;len)<br></span><span style="color: #008080;">&nbsp;76</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;77</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;v[c]</span><span style="color: #000000;">=</span><span style="color: #000000;">e;<br></span><span style="color: #008080;">&nbsp;78</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;val[c]</span><span style="color: #000000;">=</span><span style="color: #000000;">len;<br></span><span style="color: #008080;">&nbsp;79</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;nxt[c]</span><span style="color: #000000;">=</span><span style="color: #000000;">g[s];<br></span><span style="color: #008080;">&nbsp;80</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;g[s]</span><span style="color: #000000;">=</span><span style="color: #000000;">c</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;81</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;82</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;">&nbsp;83</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;84</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;test;<br></span><span style="color: #008080;">&nbsp;85</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">test);<br></span><span style="color: #008080;">&nbsp;86</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(test</span><span style="color: #000000;">--</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;87</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;88</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n,x,y,maxnum;<br></span><span style="color: #008080;">&nbsp;89</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d%d%d%d</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">x,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">y,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">maxnum);<br></span><span style="color: #008080;">&nbsp;90</span>&nbsp;<span style="color: #000000;">&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;">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;">)<br></span><span style="color: #008080;">&nbsp;91</span>&nbsp;<span style="color: #000000;">&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;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d%d%d</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">data[i].s,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">data[i].e,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">data[i].h);<br></span><span style="color: #008080;">&nbsp;93</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[i].s</span><span style="color: #000000;">+=</span><span style="color: #000000;">20000</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;94</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[i].e</span><span style="color: #000000;">+=</span><span style="color: #000000;">20000</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;95</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;96</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span><span style="color: #000000;">+=</span><span style="color: #000000;">20000</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;97</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[</span><span style="color: #000000;">0</span><span style="color: #000000;">].h</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;98</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(data</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,data</span><span style="color: #000000;">+</span><span style="color: #000000;">n</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,cmp);<br></span><span style="color: #008080;">&nbsp;99</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init(</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #000000;">40001</span><span style="color: #000000;">);<br></span><span style="color: #008080;">100</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #000000;">40001</span><span style="color: #000000;">,</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br></span><span style="color: #008080;">101</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(g,</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(g));<br></span><span style="color: #008080;">102</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">103</span>&nbsp;<span style="color: #000000;">&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;">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;">)<br></span><span style="color: #008080;">104</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">105</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;p</span><span style="color: #000000;">=</span><span style="color: #0000ff;">get</span><span style="color: #000000;">(data[i].s);<br></span><span style="color: #008080;">106</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(data[i].h</span><span style="color: #000000;">-</span><span style="color: #000000;">data[p].h</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">maxnum)<br></span><span style="color: #008080;">107</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addedge(</span><span style="color: #000000;">2</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;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">p</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">:</span><span style="color: #000000;">0</span><span style="color: #000000;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">abs(data[i].s</span><span style="color: #000000;">-</span><span style="color: #000000;">data[p].s):</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br></span><span style="color: #008080;">109</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addedge(</span><span style="color: #000000;">2</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;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">p:</span><span style="color: #000000;">0</span><span style="color: #000000;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">abs(data[i].s</span><span style="color: #000000;">-</span><span style="color: #000000;">data[p].e):</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br></span><span style="color: #008080;">110</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">111</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">=</span><span style="color: #0000ff;">get</span><span style="color: #000000;">(data[i].e);<br></span><span style="color: #008080;">112</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(data[i].h</span><span style="color: #000000;">-</span><span style="color: #000000;">data[p].h</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">maxnum)<br></span><span style="color: #008080;">113</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">114</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addedge(</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">i,p</span><span style="color: #000000;">?</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">p</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">:</span><span style="color: #000000;">0</span><span style="color: #000000;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">abs(data[i].e</span><span style="color: #000000;">-</span><span style="color: #000000;">data[p].s):</span><span style="color: #000000;">0</span><span style="color: #000000;">);<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;&nbsp;&nbsp;addedge(</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">i,p</span><span style="color: #000000;">?</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">p:</span><span style="color: #000000;">0</span><span style="color: #000000;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">abs(data[i].e</span><span style="color: #000000;">-</span><span style="color: #000000;">data[p].e):</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br></span><span style="color: #008080;">116</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">117</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(data[i].s,data[i].e</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,i);<br></span><span style="color: #008080;">118</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">119</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">120</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;p</span><span style="color: #000000;">=</span><span style="color: #0000ff;">get</span><span style="color: #000000;">(x);<br></span><span style="color: #008080;">121</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addedge(</span><span style="color: #000000;">2</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;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">p</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">:</span><span style="color: #000000;">0</span><span style="color: #000000;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">abs(x</span><span style="color: #000000;">-</span><span style="color: #000000;">data[p].s):</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br></span><span style="color: #008080;">122</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addedge(</span><span style="color: #000000;">2</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;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">p:</span><span style="color: #000000;">0</span><span style="color: #000000;">,p</span><span style="color: #000000;">?</span><span style="color: #000000;">abs(x</span><span style="color: #000000;">-</span><span style="color: #000000;">data[p].e):</span><span style="color: #000000;">0</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;}<br></span><span style="color: #008080;">124</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(len,</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(len));<br></span><span style="color: #008080;">125</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,y</span><span style="color: #000000;">+</span><span style="color: #000000;">solve(</span><span style="color: #000000;">2</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;">));<br></span><span style="color: #008080;">126</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">127</span>&nbsp;<span style="color: #000000;">&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;">128</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">129</span>&nbsp;<span style="color: #000000;"></span></div>
<br></p><img src ="http://www.cppblog.com/yzhw/aggbug/132570.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-11-05 15:47 <a href="http://www.cppblog.com/yzhw/archive/2010/11/05/132570.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku2168 Joke with Turtles 区间DP 注意~</title><link>http://www.cppblog.com/yzhw/archive/2010/11/02/132078.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Mon, 01 Nov 2010 18:37:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/11/02/132078.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/132078.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/11/02/132078.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/132078.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/132078.html</trackback:ping><description><![CDATA[<p><strong><span>题目大意：</span></strong></p>
<p><span>有N只可爱的小海龟在赛跑。询问每只小海龟它是第几名，它会回答你两个数，ai,bi，分别表示在它前面的小海龟数和在它后面的小海龟数。接着你发现其中有些小海龟对你撒了谎，因为根据他们的说法你根本没法给他们排队！但是你是善良的，你不希望有很多小海龟在撒谎，想找出最少有哪几只小海龟在撒谎。（注意：小海龟的名次可能是并列的！）</span></p>
<p>&nbsp;</p>
<p><strong><span>分析：</span></strong></p>
<p><span>若一只海龟说了真话，那么该海龟的位置一定是在区间[ai+1,n-bi]<span><span> </span></span>上。若有k只海龟选择了相同的区间<span><span> </span></span>，则根据并列关系，该区间最多能同时拥有min{n-ai-bi,k}只海龟（<span style="COLOR: red">多出来的海龟肯定是说谎的，预先排除掉</span>）。可以计算出每个区间最多能有多少只海龟，把数值看做区间的&#8220;权&#8221;。则问题转化为，在一些带权区间中，选出权和最大的区间，使它们之间不能互相重叠。</span></p>
<p>&nbsp;</p>
<p><strong><span>算法：</span></strong></p>
<p><span>算出每个出现过的区间的&#8220;权<span>&#8221;vi<span> </span></span>，接下来的算法就是动态规划了。先按右端点坐标从小到大排序，令pi<span><span> </span></span>为在区间<span><span> i</span></span>左边的且与之无公共点的最大区间编号，设状态f[i]<span><span> </span></span>为在前i<span><span> </span></span>个区间中可选出区间的最大权和，则状态转移方程为f(i)=max(f(i-1),f(pi)+vi)<span><span> </span></span>，说真话海龟的最大数量就是最后一个区间的f(i)<span><span> </span></span>值。<br>论文：<a href="http://www.cppblog.com/Files/yzhw/range.rar">/Files/yzhw/range.rar</a><br><br><span style="COLOR: red">注意，在区间类问题中，要合理设计状态，究竟状态表示的是前i个区间（第i个区间不一定取）还是第i个区间一定要取的前i个区间。</span></span></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">map</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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;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;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;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;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;ans;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">map</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">,vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;refer;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;define&nbsp;pos(a,b)&nbsp;(((b)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">(a))<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_208_230_Open_Image onclick="this.style.display='none'; Codehighlighter1_208_230_Open_Text.style.display='none'; Codehighlighter1_208_230_Closed_Image.style.display='inline'; Codehighlighter1_208_230_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_208_230_Closed_Image onclick="this.style.display='none'; Codehighlighter1_208_230_Closed_Text.style.display='none'; Codehighlighter1_208_230_Open_Image.style.display='inline'; Codehighlighter1_208_230_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_208_230_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_208_230_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;s,e,val,per;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">ran[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dp[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;path[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;used[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;searchper(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;pos)<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_321_490_Open_Image onclick="this.style.display='none'; Codehighlighter1_321_490_Open_Text.style.display='none'; Codehighlighter1_321_490_Closed_Image.style.display='inline'; Codehighlighter1_321_490_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_321_490_Closed_Image onclick="this.style.display='none'; Codehighlighter1_321_490_Closed_Text.style.display='none'; Codehighlighter1_321_490_Open_Image.style.display='inline'; Codehighlighter1_321_490_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_321_490_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_321_490_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,mid,e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;e</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(s</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">e)<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img id=Codehighlighter1_373_474_Open_Image onclick="this.style.display='none'; Codehighlighter1_373_474_Open_Text.style.display='none'; Codehighlighter1_373_474_Closed_Image.style.display='inline'; Codehighlighter1_373_474_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_373_474_Closed_Image onclick="this.style.display='none'; Codehighlighter1_373_474_Closed_Text.style.display='none'; Codehighlighter1_373_474_Open_Image.style.display='inline'; Codehighlighter1_373_474_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_373_474_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_373_474_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mid</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(s</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">e)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ran[mid].e</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">pos)<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mid</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mid</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;e;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img id=Codehighlighter1_503_1766_Open_Image onclick="this.style.display='none'; Codehighlighter1_503_1766_Open_Text.style.display='none'; Codehighlighter1_503_1766_Closed_Image.style.display='inline'; Codehighlighter1_503_1766_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_503_1766_Closed_Image onclick="this.style.display='none'; Codehighlighter1_503_1766_Closed_Text.style.display='none'; Codehighlighter1_503_1766_Open_Image.style.display='inline'; Codehighlighter1_503_1766_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_503_1766_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_503_1766_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n;<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;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">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">)<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img id=Codehighlighter1_565_812_Open_Image onclick="this.style.display='none'; Codehighlighter1_565_812_Open_Text.style.display='none'; Codehighlighter1_565_812_Closed_Image.style.display='inline'; Codehighlighter1_565_812_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_565_812_Closed_Image onclick="this.style.display='none'; Codehighlighter1_565_812_Closed_Text.style.display='none'; Codehighlighter1_565_812_Open_Image.style.display='inline'; Codehighlighter1_565_812_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_565_812_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_565_812_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a,b;<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">b);<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(a</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">n)<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans.push_back(i);<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img id=Codehighlighter1_670_806_Open_Image onclick="this.style.display='none'; Codehighlighter1_670_806_Open_Text.style.display='none'; Codehighlighter1_670_806_Closed_Image.style.display='inline'; Codehighlighter1_670_806_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_670_806_Closed_Image onclick="this.style.display='none'; Codehighlighter1_670_806_Closed_Text.style.display='none'; Codehighlighter1_670_806_Open_Image.style.display='inline'; Codehighlighter1_670_806_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_670_806_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_670_806_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(refer[pos(a</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">b)].size()</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">a)<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;refer[pos(a</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">b)].push_back(i);<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans.push_back(i);<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(map</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">,vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">refer.begin();i</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">refer.end();i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img id=Codehighlighter1_894_1007_Open_Image onclick="this.style.display='none'; Codehighlighter1_894_1007_Open_Text.style.display='none'; Codehighlighter1_894_1007_Closed_Image.style.display='inline'; Codehighlighter1_894_1007_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_894_1007_Closed_Image onclick="this.style.display='none'; Codehighlighter1_894_1007_Closed_Text.style.display='none'; Codehighlighter1_894_1007_Open_Image.style.display='inline'; Codehighlighter1_894_1007_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_894_1007_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_894_1007_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ran[c].s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">first)</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">1023</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ran[c].e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">((i</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">first)</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ran[c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">].val</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">second).size();<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">c;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ran[i].per</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">searchper(ran[i].s);<br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;path[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">c;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">61</span><span style="COLOR: #000000"><img id=Codehighlighter1_1130_1281_Open_Image onclick="this.style.display='none'; Codehighlighter1_1130_1281_Open_Text.style.display='none'; Codehighlighter1_1130_1281_Closed_Image.style.display='inline'; Codehighlighter1_1130_1281_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1130_1281_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1130_1281_Closed_Text.style.display='none'; Codehighlighter1_1130_1281_Open_Image.style.display='inline'; Codehighlighter1_1130_1281_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1130_1281_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1130_1281_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(dp[i</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">dp[ran[i].per]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">ran[i].val)<br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,dp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">65</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,dp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[ran[i].per]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">ran[i].val;<br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">67</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;memset(used,</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(used));<br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">69</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(p)<br></span><span style="COLOR: #008080">70</span><span style="COLOR: #000000"><img id=Codehighlighter1_1352_1463_Open_Image onclick="this.style.display='none'; Codehighlighter1_1352_1463_Open_Text.style.display='none'; Codehighlighter1_1352_1463_Closed_Image.style.display='inline'; Codehighlighter1_1352_1463_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1352_1463_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1352_1463_Closed_Text.style.display='none'; Codehighlighter1_1352_1463_Open_Image.style.display='inline'; Codehighlighter1_1352_1463_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1352_1463_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1352_1463_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">71</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(path[p])<br></span><span style="COLOR: #008080">72</span><span style="COLOR: #000000"><img id=Codehighlighter1_1378_1432_Open_Image onclick="this.style.display='none'; Codehighlighter1_1378_1432_Open_Text.style.display='none'; Codehighlighter1_1378_1432_Closed_Image.style.display='inline'; Codehighlighter1_1378_1432_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1378_1432_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1378_1432_Closed_Text.style.display='none'; Codehighlighter1_1378_1432_Open_Image.style.display='inline'; Codehighlighter1_1378_1432_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1378_1432_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1378_1432_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">73</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;used[p]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">74</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">ran[p].per;<br></span><span style="COLOR: #008080">75</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">76</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">77</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">78</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">79</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">c;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">80</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">used[i])<br></span><span style="COLOR: #008080">81</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">refer[pos(ran[i].s,ran[i].e)].size();j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">82</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans.push_back(refer[pos(ran[i].s,ran[i].e)][j]);<br></span><span style="COLOR: #008080">83</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">,ans.size());<br></span><span style="COLOR: #008080">84</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">ans.size();i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">85</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,ans[i]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">86</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">87</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">88</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">89</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">90</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/132078.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-11-02 02:37 <a href="http://www.cppblog.com/yzhw/archive/2010/11/02/132078.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku3171 Cleaning Shifts 区间最小权覆盖，DP+单调队列</title><link>http://www.cppblog.com/yzhw/archive/2010/11/02/132076.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Mon, 01 Nov 2010 18:21:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/11/02/132076.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/132076.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/11/02/132076.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/132076.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/132076.html</trackback:ping><description><![CDATA[<p>题意是这样：有N个牛，可以为FJ工作一段时间[s<sub>i</sub>,t<sub>i</sub>]，FJ要支付给他p<sub>i</sub>工资。FJ需要整天都有牛为他工作，问他至少需要支付多少钱。<br>首先，可以对区间按照右端点为第一关键字排序，状态dp[pos]表示聘用第pos个牛时并且之前的区间都已经覆盖需要支付的钱，一个朴素的DP转移可以表示为<br>dp[pos]=min{dp[i]} e<sub>i</sub>&gt;=s<sub>i</sub>。当然，我们立刻会发现一点，如果决策dp[j]&gt;dp[i]，而j&lt;i，即e<sub>j</sub>&lt;e<sub>i</sub>，那么j这个决策是多余的。因此，我们可以维护一个单调队列，通过二分查找来找到满足e<sub>i</sub>&gt;=s<sub>i</sub>的dp[i]最小值，整个算法复杂度nlogn。<br>具体分析见周大牛论文<a href="http://www.cppblog.com/Files/yzhw/range.rar">/Files/yzhw/range.rar</a><br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.util.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;node&nbsp;</span><span style="COLOR: #0000ff">implements</span><span style="COLOR: #000000">&nbsp;Comparable</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">node</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 id=Codehighlighter1_77_149_Open_Image onclick="this.style.display='none'; Codehighlighter1_77_149_Open_Text.style.display='none'; Codehighlighter1_77_149_Closed_Image.style.display='inline'; Codehighlighter1_77_149_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_77_149_Closed_Image onclick="this.style.display='none'; Codehighlighter1_77_149_Closed_Text.style.display='none'; Codehighlighter1_77_149_Open_Image.style.display='inline'; Codehighlighter1_77_149_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_77_149_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_77_149_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;s,e,val;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;compareTo(node&nbsp;pos)<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_126_147_Open_Image onclick="this.style.display='none'; Codehighlighter1_126_147_Open_Text.style.display='none'; Codehighlighter1_126_147_Closed_Image.style.display='inline'; Codehighlighter1_126_147_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_126_147_Closed_Image onclick="this.style.display='none'; Codehighlighter1_126_147_Closed_Text.style.display='none'; Codehighlighter1_126_147_Open_Image.style.display='inline'; Codehighlighter1_126_147_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_126_147_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_126_147_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;e</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">pos.e;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_169_1708_Open_Image onclick="this.style.display='none'; Codehighlighter1_169_1708_Open_Text.style.display='none'; Codehighlighter1_169_1708_Closed_Image.style.display='inline'; Codehighlighter1_169_1708_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_169_1708_Closed_Image onclick="this.style.display='none'; Codehighlighter1_169_1708_Closed_Text.style.display='none'; Codehighlighter1_169_1708_Open_Image.style.display='inline'; Codehighlighter1_169_1708_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Main&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_169_1708_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_169_1708_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_173_196_Open_Image onclick="this.style.display='none'; Codehighlighter1_173_196_Open_Text.style.display='none'; Codehighlighter1_173_196_Closed_Image.style.display='inline'; Codehighlighter1_173_196_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_173_196_Closed_Image onclick="this.style.display='none'; Codehighlighter1_173_196_Closed_Text.style.display='none'; Codehighlighter1_173_196_Open_Image.style.display='inline'; Codehighlighter1_173_196_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_173_196_Closed_Text>/**&nbsp;*/</span><span id=Codehighlighter1_173_196_Open_Text><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000">&nbsp;args<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;node&nbsp;arr[];<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dp[];<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;stack[][],top</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;search(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;pos)<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img id=Codehighlighter1_295_431_Open_Image onclick="this.style.display='none'; Codehighlighter1_295_431_Open_Text.style.display='none'; Codehighlighter1_295_431_Closed_Image.style.display='inline'; Codehighlighter1_295_431_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_295_431_Closed_Image onclick="this.style.display='none'; Codehighlighter1_295_431_Closed_Text.style.display='none'; Codehighlighter1_295_431_Open_Image.style.display='inline'; Codehighlighter1_295_431_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_295_431_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_295_431_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">top,mid</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 align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(s</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">e)<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img id=Codehighlighter1_336_416_Open_Image onclick="this.style.display='none'; Codehighlighter1_336_416_Open_Text.style.display='none'; Codehighlighter1_336_416_Closed_Image.style.display='inline'; Codehighlighter1_336_416_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_336_416_Closed_Image onclick="this.style.display='none'; Codehighlighter1_336_416_Closed_Text.style.display='none'; Codehighlighter1_336_416_Open_Image.style.display='inline'; Codehighlighter1_336_416_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_336_416_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_336_416_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mid</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(s</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">e)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(stack[mid][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">pos)<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mid</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mid</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;s;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img id=Codehighlighter1_491_1705_Open_Image onclick="this.style.display='none'; Codehighlighter1_491_1705_Open_Text.style.display='none'; Codehighlighter1_491_1705_Closed_Image.style.display='inline'; Codehighlighter1_491_1705_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_491_1705_Closed_Image onclick="this.style.display='none'; Codehighlighter1_491_1705_Closed_Text.style.display='none'; Codehighlighter1_491_1705_Open_Image.style.display='inline'; Codehighlighter1_491_1705_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000">&nbsp;IOException</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_491_1705_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_491_1705_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scanner&nbsp;in</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Scanner(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;BufferedReader(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;InputStreamReader(System.in)));<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">in.nextInt(),m</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">in.nextInt(),e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">in.nextInt();<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;node[n];<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[n];<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[n][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">arr.length;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img id=Codehighlighter1_718_918_Open_Image onclick="this.style.display='none'; Codehighlighter1_718_918_Open_Text.style.display='none'; Codehighlighter1_718_918_Closed_Image.style.display='inline'; Codehighlighter1_718_918_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_718_918_Closed_Image onclick="this.style.display='none'; Codehighlighter1_718_918_Closed_Text.style.display='none'; Codehighlighter1_718_918_Open_Image.style.display='inline'; Codehighlighter1_718_918_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_718_918_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_718_918_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;node();<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i].s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">in.nextInt();<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i].e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">in.nextInt();<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(arr[i].e</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">arr[i].s)<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img id=Codehighlighter1_822_886_Open_Image onclick="this.style.display='none'; Codehighlighter1_822_886_Open_Text.style.display='none'; Codehighlighter1_822_886_Closed_Image.style.display='inline'; Codehighlighter1_822_886_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_822_886_Closed_Image onclick="this.style.display='none'; Codehighlighter1_822_886_Closed_Text.style.display='none'; Codehighlighter1_822_886_Open_Image.style.display='inline'; Codehighlighter1_822_886_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_822_886_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_822_886_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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">arr[i].e;<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i].e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[i].s;<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i].s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t;<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i].val</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">in.nextInt();<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Arrays.sort(arr);<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">Integer.MAX_VALUE;<br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;arr[i].s</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">m</span><span style="COLOR: #000000">&amp;&amp;</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">);<br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">arr[n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">].e</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">e)&nbsp;System.out.println(</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img id=Codehighlighter1_1061_1696_Open_Image onclick="this.style.display='none'; Codehighlighter1_1061_1696_Open_Text.style.display='none'; Codehighlighter1_1061_1696_Closed_Image.style.display='inline'; Codehighlighter1_1061_1696_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1061_1696_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1061_1696_Closed_Text.style.display='none'; Codehighlighter1_1061_1696_Open_Image.style.display='inline'; Codehighlighter1_1061_1696_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1061_1696_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1061_1696_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[i].val;<br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(arr[i].e</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">e)&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">Math.min(ans,&nbsp;dp[i]);<br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack[</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">top][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i];<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack[top][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[i].e;<br></span><span style="COLOR: #008080">61</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img id=Codehighlighter1_1205_1597_Open_Image onclick="this.style.display='none'; Codehighlighter1_1205_1597_Open_Text.style.display='none'; Codehighlighter1_1205_1597_Closed_Image.style.display='inline'; Codehighlighter1_1205_1597_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1205_1597_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1205_1597_Closed_Text.style.display='none'; Codehighlighter1_1205_1597_Open_Image.style.display='inline'; Codehighlighter1_1205_1597_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1205_1597_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1205_1597_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;pos</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">search(arr[i].s</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(pos</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">top)<br></span><span style="COLOR: #008080">65</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(arr[i].s</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">m)<br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[i].val;<br></span><span style="COLOR: #008080">67</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">69</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">70</span><span style="COLOR: #000000"><img id=Codehighlighter1_1339_1444_Open_Image onclick="this.style.display='none'; Codehighlighter1_1339_1444_Open_Text.style.display='none'; Codehighlighter1_1339_1444_Closed_Image.style.display='inline'; Codehighlighter1_1339_1444_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1339_1444_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1339_1444_Closed_Text.style.display='none'; Codehighlighter1_1339_1444_Open_Image.style.display='inline'; Codehighlighter1_1339_1444_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1339_1444_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1339_1444_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">71</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">stack[pos][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">arr[i].val;<br></span><span style="COLOR: #008080">72</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(arr[i].s</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">m)<br></span><span style="COLOR: #008080">73</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">Math.min(dp[i],&nbsp;arr[i].val);<br></span><span style="COLOR: #008080">74</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">75</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(top</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">stack[top][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">dp[i])&nbsp;top</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">76</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack[</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">top][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dp[i];<br></span><span style="COLOR: #008080">77</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack[top][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[i].e;<br></span><span style="COLOR: #008080">78</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(arr[i].e</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">e)&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">Math.min(ans,&nbsp;dp[i]);<br></span><span style="COLOR: #008080">79</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">80</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ans</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">Integer.MAX_VALUE)<br></span><span style="COLOR: #008080">81</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">82</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">83</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(ans);<br></span><span style="COLOR: #008080">84</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">85</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">86</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">87</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">88</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">89</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">90</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">91</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<p><br>&nbsp;</p>
<p><span></span>&nbsp;</p>
<img src ="http://www.cppblog.com/yzhw/aggbug/132076.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-11-02 02:21 <a href="http://www.cppblog.com/yzhw/archive/2010/11/02/132076.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pku2342 Anniversary party 求树的最大权独立集</title><link>http://www.cppblog.com/yzhw/archive/2010/11/02/132068.html</link><dc:creator>yzhw</dc:creator><author>yzhw</author><pubDate>Mon, 01 Nov 2010 17:48:00 GMT</pubDate><guid>http://www.cppblog.com/yzhw/archive/2010/11/02/132068.html</guid><wfw:comment>http://www.cppblog.com/yzhw/comments/132068.html</wfw:comment><comments>http://www.cppblog.com/yzhw/archive/2010/11/02/132068.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yzhw/comments/commentRss/132068.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yzhw/services/trackbacks/132068.html</trackback:ping><description><![CDATA[题意简要的说就是给一棵树，求其最大权独立集。<br>树上的问题一般都可以用DP来解决。<br>设dp[pos]是以pos为根的最大权独立集，那么dp[pos]=max{val[pos]+dp[son[son[pos]]],dp[son[pos]]}<br>附代码
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;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;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;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;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;define&nbsp;N&nbsp;</span><span style="COLOR: #000000">10000</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#&nbsp;define&nbsp;max(a,b)&nbsp;((a)</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">(b)</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">(a):(b))<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">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;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;val[N],dp[N],n;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;g[N];<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;solve(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;pos)<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_231_596_Open_Image onclick="this.style.display='none'; Codehighlighter1_231_596_Open_Text.style.display='none'; Codehighlighter1_231_596_Closed_Image.style.display='inline'; Codehighlighter1_231_596_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_231_596_Closed_Image onclick="this.style.display='none'; Codehighlighter1_231_596_Closed_Text.style.display='none'; Codehighlighter1_231_596_Open_Image.style.display='inline'; Codehighlighter1_231_596_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_231_596_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_231_596_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(dp[pos]</span><span style="COLOR: #000000">!=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;dp[pos];<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img id=Codehighlighter1_282_594_Open_Image onclick="this.style.display='none'; Codehighlighter1_282_594_Open_Text.style.display='none'; Codehighlighter1_282_594_Closed_Image.style.display='inline'; Codehighlighter1_282_594_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_282_594_Closed_Image onclick="this.style.display='none'; Codehighlighter1_282_594_Closed_Text.style.display='none'; Codehighlighter1_282_594_Open_Image.style.display='inline'; Codehighlighter1_282_594_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_282_594_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_282_594_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[pos]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;total</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">val[pos];<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</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">g[pos].size();i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_380_519_Open_Image onclick="this.style.display='none'; Codehighlighter1_380_519_Open_Text.style.display='none'; Codehighlighter1_380_519_Closed_Image.style.display='inline'; Codehighlighter1_380_519_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_380_519_Closed_Image onclick="this.style.display='none'; Codehighlighter1_380_519_Closed_Text.style.display='none'; Codehighlighter1_380_519_Open_Image.style.display='inline'; Codehighlighter1_380_519_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_380_519_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_380_519_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[pos]</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">solve(g[pos][i]);<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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">g[g[pos][i]].size();j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">solve(g[g[pos][i]][j]);<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[pos]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">max(dp[pos],total);<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;dp[pos];<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img id=Codehighlighter1_609_1012_Open_Image onclick="this.style.display='none'; Codehighlighter1_609_1012_Open_Text.style.display='none'; Codehighlighter1_609_1012_Closed_Image.style.display='inline'; Codehighlighter1_609_1012_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_609_1012_Closed_Image onclick="this.style.display='none'; Codehighlighter1_609_1012_Closed_Text.style.display='none'; Codehighlighter1_609_1012_Open_Image.style.display='inline'; Codehighlighter1_609_1012_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_609_1012_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_609_1012_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">)<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,val</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i);<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img id=Codehighlighter1_703_816_Open_Image onclick="this.style.display='none'; Codehighlighter1_703_816_Open_Text.style.display='none'; Codehighlighter1_703_816_Closed_Image.style.display='inline'; Codehighlighter1_703_816_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_703_816_Closed_Image onclick="this.style.display='none'; Codehighlighter1_703_816_Closed_Text.style.display='none'; Codehighlighter1_703_816_Open_Image.style.display='inline'; Codehighlighter1_703_816_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_703_816_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_703_816_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;now,fa;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">now,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">fa);<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">now</span><span style="COLOR: #000000">&amp;&amp;!</span><span style="COLOR: #000000">fa)&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g[fa].push_back(now);<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(dp));<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;total</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">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">)<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(dp[i]</span><span style="COLOR: #000000">==-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">max(total,solve(i));<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,total);<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;system("pause");</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">48</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></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">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/yzhw/aggbug/132068.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yzhw/" target="_blank">yzhw</a> 2010-11-02 01:48 <a href="http://www.cppblog.com/yzhw/archive/2010/11/02/132068.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>