﻿<?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++博客-Codes of Sephiroth-随笔分类-信息奥赛&amp;其他有关信息的东西</title><link>http://www.cppblog.com/sephiroth/category/14758.html</link><description>There is no need of color in my world.</description><language>zh-cn</language><lastBuildDate>Wed, 01 Sep 2010 15:48:40 GMT</lastBuildDate><pubDate>Wed, 01 Sep 2010 15:48:40 GMT</pubDate><ttl>60</ttl><item><title>&amp;ldquo;长沙一中学习&amp;rdquo;-day2</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/01/125561.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Wed, 01 Sep 2010 13:54:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/01/125561.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125561.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/01/125561.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125561.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125561.html</trackback:ping><description><![CDATA[<p>今天上午讲的是线性的动归。讲的例题有：</p> <ol> <li>机器分配模型</li> <li>船</li> <li>楼梯问题</li> <li>田忌赛马</li> <li>最长公共子串</li></ol> <p>然后就是有关矩形的动态规划。如下：</p> <ol> <li>滑雪</li> <li>工业时代</li></ol> <p>还有区间类的：</p> <ol> <li>凸多边形划分</li> <li>最大乘积</li> <li>石子合并（用到了四边形不等式）</li> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/09/01/125557.html">数字游戏</a></li></ol> <p>然后有三道测试题：</p> <ol> <li>四塔问题</li> <li>关灯</li> <li>任务安排</li></ol> <p>下午开始将树形的动态规划。</p> <ol> <li>聚会的快乐</li> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/09/01/125558.html">三色二叉树</a></li> <li>皇宫看守</li> <li>珠宝</li> <li>符文之旅（最大与最小）</li></ol> <p>没有写的题目以后会逐步完成。</p><img src ="http://www.cppblog.com/sephiroth/aggbug/125561.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-01 21:54 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/01/125561.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>树形动态规划-三色二叉树</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/01/125558.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Wed, 01 Sep 2010 13:41:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/01/125558.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125558.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/01/125558.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125558.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125558.html</trackback:ping><description><![CDATA[<p>【问题描述】</p> <p>一棵二叉树可以按照如下规则表示成一个由0、1、2 组成的字符序列，我们称之为“二叉树序列S”：</p> <p>2表示该树有两个子节点， 和分别表示其两个子树的二叉树序列</p> <p>1表示该树有一个子节点， 为其子树的二叉树序列</p> <p>0表示该树没有子节点</p> <p>你的任务是要对一棵二叉树的节点进行染色。每个节点可以被染成红色、绿色或蓝色。并且，一个节点与其子节点的颜色必须不同，如果该节点有两个子节点，那么这两个子节点的颜色也必须不相同。给定一棵二叉树的二叉树序列，请求出这棵树中最多和最少有多少个点能够被染成绿色。</p> <p>【输入文件】</p> <p>输入文件名：TRO.IN</p> <p>输入文件仅有一行，不超过10000 个字符，表示一个二叉树序列。</p> <p>【输出文件】</p> <p>输出文件名：TRO.OUT</p> <p>输出文件也只有一行，包含两个数，依次表示最多和最少有多少个点能够被</p> <p>染成绿色。</p> <p>【样例输入】</p> <p>1122002010</p> <p>【样例输出】</p> <p>5 2</p> <p>【分析】</p> <h1>1.动归分析</h1> <p>拿最大来说。</p> <p>每个节点的状态只有三种颜色，我们用f[i][0],f[i][1]分别代表第i个点染绿色和不然绿色的最多的点数。因为如果一个点不染绿色，那么他染另两种颜色是等价的。如此我们就得到了如下的动规方程：</p> <ul> <li>叶子：f[i][0]=1; f[i][1]=0;  <li>一个子节点：f[i][0]=f[子节点][1]; f[i][1]=max(f[子节点][0],f[子节点][1]);  <li>两个子节点：f[i][0]=f[左儿子][1]+f[右儿子][1]; f[i][1]=max(f[左儿子][1]+f[右儿子][0],f[左儿子][0]+f[右儿子][1]);</li></ul> <p>最后输出就是max(f[0][1],f[0][0])。</p> <p>最小的和最大的相同。</p> <h1>2.树的确定</h1> <p>因为是一棵二叉树的前序遍历，那么对于一个有子节点的点来说，左儿子就是i+1。我们引进一个link[i]，表示以i为根的子树最后一个点的标号，那么r[i]=link[l[i]]+1。</p> <p>对于link[l]，我们如此确定：</p> <ul> <li>叶子：link[l]=l;  <li>一个子节点：link[l]=link[l+1];  <li>两个子节点：link[l]=link[link[l+1]+1];</li></ul> <p>然后就是实现了。因为自己弄得还是不是很熟，打了两节课。</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #include &lt;<span style="color: #0000ff">string</span>.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: #include &lt;iostream&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: #define maxn 10010
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: #define MAXINT 10000000
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: <span style="color: #0000ff">char</span> s[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: <span style="color: #0000ff">int</span> f[maxn][2];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: <span style="color: #0000ff">int</span> link[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: <span style="color: #0000ff">int</span> n;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12: <span style="color: #0000ff">int</span> l[maxn],r[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14: <span style="color: #0000ff">int</span> _find(<span style="color: #0000ff">int</span> x)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:     <span style="color: #0000ff">if</span> (link[x]) <span style="color: #0000ff">return</span> link[x];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:     <span style="color: #0000ff">if</span> (s[x]=='0') link[x]=x;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18:     <span style="color: #0000ff">else</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:         <span style="color: #0000ff">if</span> (s[x]=='1') link[x]=_find(x+1);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:         <span style="color: #0000ff">else</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:             link[x]=_find(_find(x+1)+1);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:     <span style="color: #0000ff">return</span> link[x];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25: <span style="color: #0000ff">void</span> find1(<span style="color: #0000ff">int</span> x)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27:     <span style="color: #0000ff">if</span> (f[x][0]) <span style="color: #0000ff">return</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28:     <span style="color: #0000ff">if</span> (s[x]=='0')
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:         f[x][0]=1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:         f[x][1]=0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33:     <span style="color: #0000ff">else</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34:         <span style="color: #0000ff">if</span> (s[x]=='1')
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 35:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 36:             find1(l[x]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 37:             f[x][0]=f[l[x]][1]+1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 38:             f[x][1]=max(f[l[x]][1],f[l[x]][0]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 39:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 40:         <span style="color: #0000ff">else</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 41:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 42:             find1(l[x]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 43:             find1(r[x]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 44:             f[x][0]=f[l[x]][1]+f[r[x]][1]+1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 45:             f[x][1]=max(f[l[x]][1]+f[r[x]][0],f[l[x]][0]+f[r[x]][1]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 46:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 47: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 48: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 49: <span style="color: #0000ff">void</span> find2(<span style="color: #0000ff">int</span> x)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 50: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 51:     <span style="color: #0000ff">if</span> (f[x][0]&lt;MAXINT) <span style="color: #0000ff">return</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 52:     <span style="color: #0000ff">if</span> (s[x]=='0')
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 53:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 54:         f[x][0]=1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 55:         f[x][1]=0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 56:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 57:     <span style="color: #0000ff">else</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 58:         <span style="color: #0000ff">if</span> (s[x]=='1')
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 59:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 60:             find2(l[x]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 61:             f[x][0]=f[l[x]][1]+1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 62:             f[x][1]=min(f[l[x]][1],f[l[x]][0]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 63:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 64:         <span style="color: #0000ff">else</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 65:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 66:             find2(l[x]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 67:             find2(r[x]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 68:             f[x][0]=f[l[x]][1]+f[r[x]][1]+1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 69:             f[x][1]=min(f[l[x]][1]+f[r[x]][0],f[l[x]][0]+f[r[x]][1]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 70:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 71: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 72: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 73: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 74: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 75:     freopen("<span style="color: #8b0000">tro.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 76:     freopen("<span style="color: #8b0000">tro.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 77:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 78:     scanf("<span style="color: #8b0000">%s</span>",s);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 79:     n=strlen(s);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 80:     _find(0);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 81:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 82:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 83:         l[i]=i+1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 84:         r[i]=link[l[i]]+1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 85:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 86:     find1(0);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 87:     printf("<span style="color: #8b0000">%d </span>",max(f[0][0],f[0][1]));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 88:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 89:         f[i][0]=f[i][1]=MAXINT;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 90:     find2(0);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 91:     printf("<span style="color: #8b0000">%d\n</span>",min(f[0][0],f[0][1]));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 92:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 93: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 94: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125558.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-01 21:41 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/01/125558.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>区间类动态规划-数字游戏</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/01/125557.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Wed, 01 Sep 2010 13:40:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/01/125557.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125557.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/01/125557.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125557.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125557.html</trackback:ping><description><![CDATA[<p>题目网上都可以找到。</p> <p>注意初始化s[i][j]的时候要加上100000而不是10！！！我傻叉子了= =</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #define MAXINT 10000000
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: #define maxn 200
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">int</span> f[maxn][maxn][maxn][2];<span style="color: #008000">//0 max|||| 1 min</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: <span style="color: #0000ff">int</span> s[maxn][maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">int</span> a[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: <span style="color: #0000ff">int</span> n,m;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: <span style="color: #0000ff">int</span> maxans,minans=MAXINT;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: <span style="color: #0000ff">void</span> find(<span style="color: #0000ff">int</span> x,<span style="color: #0000ff">int</span> y,<span style="color: #0000ff">int</span> t)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13:     <span style="color: #0000ff">if</span> (f[x][y][t][0]) <span style="color: #0000ff">return</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14:     <span style="color: #0000ff">if</span> (t==1)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:         f[x][y][1][0]=f[x][y][1][1]=s[x][y];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:         <span style="color: #0000ff">return</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> k=x+t-1-1;k&lt;y;++k)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:         find(x,k,t-1);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:         <span style="color: #0000ff">if</span> (f[x][k][t-1][1]*s[k+1][y]&lt;f[x][y][t][1]) f[x][y][t][1]=f[x][k][t-1][1]*s[k+1][y];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:         <span style="color: #0000ff">if</span> (f[x][k][t-1][0]*s[k+1][y]&gt;f[x][y][t][0]) f[x][y][t][0]=f[x][k][t-1][0]*s[k+1][y];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:     freopen("<span style="color: #8b0000">game.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:     freopen("<span style="color: #8b0000">game.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:     scanf("<span style="color: #8b0000">%d%d</span>",&amp;n,&amp;m);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 35:         scanf("<span style="color: #8b0000">%d</span>",&amp;a[i]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 36:         a[i+n]=a[i];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 37:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 38:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=2*n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 39:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=i;j&lt;=2*n;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 40:             <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> k=1;k&lt;=m;++k)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 41:                 f[i][j][k][1]=MAXINT;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 42:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=2*n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 43:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=i;j&lt;=2*n;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 44:             s[i][j]=(s[i][j-1]+a[j]+100000)%10;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 45:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i) find(i,i+n-1,m);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 46:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 47:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 48:         <span style="color: #0000ff">if</span> (f[i][i+n-1][m][0]&gt;maxans) maxans=f[i][i+n-1][m][0];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 49:         <span style="color: #0000ff">if</span> (f[i][i+n-1][m][1]&lt;minans) minans=f[i][i+n-1][m][1];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 50:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 51:     printf("<span style="color: #8b0000">%d\n%d\n</span>",minans,maxans);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 52:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 53: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 54: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125557.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-01 21:40 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/01/125557.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数学问题-Black and White</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/01/125476.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Tue, 31 Aug 2010 23:00:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/01/125476.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125476.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/01/125476.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125476.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125476.html</trackback:ping><description><![CDATA[<p>【题目描述】</p> <p>寻找一个由n个整数组成的数列，其中任意连续p个整数之和为正，任意连续q个整数之和为负。若不存在这样的整数数列，则输出NO，否则输出其中一个数列。</p> <p>【输入】</p> <p>对于每个测试点将给你M组数据，要求你对于每组数据，判断是否存在这样的整数数列。</p> <p>输入的第一行是一个正整数M，（1&lt;=N&lt;=10000）,接下来的M行对应M组数据，每行有三个正整数N、P、Q（1&lt;=n,p,q&lt;=10^8）。</p> <p>【输出】</p> <p>输出数据共N行，每行为yes或者no，如果第I组数据有解，则在第I行输出yes，否则输出no</p> <p>【输入输出示例】</p> <table cellspacing="0" cellpadding="2" width="400" border="0"> <tbody> <tr> <td valign="top" width="200">输入(sequence.in) </td> <td valign="top" width="200">输出(sequence.out)</td></tr> <tr> <td valign="top" width="200">2<br>1 1 9<br>10 2 4</td> <td valign="top" width="200">yes<br>no</td></tr></tbody></table> <p>【评分标准】</p> <p>对于每个测试点，如果你能够在1S内通过每组数据，你将得到这个测试点的分数，否则，这个测试点你只能得0分。</p> <p>【分析】</p> <p>原题目是要求输出一种可能的解，如果没有解就输出-1。这样的话就要用到差分约束。</p> <p>现在的话，只需要一个公式。如果有解，应满足：n&lt;=q+p-gcd(p,q)-1。</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #include &lt;iostream&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">int</span> n,m,p,q;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">int</span> gcd(<span style="color: #0000ff">int</span> a,<span style="color: #0000ff">int</span> b)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9:     <span style="color: #0000ff">if</span> (a&lt;b) swap(a,b);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10:     <span style="color: #0000ff">int</span> t;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11:     <span style="color: #0000ff">while</span> (b!=0)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13:         t=a;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14:         a=b;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15:         b=t%a;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:     <span style="color: #0000ff">return</span> a;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:     freopen("<span style="color: #8b0000">sequence.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:     freopen("<span style="color: #8b0000">sequence.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25:     scanf("<span style="color: #8b0000">%d</span>",&amp;m);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;m;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28:         scanf("<span style="color: #8b0000">%d%d%d</span>",&amp;n,&amp;p,&amp;q);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:         <span style="color: #0000ff">if</span> (n&lt;=p+q+gcd(p,q)-1) printf("<span style="color: #8b0000">YES\n</span>");
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:         <span style="color: #0000ff">else</span> printf("<span style="color: #8b0000">NO\n</span>");
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34: </pre></pre>
<p>下面是我写的查分约束。</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #define MAXINT 1000000
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: #define maxn 1010
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">struct</span> ss
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7:     <span style="color: #0000ff">int</span> x,y,dis;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: } l[10000];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: <span style="color: #0000ff">int</span> s[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: <span style="color: #0000ff">int</span> a[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12: <span style="color: #0000ff">int</span> d[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13: <span style="color: #0000ff">int</span> n,q,p,tot;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:     scanf("<span style="color: #8b0000">%d%d%d</span>",&amp;n,&amp;p,&amp;q);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:         <span style="color: #0000ff">if</span> (i+p&gt;n) <span style="color: #0000ff">break</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:         <span style="color: #0000ff">else</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:             l[++tot].x=i+p;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:             l[tot].y=i;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24:             l[tot].dis=-1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27:         <span style="color: #0000ff">if</span> (i+q&gt;n) <span style="color: #0000ff">break</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28:         <span style="color: #0000ff">else</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:             l[++tot].x=i;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:             l[tot].y=i+q;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:             l[tot].dis=-1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 35:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 36:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=tot;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 37:             <span style="color: #0000ff">if</span> (d[l[j].y]&gt;d[l[j].x]+l[j].dis)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 38:                 d[l[j].y]=d[l[j].x]+l[j].dis;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 39:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=tot;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 40:             <span style="color: #0000ff">if</span> (d[l[j].y]&gt;d[l[j].x]+l[j].dis)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 41:             {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 42:                 printf("<span style="color: #8b0000">-1\n</span>");
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 43:                 <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 44:             }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 45:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 46:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 47:         s[i]=d[i];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 48:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i) printf("<span style="color: #8b0000">%d\n</span>",s[i]-s[i-1]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 49:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 50: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 51: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125476.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-01 07:00 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/01/125476.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>&amp;ldquo;长沙一中学习&amp;rdquo;-day1</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/31/125444.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Tue, 31 Aug 2010 12:09:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/31/125444.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125444.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/31/125444.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125444.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125444.html</trackback:ping><description><![CDATA[<p>今天上午从东区搬东西到西区。11点都收拾完了，然后到水房泼了一个小时的水。</p> <p>下午两点多的时候曹老师开始讲课。</p> <p>今天的课程是两个内容：全面分析试题，动态规划。</p> <p>曹老师拿他给自己的学生布置的任务做例子，大概的说了一下从一个题目的模型到完整的题目的过程。首先曹老师给了4道题目，都只是大概的描述。然后将每个条件定严谨。确定输入输出的格式。分析可以用什么算法，每种算法的时间复杂度以及可以通过的数据范围。根据算法定出数据，写出标程。曹老师说他们的学生每个人通过自己的分析，做出10个数据，然后大概100多个测试点来测试每个人写的程序。</p> <p>以下是4道题目。第二题有些瓶颈，一会再发。</p> <ol> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125439.html">动态规划-走迷宫问题</a></li> <li>空缺</li> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125440.html">贪心-买彩票</a></li> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125441.html">数学问题-Black and White</a></li></ol><img src ="http://www.cppblog.com/sephiroth/aggbug/125444.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-31 20:09 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125444.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数学问题-Black and White</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/31/125441.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Tue, 31 Aug 2010 11:59:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/31/125441.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125441.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/31/125441.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125441.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125441.html</trackback:ping><description><![CDATA[<p>【题目描述】</p> <p>寻找一个由n个整数组成的数列，其中任意连续p个整数之和为正，任意连续q个整数之和为负。若不存在这样的整数数列，则输出NO，否则输出其中一个数列。</p> <p>【输入】</p> <p>对于每个测试点将给你M组数据，要求你对于每组数据，判断是否存在这样的整数数列。</p> <p>输入的第一行是一个正整数M，（1&lt;=N&lt;=10000）,接下来的M行对应M组数据，每行有三个正整数N、P、Q（1&lt;=n,p,q&lt;=10^8）。</p> <p>【输出】</p> <p>输出数据共N行，每行为yes或者no，如果第I组数据有解，则在第I行输出yes，否则输出no</p> <p>【输入输出示例】</p> <table cellspacing="0" cellpadding="2" width="400" border="0"> <tbody> <tr> <td valign="top" width="200">输入(sequence.in) </td> <td valign="top" width="200">输出(sequence.out)</td></tr> <tr> <td valign="top" width="200">2<br>1 1 9<br>10 2 4</td> <td valign="top" width="200">yes<br>no</td></tr></tbody></table> <p>【评分标准】</p> <p>对于每个测试点，如果你能够在1S内通过每组数据，你将得到这个测试点的分数，否则，这个测试点你只能得0分。</p> <p>【分析】</p> <p>原题目是要求输出一种可能的解，如果没有解就输出-1。这样的话就要用到差分约束。</p> <p>现在的话，只需要一个公式。如果有解，应满足：n&lt;=q+p+gcd(p,q)-1。</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #include &lt;iostream&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">int</span> n,m,p,q;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">int</span> gcd(<span style="color: #0000ff">int</span> a,<span style="color: #0000ff">int</span> b)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9:     <span style="color: #0000ff">if</span> (a&lt;b) swap(a,b);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10:     <span style="color: #0000ff">int</span> t;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11:     <span style="color: #0000ff">while</span> (b!=0)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13:         t=a;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14:         a=b;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15:         b=t%a;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:     <span style="color: #0000ff">return</span> a;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:     freopen("<span style="color: #8b0000">sequence.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:     freopen("<span style="color: #8b0000">sequence.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25:     scanf("<span style="color: #8b0000">%d</span>",&amp;m);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;m;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28:         scanf("<span style="color: #8b0000">%d%d%d</span>",&amp;n,&amp;p,&amp;q);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:         <span style="color: #0000ff">if</span> (n&lt;=p+q+gcd(p,q)-1) printf("<span style="color: #8b0000">YES\n</span>");
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:         <span style="color: #0000ff">else</span> printf("<span style="color: #8b0000">NO\n</span>");
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125441.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-31 19:59 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125441.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>贪心-买彩票</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/31/125440.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Tue, 31 Aug 2010 11:55:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/31/125440.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125440.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/31/125440.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125440.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125440.html</trackback:ping><description><![CDATA[<p>【问题描述】</p> <p>电视里面正放着“抽百万大奖，赢幸福生活”的宣传广告，bird看后也想去试试手气，当然，作为经济学院的高材生，他可不屑只是单纯的去碰运气。经过他的一番分析，发现，商家在彩票里面做了手脚，使得每个抽奖点的中奖概率不是完全一样的，而且随着时间的变化而变化，不过这种变化是有规律的。对于第I个抽奖点，最开始的中奖概率是百万分之Pi，以后每抽一张彩票后都要重新排队，花费的时间是T分钟，每抽一次减少的概率为Di。</p> <p>由于可怜的bird还有一大堆的作业没做，他只能抽出H个小时去买彩票。由于抽奖地点都在一路公共汽车的线路上，所以怕麻烦的bird决定按车站顺序抽奖，当然，bird可以从任意一站开始抽奖，对于经过的抽奖点可以买彩票，也可以不买。假设从第I个抽奖点到第I+1个抽奖点需要做Ci分钟的汽车。</p> <p>Bird希望能在有限的H个小时内获得最好的运气——即抽奖的概率和最大。</p> <p>[输入] 输入文件名：(tickt.in)</p> <p>第一行为一个整数n，表示抽奖点的个数，1&lt;=n&lt;=200</p> <p>第二行是两个整数H和T，1&lt;=H&lt;=10，1&lt;=T&lt;=60。</p> <p>接下来的n行，每行3个整数，分别是Pi，Di，Ci（Cn=0）。1&lt;=Pi&lt;=10000，Di&lt;=Pi，1&lt;=Ci&lt;=600。</p> <p>[输出] 输出文件名：(tickt.out)</p> <p>文件仅有一行，为一个整数，即抽奖概率和的最大值。</p> <p>【输入输出样例】</p> <table cellspacing="0" cellpadding="2" width="400" border="0"> <tbody> <tr> <td valign="top" width="200">tickt.in </td> <td valign="top" width="200">tickt.out</td></tr> <tr> <td valign="top" width="200"> <p>2</p> <p>1 20</p> <p>200 100 10</p> <p>300 200 0</p></td> <td valign="top" width="200">500</td></tr></tbody></table> <p>【样例说明】</p> <p>首先，bird从1号开始抽奖，花费20分钟，得到概率200，然后坐车到2号，花费10分钟，再花20分钟得到概率300，概率和是500，花费50分钟。</p> <p>【评分标准】</p> <p>对于每个测试点，如果你能够在规定的时间内通过每组数据，你将得到这个测试点的分数，否则，这个测试点你只能得0分。</p> <p>【分析】</p> <p>由CEOI的钓鱼改编，具体可以看《算法艺术与信息学竞赛》P13。</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #include &lt;iostream&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: #define maxn 210
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: <span style="color: #0000ff">int</span> b[maxn][maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">int</span> p[maxn],d[maxn],c[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: <span style="color: #0000ff">int</span> h,t,tot;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: <span style="color: #0000ff">struct</span> ss
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11:     <span style="color: #0000ff">int</span> pi,di;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12: } hp[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13: <span style="color: #0000ff">int</span> remain,ans,teans,n;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15: <span style="color: #0000ff">void</span> down(<span style="color: #0000ff">int</span> x)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:     <span style="color: #0000ff">int</span> te=2*x;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18:     <span style="color: #0000ff">while</span> (te&lt;=tot)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:         <span style="color: #0000ff">if</span> ((te+1&lt;=tot)&amp;&amp;(hp[te].pi&lt;hp[te+1].pi)) ++te;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:         <span style="color: #0000ff">if</span> (hp[x].pi&gt;hp[te].pi) <span style="color: #0000ff">break</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:         swap(hp[x],hp[te]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:         x=te;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24:         te=x*2;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:     freopen("<span style="color: #8b0000">ticket.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:     freopen("<span style="color: #8b0000">ticket.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33:     scanf("<span style="color: #8b0000">%d%d%d</span>",&amp;n,&amp;h,&amp;t);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34:     h*=60;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 35:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i) scanf("<span style="color: #8b0000">%d%d%d</span>",&amp;p[i],&amp;d[i],&amp;c[i]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 36:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 37:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=i+1;j&lt;=n;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 38:             b[i][j]=b[i][j-1]+c[j-1];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 39:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 40:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=n;j&gt;=i;--j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 41:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 42:             teans=0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 43:             remain=h-b[i][j];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 44:             memset(hp,0,<span style="color: #0000ff">sizeof</span>(hp));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 45:             <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> k=1;k&lt;=j-i+1;++k)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 46:             {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 47:                 hp[k].pi=p[i+k-1];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 48:                 hp[k].di=d[i+k-1];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 49:             }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 50:             tot=j-i+1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 51:             <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> k=j-i+1;k&gt;=1;--k) down(k);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 52:             <span style="color: #0000ff">while</span> ((remain&gt;=t)&amp;&amp;(hp[1].pi&gt;0))
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 53:             {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 54:                 teans+=hp[1].pi;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 55:                 hp[1].pi-=hp[1].di;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 56:                 remain-=t;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 57:                 down(1);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 58:             }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 59:             <span style="color: #0000ff">if</span> (teans&gt;ans) ans=teans;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 60:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 61:     printf("<span style="color: #8b0000">%d\n</span>",ans);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 62:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 63: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 64: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125440.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-31 19:55 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125440.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态规划-走迷宫问题</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/31/125439.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Tue, 31 Aug 2010 11:52:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/31/125439.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125439.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/31/125439.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125439.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125439.html</trackback:ping><description><![CDATA[<p></p> <p>[题目描述]</p> <p>有一个n*n的迷宫，每个方格里都有着相应的数字。你从左上角出发，每次可以向上下左右四个方向最多移动k格，并且要求你每次到达的方格里的数字必须大于上一次所在方格的数字。现在要求你走过的方格的所有数之和最大，问这个最大和是多少。</p> <p>[输入]</p> <p>输入数据第一行为两个正整数N、K（1&lt;=N&lt;=100,0&lt;=K&lt;=N）</p> <p>接下来的n行，每行有n个不超过integer范围的整数，表示地图中的数。</p> <p>[输出]</p> <p>输出数据只有一行，为最大的和。</p> <p>[输入输出示例]</p> <p>输入(maze.in) 输出(maze.out)</p> <p>3 1 25</p> <p>3 6 2</p> <p>4 7 9</p> <p>2 3 1</p> <p>[评分标准]</p> <p>对于每个测试数据，如果你能够得出正确的答案，那么你将得到满分，否则得0分。</p> <p>[分析]</p> <p>很明显的动态规划，应该是从《滑雪》那道题改编而来的。</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #define maxn 110
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: <span style="color: #0000ff">int</span> a[maxn][maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">int</span> f[maxn][maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: <span style="color: #0000ff">int</span> n,ans,k;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">int</span> xx[4]={0,0,1,-1};
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: <span style="color: #0000ff">int</span> yy[4]={1,-1,0,0};
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: <span style="color: #0000ff">int</span> find(<span style="color: #0000ff">int</span> x,<span style="color: #0000ff">int</span> y)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12:     <span style="color: #0000ff">if</span> (f[x][y]) <span style="color: #0000ff">return</span> f[x][y];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13:     <span style="color: #0000ff">int</span> temx,temy;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;4;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=k;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:             temx=x+xx[i]*j;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18:             temy=y+yy[i]*j;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:             <span style="color: #0000ff">if</span> ((temx&gt;0)&amp;&amp;(temx&lt;=n)&amp;&amp;(temy&gt;0)&amp;&amp;(temy&lt;=n))
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:                 <span style="color: #0000ff">if</span> ((a[temx][temy]&gt;a[x][y])&amp;&amp;(find(temx,temy)&gt;f[x][y]))
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:                     f[x][y]=find(temx,temy);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:     f[x][y]+=a[x][y];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24:     <span style="color: #0000ff">return</span> f[x][y];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:     freopen("<span style="color: #8b0000">maze.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:     freopen("<span style="color: #8b0000">maze.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:     scanf("<span style="color: #8b0000">%d%d</span>",&amp;n,&amp;k);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=n;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 35:             scanf("<span style="color: #8b0000">%d</span>",&amp;a[i][j]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 36:     printf("<span style="color: #8b0000">%d\n</span>",find(1,1));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 37:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 38: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 39: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125439.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-31 19:52 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125439.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>即将开始的5天集训</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/31/125356.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Tue, 31 Aug 2010 01:57:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/31/125356.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125356.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/31/125356.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125356.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125356.html</trackback:ping><description><![CDATA[<p>请了一个长沙第一中学的老师，很出名。大概带出来了几百个省一吧。</p> <p>从今天开始进入为期5天的“长沙一中学习”专题活动~</p> <p>也许这几天用不了writer了，因为安装的时候ms要重新启动。西区的电脑有还原卡的。</p> <p>To be continued.</p><img src ="http://www.cppblog.com/sephiroth/aggbug/125356.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-31 09:57 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125356.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态规划-最小与最大</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/31/125337.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Mon, 30 Aug 2010 23:46:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/31/125337.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125337.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/31/125337.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125337.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125337.html</trackback:ping><description><![CDATA[<p> <p>【问题描述】</p> <p>做过了乘积最大这道题，相信这道题也难不倒你。</p> <p>已知一个数串，可以在适当的位置加入乘号（设加了k个，当然也可不加，即分成k+1个部分），设这k+1个部分的乘积（如果k=0,则乘积即为原数串的值）对m 的余数（即mod m）为x;</p> <p>现求x能达到的最小值及该情况下k的最小值，以及x能达到的最大值及该情况下的k的最小值（可以存在x的最小值与最大值相同的情况）。</p> <p>【输入】</p> <p>第一行为数串，长度为n 满足2&lt;=n&lt;=1000，且数串中不存在0；</p> <p>第二行为m，满足2&lt;=m&lt;=50。</p> <p>【输出】</p> <p>四个数，分别为x的最小值 和 该情况下的k，以及x的最大值和 该情况下的k，中间用空格隔开。</p> <p>【样例输入】</p> <p>4421</p> <p>22</p> <p>【样例输出】</p> <p>0 1 21 0</p> <hr> </hr> <p>既然题目要求的都是乘号的最小值，那么我们自然地就会想到动归数组中记录乘号的最小值。f[i][j]表示0~i的串达到j这个余数所需最少的乘号数。预处理b[i][j]表示从i到j的数对m的余数。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: #include &lt;stdio.h&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: #include &lt;<span style="color: #0000ff">string</span>.h&gt;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: #define MAXINT 100000
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: #define maxn 1010
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: <span style="color: #0000ff">int</span> f[maxn][50];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: <span style="color: #0000ff">int</span> b[maxn][maxn];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: <span style="color: #0000ff">int</span> n,m;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9: <span style="color: #0000ff">char</span> s[maxn];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: <span style="color: #0000ff">int</span> tem;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: <span style="color: #0000ff">int</span> main()
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14:     freopen("<span style="color: #8b0000">input.txt</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15:     freopen("<span style="color: #8b0000">output.txt</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:     
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17:     scanf("<span style="color: #8b0000">%s%d</span>",s,&amp;m);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18:     n=strlen(s);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;n;++i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=0;j&lt;m;++j)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21:             f[i][j]=MAXINT;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;n;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23:     {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24:         tem=(tem*10+s[i]-'0')%m;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25:         f[i][tem]=0;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26:     }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;n;++i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28:     {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29:         tem=0;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=i;j&lt;n;++j)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31:         {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32:             tem=(tem*10+s[j]-'0')%m;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33:             b[i][j]=tem;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34:         }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35:     }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;n;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=0;j&lt;m;++j)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38:             <span style="color: #0000ff">if</span> (f[i][j]&lt;MAXINT)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39:                 <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> k=i+1;k&lt;n;++k)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 40:                 {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 41:                     tem=(j*b[i+1][k])%m;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 42:                     <span style="color: #0000ff">if</span> (f[i][j]+1&lt;f[k][tem]) f[k][tem]=f[i][j]+1;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 43:                 }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 44:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;m;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 45:         <span style="color: #0000ff">if</span> (f[n-1][i]&lt;MAXINT)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 46:         {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 47:             printf("<span style="color: #8b0000">%d %d </span>",i,f[n-1][i]);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 48:             <span style="color: #0000ff">break</span>;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 49:         }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 50:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=m-1;i&gt;=0;--i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 51:         <span style="color: #0000ff">if</span> (f[n-1][i]&lt;MAXINT)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 52:         {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 53:             printf("<span style="color: #8b0000">%d %d\n</span>",i,f[n-1][i]);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 54:             <span style="color: #0000ff">break</span>;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 55:         }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 56:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 57: }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 58: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125337.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-31 07:46 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125337.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态规划-最小总代价</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/30/125317.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Mon, 30 Aug 2010 13:59:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/30/125317.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125317.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/30/125317.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125317.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125317.html</trackback:ping><description><![CDATA[<p> <p>【问题描述】</p> <p>n个人在做传递物品的游戏,编号为1-n。</p> <p>游戏规则是这样的：开始时物品可以在任意一人手上，他可把物品传递给其他人中的任意一位；下一个人可以传递给未接过物品的任意一人。</p> <p>即物品只能经过同一个人一次，而且每次传递过程都有一个代价；不同的人传给不同的人的代价值之间没有联系；</p> <p>求当物品经过所有n个人后，整个过程的总代价是多少。</p> <p>【输入】</p> <p>第一行为n,表示共有n个人（16&gt;=n&gt;=2）；</p> <p>以下为n*n的矩阵，第i+1行、第j列表示物品从编号为i的人传递到编号为j的人所花费的代价，特别的有第i+1行、第i列为-1（因为物品不能自己传给自己），其他数据均为正整数(&lt;=10000)。</p> <p>(对于50%的数据，n&lt;=11)。</p> <p>【输出】</p> <p>一个数，为最小的代价总和。</p> <p>【样例输入】</p> <p>2</p> <p>-1 9794</p> <p>2724 –1</p> <p>【样例输出】</p> <p>2724</p> <hr> </hr> <p><strike>时间到了啊~ 明天再继续写。</strike></p> <p>是一个用二进制来表示图的状态的动态规划，或者说是记忆化搜索。f[i][j]中，i来表示图的二进制状态，j表示这个状态中第一个点。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: #include &lt;stdio.h&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: #include &lt;iostream&gt;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: #define maxn 20
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: #define MAXINT 1000000
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: 
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: <span style="color: #0000ff">int</span> dis[maxn][maxn];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: <span style="color: #0000ff">int</span> n;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9: <span style="color: #0000ff">int</span> f[70000][20];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: <span style="color: #0000ff">int</span> ans=MAXINT;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: <span style="color: #0000ff">int</span> find(<span style="color: #0000ff">int</span> a,<span style="color: #0000ff">int</span> b)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14:     <span style="color: #0000ff">if</span> (f[a][b]!=MAXINT) <span style="color: #0000ff">return</span> f[a][b];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15:     <span style="color: #0000ff">int</span> tem=a;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:     a-=(1&lt;&lt;b);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17:     <span style="color: #0000ff">if</span> (!a)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18:     {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19:         f[tem][b]=0;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20:         <span style="color: #0000ff">return</span> 0;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21:     }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;n;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23:         <span style="color: #0000ff">if</span> ((1&lt;&lt;i)&amp;a)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24:             f[tem][b]=min(find(a,i)+dis[b][i],f[tem][b]);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25:     <span style="color: #0000ff">return</span> f[tem][b];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26: }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28: <span style="color: #0000ff">int</span> main()
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29: {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30:     freopen("<span style="color: #8b0000">input.txt</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31:     freopen("<span style="color: #8b0000">output.txt</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32:     
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33:     scanf("<span style="color: #8b0000">%d</span>",&amp;n);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;n;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=0;j&lt;n;++j)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36:             scanf("<span style="color: #8b0000">%d</span>",&amp;dis[i][j]);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;(1&lt;&lt;n);++i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=0;j&lt;n;++j)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39:             f[i][j]=MAXINT;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 40:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;n;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 41:         <span style="color: #0000ff">if</span> (find((1&lt;&lt;n)-1,i)&lt;ans)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 42:             ans=find((1&lt;&lt;n)-1,i);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 43:     printf("<span style="color: #8b0000">%d\n</span>",ans);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 44:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 45: }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 46: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125317.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-30 21:59 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/30/125317.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符串处理-牛的人品</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/30/125316.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Mon, 30 Aug 2010 13:55:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/30/125316.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125316.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/30/125316.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125316.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125316.html</trackback:ping><description><![CDATA[<p><b></b> <p>【问题描述】</p> <p>天苍苍，野茫茫，JSZX的菜鸟们来到OI牧场旅游，看到了好多好多的牛。OI牧场所有的牛都觉得自己的Rp最高（简称RP牛），为此他们常争论不休。于是，他们让JSZX的菜菜们用最最朴素的方法找出这只RP牛。</p> <p>经过讨论，最菜的mmk想出了最朴素的方法：</p> <p>我们要以cows的名字为线索，来找出RP牛。</p> <p>首先，得到n头牛的名字清单（每头牛的名字是一个仅包含小写字母的字符串，且这些牛的读写方式比较特殊—从右到左），然后对每头牛进行检验，检验按照牛的读写方式进行。规则如下：</p> <p>1．Rp 牛的名字中必须有子串“jszxoier”</p> <p>2．将名字中的每个“cow”的替换为“bird”。</p> <p>3．计算Rp值：A为名字中子串“r”的个数；</p> <p>B为名字中子串“p”的个数；</p> <p>C为名字中字串“rp”的个数；</p> <p>Rp值即为5×A+5×B+20×C。</p> <p>最后输出RP牛的名字，若有多个RP牛，则输出名字最短的那个。</p> <p>假如你也是牛中一员，尽管你很不屑这样的水题，但是，你很想到RP牛那里分点Rp，所以你决定解决这道题，并算出RP牛的Rp是多少。</p> <p>【输入】</p> <p>第一行，一个数n（n&lt;=3000）。</p> <p>接下来的n行，每行一个字符串，长度&lt;=300，数据保证存在RP牛。</p> <p>【输出】</p> <p>共两行</p> <p>第一行为RP牛的名字</p> <p>第二行为RP牛的Rp值</p> <p>【样例输入】</p> <p>8</p> <p>reioxzsjzmy</p> <p>mmk</p> <p>jwc</p> <p>zxf</p> <p>jwc</p> <p>wangwei</p> <p>xcy</p> <p>yuhc</p> <p>【样例输出】</p> <p>reioxzsjzmy</p> <p>5</p> <hr> </hr> <p>我用KMP匹配的，代码挺短，还比string.h的好用。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: #include &lt;stdio.h&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: #include &lt;<span style="color: #0000ff">string</span>.h&gt;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: #include &lt;iostream&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: #define maxn 400
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: 
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: <span style="color: #0000ff">int</span> f[maxn];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: <span style="color: #0000ff">char</span> *aa="<span style="color: #8b0000">jszxoier</span>",*bb="<span style="color: #8b0000">cow</span>",*ee="<span style="color: #8b0000">rp</span>",s[maxn];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9: <span style="color: #0000ff">int</span> a,b,c,tem,ans,anslen,len,m;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: <span style="color: #0000ff">int</span> n;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: <span style="color: #0000ff">bool</span> t;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: <span style="color: #0000ff">char</span> anss[400];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14: <span style="color: #0000ff">void</span> initf(<span style="color: #0000ff">char</span> *s)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:     memset(f,0,<span style="color: #0000ff">sizeof</span>(f));
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17:     m=strlen(s);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18:     <span style="color: #0000ff">int</span> k=-1;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19:     f[0]=-1;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;m;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21:     {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22:         <span style="color: #0000ff">while</span> ((k&gt;-1)&amp;&amp;(s[k+1]!=s[i])) k=f[k];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23:         <span style="color: #0000ff">if</span> (s[k+1]==s[i]) ++k;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24:         f[i]=k;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25:     }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26: }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28: <span style="color: #0000ff">void</span> changef(<span style="color: #0000ff">char</span> *s)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29: {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30:     initf(s);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31:     <span style="color: #0000ff">int</span> k;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;m;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33:     {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34:         k=f[i];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35:         <span style="color: #0000ff">while</span> ((k&gt;-1)&amp;&amp;(s[k+1]==s[i+1])) k=f[k];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36:         f[i]=k;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37:     }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38: }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 40: <span style="color: #0000ff">int</span> main()
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 41: {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 42:     freopen("<span style="color: #8b0000">input.txt</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 43:     freopen("<span style="color: #8b0000">output.txt</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 44:     
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 45:     scanf("<span style="color: #8b0000">%d</span>",&amp;n);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 46:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> dt=1;dt&lt;=n;++dt)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 47:     {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 48:         memset(s,0,<span style="color: #0000ff">sizeof</span>(s));
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 49:         scanf("<span style="color: #8b0000">%s</span>",s);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 50:         strrev(s);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 51:         changef(aa);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 52:         len=strlen(s);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 53:         <span style="color: #0000ff">int</span> k=-1;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 54:         t=0;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 55:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;len;++i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 56:         {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 57:             <span style="color: #0000ff">while</span> ((k&gt;-1)&amp;&amp;(aa[k+1]!=s[i])) k=f[k];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 58:             <span style="color: #0000ff">if</span> (aa[k+1]==s[i]) ++k;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 59:             <span style="color: #0000ff">if</span> (k==m-1)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 60:             {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 61:                 t=1;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 62:                 <span style="color: #0000ff">break</span>;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 63:             }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 64:         }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 65:         <span style="color: #0000ff">if</span> (!t) <span style="color: #0000ff">continue</span>;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 66:         a=b=c=0;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 67:         changef(bb);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 68:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;len;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 69:         {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 70:             <span style="color: #0000ff">while</span> ((k&gt;-1)&amp;&amp;(bb[k+1]!=s[i])) k=f[k];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 71:             <span style="color: #0000ff">if</span> (bb[k+1]==s[i]) ++k;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 72:             <span style="color: #0000ff">if</span> (k==m-1) ++a;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 73:         }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 74:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;len;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 75:             <span style="color: #0000ff">if</span> (s[i]=='r') ++a;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 76:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;len;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 77:             <span style="color: #0000ff">if</span> (s[i]=='p') ++b;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 78:         changef(ee);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 79:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0;i&lt;len;++i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 80:         {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 81:             <span style="color: #0000ff">while</span> ((k&gt;-1)&amp;&amp;(ee[k+1]!=s[i])) k=f[k];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 82:             <span style="color: #0000ff">if</span> (ee[k+1]==s[i]) ++k;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 83:             <span style="color: #0000ff">if</span> (k==m-1) ++c;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 84:         }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 85:         tem=a*5+b*5+c*20;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 86:         <span style="color: #0000ff">if</span> (tem&gt;ans)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 87:         {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 88:             memset(anss,0,<span style="color: #0000ff">sizeof</span>(anss));
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 89:             strcat(anss,s);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 90:             ans=tem;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 91:             anslen=len;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 92:         }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 93:         <span style="color: #0000ff">else</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 94:             <span style="color: #0000ff">if</span> (tem==ans)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 95:                 <span style="color: #0000ff">if</span> (len&lt;anslen)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 96:                 {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 97:                     memset(anss,0,<span style="color: #0000ff">sizeof</span>(anss));
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 98:                     strcat(anss,s);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 99:                 }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">100:     }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">101:     strrev(anss);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">102:     printf("<span style="color: #8b0000">%s\n%d\n</span>",anss,ans);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">103:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">104: }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">105: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125316.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-30 21:55 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/30/125316.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数的划分-递推动态规划</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/28/125022.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Sat, 28 Aug 2010 03:04:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/28/125022.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125022.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/28/125022.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125022.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125022.html</trackback:ping><description><![CDATA[<p>又一经典问题，noip2001。</p> <p>用到了分类的思想。对于f[i][j]代表i分为j份。我们分为以下两类：</p> <ol> <li>每份都没有1：那么我们只需要将每份都减1然后保证有j份。即加上f[i-j][j]。</li> <li>至少有一份1：那么我们提出1个1,即加上f[i-1][j-1]。</li></ol><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: #include &lt;stdio.h&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: #define maxn 300
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: <span style="color: #0000ff">int</span> f[maxn][maxn];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: <span style="color: #0000ff">int</span> n,m;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: 
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: <span style="color: #0000ff">int</span> main()
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9:     scanf("<span style="color: #8b0000">%d%d</span>",&amp;n,&amp;m);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10:     f[0][0]=1;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=m;++j)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13:             <span style="color: #0000ff">if</span> (i-j&gt;=0)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14:                 f[i][j]=f[i-j][j]+f[i-1][j-1];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15:     printf("<span style="color: #8b0000">%d\n</span>",f[n][m]);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17: }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125022.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-28 11:04 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/28/125022.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>核电站问题-递推动态规划</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/28/125019.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Sat, 28 Aug 2010 02:29:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/28/125019.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125019.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/28/125019.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125019.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125019.html</trackback:ping><description><![CDATA[<p>很经典的问题，由于递推公式比较诡异，所以特地的记录下来。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: #include &lt;stdio.h&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: #define maxn 100
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: <span style="color: #0000ff">unsigned</span> <span style="color: #0000ff">long</span> <span style="color: #0000ff">long</span> f[maxn];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: <span style="color: #0000ff">int</span> n,m;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: 
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: <span style="color: #0000ff">int</span> main()
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9:     f[0]=1;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10:     scanf("<span style="color: #8b0000">%d%d</span>",&amp;n,&amp;m);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12:     {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13:         f[i]=f[i-1]*2;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14:         <span style="color: #0000ff">if</span> (i-m-1&gt;=0) f[i]-=f[i-m-1];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15:         <span style="color: #0000ff">if</span> (i-m-1==-1) f[i]-=1;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:     }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17:     printf("<span style="color: #8b0000">%I64d\n</span>",f[n]);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19: }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125019.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-28 10:29 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/28/125019.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>题目&amp;amp;知识分类</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/27/124957.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Fri, 27 Aug 2010 10:08:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/27/124957.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/124957.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/27/124957.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/124957.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/124957.html</trackback:ping><description><![CDATA[<h1>1.动态规划</h1> <ol> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/27/124954.html">树形依赖背包——百度NOIP吧编程挑战赛"charge"</a>  <li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/28/125019.html">核电站问题-递推动态规划</a>  <li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/28/125022.html">数的划分-递推动态规划</a>  <li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125439.html">动态规划-走迷宫问题</a> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/09/01/125557.html">区间类动态规划-数字游戏</a> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/09/01/125558.html">树形动态规划-三色二叉树</a></li></ol> <h1>2.贪心</h1> <ol> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125440.html">贪心-买彩票</a></li></ol> <h1>3.数学问题</h1> <ol> <li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/31/125441.html">数学问题-Black and White</a></li></ol><img src ="http://www.cppblog.com/sephiroth/aggbug/124957.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-27 18:08 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/27/124957.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CTSC98-选课</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/27/124956.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Fri, 27 Aug 2010 10:05:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/27/124956.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/124956.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/27/124956.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/124956.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/124956.html</trackback:ping><description><![CDATA[<p>有依赖的背包，正好刚刚学到。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: #include &lt;stdio.h&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: #include &lt;stdlib.h&gt;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: #define maxn 400
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: 
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: <span style="color: #0000ff">int</span> f[maxn][maxn];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: <span style="color: #0000ff">int</span> t[maxn];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: <span style="color: #0000ff">struct</span> ss
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9:     <span style="color: #0000ff">int</span> root,num;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: } a[maxn];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: <span style="color: #0000ff">int</span> n,m;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: <span style="color: #0000ff">int</span> w[maxn];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14: <span style="color: #0000ff">int</span> cmp(<span style="color: #0000ff">const</span> <span style="color: #0000ff">void</span>*a,<span style="color: #0000ff">const</span> <span style="color: #0000ff">void</span>*b)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:     ss c=*(ss*)a,d=*(ss*)b;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17:     <span style="color: #0000ff">if</span> (c.root&lt;d.root) <span style="color: #0000ff">return</span> -1;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18:     <span style="color: #0000ff">if</span> (c.root&gt;d.root) <span style="color: #0000ff">return</span> 1;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20: }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21: 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22: <span style="color: #0000ff">void</span> dp(<span style="color: #0000ff">int</span> x)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23: {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=t[x];i&lt;t[x+1];++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25:     {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26:         <span style="color: #0000ff">int</span> k=a[i].num;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=0;j&lt;=m;++j) f[k][j]=f[x][j]+w[k];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28:         dp(k);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=m;++j)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30:             <span style="color: #0000ff">if</span> (f[k][j-1]&gt;f[x][j])
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31:                 f[x][j]=f[k][j-1];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32:     }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33: }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34: 
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35: <span style="color: #0000ff">int</span> main()
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36: {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37:     scanf("<span style="color: #8b0000">%d%d</span>",&amp;n,&amp;m);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39:     {
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 40:         scanf("<span style="color: #8b0000">%d%d</span>",&amp;a[i].root,&amp;w[i]);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 41:         a[i].num=i;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 42:     }
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 43:     a[0].root=-1;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 44:     qsort(a,n+1,<span style="color: #0000ff">sizeof</span>(ss),cmp);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 45:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 46:         <span style="color: #0000ff">if</span> (!t[a[i].root])
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 47:             t[a[i].root]=i;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 48:     t[n+1]=n+1;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 49:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=n;i&gt;=0;--i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 50:         <span style="color: #0000ff">if</span> (!t[i])
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 51:             t[i]=t[i+1];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 52:     dp(0);
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 53:     printf("<span style="color: #8b0000">%d\n</span>",f[0][m]);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 54:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 55: }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 56: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/124956.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-27 18:05 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/27/124956.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>树形依赖背包&amp;mdash;&amp;mdash;百度NOIP吧编程挑战赛&amp;quot;charge&amp;quot;</title><link>http://www.cppblog.com/sephiroth/archive/2010/08/27/124954.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Fri, 27 Aug 2010 09:25:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/08/27/124954.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/124954.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/08/27/124954.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/124954.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/124954.html</trackback:ping><description><![CDATA[<p>上周noip吧进行的编程挑战赛。</p> <p>相关内容见“<a href="http://tieba.baidu.com/f?kz=869775539">【传送门】百度NOIP吧编程挑战赛相关帖子</a>”</p> <p>第三题是一道有依赖的背包问题。今天dev问到了，于是又回忆了一下。徐持衡大牛的集训队论文中关于泛化物品的并还是不太懂。但是对这种背包有点了自己的感悟。</p> <p>这道题目对我来说有两个启发点：</p> <ol> <li>对多儿子的处理，用的是nlogn的预处理。  <li>树形的背包。</li></ol> <h1>1.多儿子的处理</h1> <p>用结构体记录关系，a[i].root是第i个关系的父节点，a[i].num是第i个关系的子节点。将数组按root排序。然后用t[i]记录i这个点的儿子在a中的起始位置。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">struct</span> ss
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">int</span> root,num;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: } a[maxn];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: <span style="color: #0000ff">int</span> t[maxn];
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: 
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: <span style="color: #0000ff">void</span> init()
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9:     qsort(a,n,<span style="color: #0000ff">sizeof</span>(ss),cmp);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11:         <span style="color: #0000ff">if</span> (!t[a[i].root])
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12:             t[a[i].root]=i;
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13:     t[n+1]=n+1;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=n;i&gt;0;--i)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15:         <span style="color: #0000ff">if</span> (!t[i])
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:             t[i]=t[i+1];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17: }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: </pre></pre>
<p>这样第i个点的子节点在a中的位置就是从t[i]到t[i+1]-1。</p>
<p>免去了链表的繁琐，效率还相当高。</p>
<h1>2.树形的背包</h1>
<p>主要思想就是对于i的节点s，f[s]强制赋值为f[i]，然后强制加入w[s],对s进行处理，再更新f[i]。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">void</span> dp(<span style="color: #0000ff">int</span> x)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=t[x];i&lt;t[x+1];++i)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4:     {
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:         <span style="color: #0000ff">int</span> k=a[i].num;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=0;j&lt;=V;++j) f[k][j]=f[x][j]+w[k];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7:         dp(k);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=v[k];j&lt;=V;++j)
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9:             <span style="color: #0000ff">if</span> (f[k][j-v[k]]&gt;f[x][j])
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10:                 f[x][j]=f[k][j-v[k]];
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11:     }
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: }</pre></pre>相当棒的解决问题。 
<h1>3.一些习题</h1>
<ol>
<li><a href="http://www.cppblog.com/sephiroth/archive/2010/08/27/124956.html">CTSC98-选课</a></li></ol><img src ="http://www.cppblog.com/sephiroth/aggbug/124954.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-08-27 17:25 <a href="http://www.cppblog.com/sephiroth/archive/2010/08/27/124954.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>