﻿<?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++博客-止于自娱-随笔分类-USACO</title><link>http://www.cppblog.com/yuziyu/category/10797.html</link><description>每天进步一点点,Coding Everyday!</description><language>zh-cn</language><lastBuildDate>Fri, 17 Jul 2009 07:55:37 GMT</lastBuildDate><pubDate>Fri, 17 Jul 2009 07:55:37 GMT</pubDate><ttl>60</ttl><item><title>USACO 4.1 Fence Loops</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 17 Jul 2009 06:26:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90336.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90336.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90336.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 求无向图中的一个最小环的长度&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/90336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-17 14:26 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 4.1 Beef McNuggets</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/12/89864.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sun, 12 Jul 2009 06:58:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/12/89864.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89864.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/12/89864.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89864.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89864.html</trackback:ping><description><![CDATA[这题有些难。虽然知道是动态规划题，但是不知道要开多大的数组，后来看analysis用一个256大小的数组循环使用，方法很巧妙。<br />先将box进行排序。<br />如果box里面的数的最大公约数不为1的话，那么所有组成的数，只可能是这个公约数的倍数，因此没有上限，输出为0.<br />用last记录最小的“不能组成的数”。这样当last之后有boxs[0]个连续数都可以组成的话，那么所有的数都可以组成。<br />last+1...last+box[0]可以组成的话，那么每个数都加一个box[0],那么新一轮的box[0]个数也可以组成，以此类推。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">nuggets.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">nuggets.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> box_num;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> boxs[</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> ok[</span><span style="color: rgb(0, 0, 0);">256</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> gcd(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> a,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> b)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(a</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">b) swap(a,b);<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> tmp;<br /><br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(b</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />        tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> a;<br />        a </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> b;<br />        b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">b;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> a;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br /><br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">box_num;<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">box_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">boxs[i];<br /><br />    sort(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">boxs[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">],</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">boxs[box_num]);<br />    <br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> t </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> boxs[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">];<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">box_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        t </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> gcd(t,boxs[i]);<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(t</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">){<br />        </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    memset(ok,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(ok));<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> last </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    ok[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(ok[i</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">256</span><span style="color: rgb(0, 0, 0);">]){<br />            ok[i</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">256</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">last</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">boxs[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]){<br />                </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">last</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />                </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />            }<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;x</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">box_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">x){<br />                ok[(i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">boxs[x])</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">256</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />            }<br />        }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />            last </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i;<br />        }<br />        </span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i;<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><center><b><font size="7">Beef McNuggets</font></b><br /><br /><b>Hubert Chen</b></center><p> Farmer Brown's cows are up in arms, having heard that McDonalds is
considering the introduction of a new product:  Beef McNuggets.  The
cows are trying to find any possible way to put such a product in a
negative light.

</p><p> One strategy the cows are pursuing is that of `inferior packaging'.
``Look,'' say the cows, ``if you have Beef McNuggets in boxes of 3, 6,
and 10, you can not satisfy a customer who wants 1, 2, 4, 5, 7, 8, 11,
14, or 17 McNuggets.  Bad packaging: bad product.''

</p><p> Help the cows. Given N (the number of packaging options, 1 &lt;= N
&lt;= 10), and a set of N positive integers (1 &lt;= i &lt;= 256) that
represent the number of nuggets in the various packages, output the
largest number of nuggets that can not be purchased by buying nuggets
in the given sizes.  Print 0 if all possible purchases can be made or
if there is no bound to the largest number.

</p><p> The largest impossible number (if it exists) will be no larger than
2,000,000,000.

</p><h3>PROGRAM NAME: nuggets</h3><h3>INPUT FORMAT</h3><table border="1"><tbody><tr><td> Line 1: </td><td> N, the number of packaging options</td></tr><tr><td> Line 2..N+1: </td><td>The number of nuggets in one kind of
box </td></tr></tbody></table><h3>SAMPLE INPUT (file nuggets.in) </h3><pre>3<br />3<br />6<br />10<br /></pre><h3>OUTPUT FORMAT</h3>

The output file should contain a single line containing a single integer
that represents the largest number of nuggets that can not be represented
or 0 if all possible purchases can be made or if there is no bound to
the largest number.

<h3>SAMPLE OUTPUT (file nuggets.out)</h3><pre>17<br /></pre><img src ="http://www.cppblog.com/yuziyu/aggbug/89864.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-12 14:58 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/12/89864.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.4 Raucous Rockers</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/11/89781.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sat, 11 Jul 2009 03:48:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/11/89781.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89781.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/11/89781.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89781.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89781.html</trackback:ping><description><![CDATA[
		<br />一开始把题意理解错了，以为刻在同一张光盘上的歌曲的时间顺序不变就可以了。<br />事实上不仅同光盘上的歌曲写入时间要按顺序，前一张光盘上的歌曲不能比后一张歌曲写入时间要晚。<br /><br />数据量比较少，用回溯法，dp也行。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">rockers.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">rockers.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> capacity[</span><span style="color: rgb(0, 0, 0);">20</span><span style="color: rgb(0, 0, 0);">];;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> songs[</span><span style="color: rgb(0, 0, 0);">20</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> song_num,disk_num;</span><span style="color: rgb(0, 0, 255);"></span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> res </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> cur;<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> backtracing(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> depth,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> last)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(depth</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">song_num){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(cur</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">res){<br />            res </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> cur;<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    <br />    //如果后面所有的歌曲都加上还比最优值小，剪枝<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(cur</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">song_num</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">depth</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">res)<br />       </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">; <br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">last;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">disk_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />         //如果当前歌曲需要刻录，那只需刻在第一张能装得下的光盘上。<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( capacity[i]</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">songs[depth]){<br />            cur</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />            capacity[i]</span><span style="color: rgb(0, 0, 0);">-=</span><span style="color: rgb(0, 0, 0);">songs[depth];<br />            backtracing(depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,i);<br />            capacity[i]</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">songs[depth];<br />            cur</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        }<br />    }<br /><br />    // 不刻当前歌曲<br />    backtracing(depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,last);<br /><br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> c;<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">song_num</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">c</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">disk_num;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">song_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">songs[i];<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">disk_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        capacity[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> c;<br /><br />    backtracing(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">res</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89781.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-11 11:48 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/11/89781.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.4 Electric Fence</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/10/89761.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 10 Jul 2009 14:19:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/10/89761.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89761.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/10/89761.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89761.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89761.html</trackback:ping><description><![CDATA[用一根平行x轴的直线扫描三角形，求交点，然后计算中间点的数，求和即可。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cmath</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">fence9.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">fence9.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n,m,p;<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">p;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> res </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> s,e;<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> y</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;y</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">m;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">y){<br />        s </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> n</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />        e </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ( y</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(n</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">p)</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> ) </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> (y</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(n</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">p)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">p) : floor(((</span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);">)y</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(n</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">p)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">m)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">p);<br />      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">  out&lt;&lt;y&lt;&lt;" "&lt;&lt;s&lt;&lt;" "&lt;&lt;e&lt;&lt;endl;</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        res </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> max(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,(e</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">s</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">));<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">res</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89761.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-10 22:19 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/10/89761.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.4 American Heritage</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 10 Jul 2009 10:51:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89748.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89748.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89748.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 给出一个树的中序遍历和先序遍历，求它的后序遍历。&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/89748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-10 18:51 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.3 A Game</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/09/89638.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Thu, 09 Jul 2009 05:50:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/09/89638.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89638.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/09/89638.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89638.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89638.html</trackback:ping><description><![CDATA[
		<br />动态规划法。<br />用dp[player][start][end]表示player在[start..end]会取得的最大值。<br />如果player==0，那么player有主动权，它要么选start,要么选end.显然，它要选使得对手得分最少的那一个。<br />当它选start时，对手所能到的最大值为dp[1][start+1][end]。当它选end时，对手所选的最大值是dp[1][start][end-1].<br />。所以我们选dp[1][start+1][end]和dp[1][start][end-1]中小的那一个。<br />如果player==1，那只能被动的等0先选了。1在剩下区段中，又作为先选的角色，即player0。<br />当只有一个数字的时候，player0就只有选这个，player1就没得选，返回0.<br />代码如下：<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">game1.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">game1.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> dp[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> sequence[</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> score(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> player,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> start,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> end)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(dp[player][start][end]</span><span style="color: rgb(0, 0, 0);">!=-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> dp[player][start][end];<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(start</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">end){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(player</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />            dp[player][start][end] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sequence[start];<br />        </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> <br />            dp[player][start][end] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> t1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> score(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,start</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,end);<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> t2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> score(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,start,end</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(player</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(t1</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">t2){<br />                dp[player][start][end] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sequence[end]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">score(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,start,end</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />            }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />                dp[player][start][end] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sequence[start]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">score(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,start</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,end);<br />            }<br />        }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(t1</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">t2){<br />                dp[player][start][end] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> score(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,start,end</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />            }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />                dp[player][start][end] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> score(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,start</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,end);<br />            }<br />        }<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> dp[player][start][end];<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    memset(dp,</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(dp));<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> size;<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">size;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">size;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">sequence[i];<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">score(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,size</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">score(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,size</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89638.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-09 13:50 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/09/89638.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.3 Home on the Range</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/08/89557.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Wed, 08 Jul 2009 09:44:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/08/89557.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89557.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/08/89557.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89557.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89557.html</trackback:ping><description><![CDATA[dp题。用dp[i][j][m]表示左上角为(i,j)，边长为m的正方形是否完整。那么<br />如果dp[i][j][m]完整，则当且仅当dp[i][j][m-1],dp[i+1][j][m-1],dp[i][j+1][m-1],dp[i+1][j+1][m-1]的正方形也是完整的(画个图就很清晰了)。由于我们从上到下，从左到右扫描每个点，在每一轮i,j用过一次，就不会再使用，所以只需用二维数组保存dp[i][j]，即可。<br />时间复杂度为O(n^3),空间复杂度为O(n^2)。analysis中有个时间复杂度为O(n^2),空间O(n)的解法。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">range.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">range.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> dp[</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> side_len;<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> input()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">side_len;<br /><br />    </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> t;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">side_len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">side_len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">( </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 255);">get</span><span style="color: rgb(0, 0, 0);">(t)</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">isspace(t) );<br />            dp[i][j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> t</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br />        }<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    input();<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> w </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;w</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">side_len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">w){<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> cnt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /><br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">side_len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">side_len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">w</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">side_len</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">w</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">side_len){<br />                    dp[i][j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> dp[i][j]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">dp[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">][j]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">dp[i][j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">dp[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">][j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">];<br />                    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(dp[i][j])<br />                        cnt</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />                }<br />            }<br />        }<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(cnt</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">w</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">cnt</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />        }<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89557.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-08 17:44 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/08/89557.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.3 Riding The Fences </title><link>http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Tue, 07 Jul 2009 08:59:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89469.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89469.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89469.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 欧拉路径&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/89469.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-07 16:59 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.2 Sweet Butter</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Mon, 06 Jul 2009 12:05:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89393.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89393.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89393.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 稀疏图的多源最短路径问题&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/89393.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-06 20:05 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.2 Magic Squares</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/06/89392.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Mon, 06 Jul 2009 12:01:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/06/89392.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89392.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/06/89392.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89392.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89392.html</trackback:ping><description><![CDATA[ BFS搜索题。代码写的比较乱，用一个set来存储已经访问过的结点。analysis中的标程有不少可以学习的地方，如几个操作的变换，用转换数组来写，代码清晰明了，encode方法就省去了set。我通过保存整个树来输出解，标程通过反向操作，来输出解，做法很巧妙。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">queue</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">msquare.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">msquare.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> final[</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">visited;<br /></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> result[</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> queue_node{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> current[</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">];<br />    queue_node </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">parent;<br />    </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> op;<br />};<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> op(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">current,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> c)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> tmp;<br />    </span><span style="color: rgb(0, 0, 255);">switch</span><span style="color: rgb(0, 0, 0);">(c){<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">A</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />                swap(current[i],current[</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">i]);<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">B</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:<br />            tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> current[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">];<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">i)<br />                current[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> current[i</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">];<br />            current[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />            tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> current[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">];<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />                current[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> current[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">];<br />            current[</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">C</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:<br />            tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> current[</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">];<br />            current[</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> current[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />            current[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> current[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">];<br />            current[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> current[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">];<br />            current[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> cur_value(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">cur)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> res </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        res</span><span style="color: rgb(0, 0, 0);">*=</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">;<br />        res</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">cur[i];<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> res;<br />}<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">final[i];<br />    }<br /><br />    queue</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">queue_node</span><span style="color: rgb(0, 0, 0);">*&gt;</span><span style="color: rgb(0, 0, 0);"> q;<br />    queue_node </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> queue_node;<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        node</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">current[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /><br />    node</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">parent </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> NULL;<br /><br />    q.push(node);<br /><br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">( </span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">q.empty() ){<br />        queue_node </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> q.front();<br />        q.pop();<br /><br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> cur </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> cur_value(node</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">current);<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( visited.find( cur) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> visited.end())<br />            </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);">;<br />        visited.insert(cur);<br /><br />        </span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> ok </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(node</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">current[i]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">final[i]){<br />                ok </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />                </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />            }<br />        }<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(ok){<br />            </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(node</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">parent</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">NULL){<br />                result[i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> node</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">op;<br />                node</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">node</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">parent;<br />            }<br /><br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />                </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />                exit(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />            }<br /><br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br /><br />            </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j;<br />            i</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">,j</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">){<br />                </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">result[i];<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(j</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">59</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />            }<br />            <br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(j</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />                </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br /><br />            exit(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />        }<br /><br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> c</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">A</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;c</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">C</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">c){<br />            queue_node </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> n </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> queue_node;<br />            memcpy(n</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">current,node</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">current,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(node</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">current));<br />            op(n</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">current,c);<br />            n</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">op </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> c;<br />            n</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">parent </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> node;<br />            q.push(n);<br />        }<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89392.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-06 20:01 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/06/89392.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.2 Spinning Wheels</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/04/89252.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sat, 04 Jul 2009 12:49:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/04/89252.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89252.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/04/89252.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89252.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89252.html</trackback:ping><description><![CDATA[最多转360秒，各个轮子就复位了。所以在360秒内，每过一秒检测一下是否可以透过光即可。<br />用一个数组记录能透过0-359的轮子的个数，当在某一度轮子的个数达到了5，则说明光可以透过，输出即可。<br />否则说明是不可能有光透过。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream in(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">spin.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream out(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">spin.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> wedge{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> start;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> extent;<br />};<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> speed[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />wedge wedges[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> wedge_num[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> onesecond()<br />{<br />   </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />       </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">wedge_num[i];</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />           wedges[i][j].start</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">speed[i];<br />           wedges[i][j].start</span><span style="color: rgb(0, 0, 0);">%=</span><span style="color: rgb(0, 0, 0);">360</span><span style="color: rgb(0, 0, 0);">;<br />       }<br />   } <br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> isok()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> tmp[</span><span style="color: rgb(0, 0, 0);">360</span><span style="color: rgb(0, 0, 0);">];<br /><br />    memset(tmp,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(tmp));<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">wedge_num[i];</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;k</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">wedges[i][j].extent;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">k)<br />                tmp[(wedges[i][j].start</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">k)</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">360</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />        }<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">360</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(tmp[i]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">)<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">speed[i];<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">wedge_num[i];<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">wedge_num[i];</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">wedges[i][j].start</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">wedges[i][j].extent;<br />        }<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isok()){<br />        </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">360</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        onesecond();<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isok()){<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />        }<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">none</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89252.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-04 20:49 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/04/89252.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.2  Stringsobits</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/03/89191.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 03 Jul 2009 12:45:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/03/89191.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89191.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/03/89191.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89191.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89191.html</trackback:ping><description><![CDATA[
		<br />首先计算出组合数。用cmb_num[i][j]表示i位数中，"1的位数小于等于j"的数的个数。<br />这样，我们从最左边开始，如果cmb_num[i-1][j]的数大于n，说明第一位为0，因为用i-1位数中"1的位数小于等于j"的数已经大于n个了。<br />如果小于n，说明第一位为1，需要i位，才能使"1的位数小于等于j"的数大于n个了。既然第一位已经是1了，接下来的i-1位组成的数的1的个数只能小于等于n-1位了。迭代输出每一位即可。<br />只是要注意溢出的问题以及cmb_num[0][1]。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">kimbits.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">kimbits.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br />unsigned </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> cmb_num[</span><span style="color: rgb(0, 0, 0);">32</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">32</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> build_cmb_num()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">32</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        cmb_num[i][</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">32</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">i;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j)<br />            cmb_num[i][j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> cmb_num[i</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">][j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">cmb_num[i</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">][j];<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">32</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">32</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            cmb_num[i][j]</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">cmb_num[i][j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">];<br />        }<br />}<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    build_cmb_num();<br /><br />    unsigned  n,l,i;<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">l</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">i;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(unsigned idx</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">n;idx</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">idx){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( i</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> cmb_num[idx</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">][l] ){<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />            i</span><span style="color: rgb(0, 0, 0);">-=</span><span style="color: rgb(0, 0, 0);">cmb_num[idx</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">][l];<br />            l</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">;<br />        }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />        }   <br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div>附题：<br /><center><b><font size="7">Stringsobits</font></b><br /><b>Kim Schrijvers</b></center><p> Consider an ordered set S of strings of N (1 &lt;= N &lt;= 31)
bits.  Bits, of course, are either 0 or 1.

</p><p> This set of strings is interesting because it is ordered and
contains all possible strings of length N that have L (1 &lt;= L
&lt;= N) or fewer bits that are `1'.

</p><p> Your task is to read a number I (1 &lt;= I &lt;= sizeof(S))
from the input and print the Ith element of the ordered set for N
bits with no more than L bits that are `1'.

</p><h3>PROGRAM NAME: kimbits</h3><h3>INPUT FORMAT</h3>

A single line with three space separated integers: N, L, and I.

<h3>SAMPLE INPUT (file kimbits.in) </h3><pre>5 3 19<br /></pre><h3>OUTPUT FORMAT</h3>

A single line containing the integer that represents the Ith element
from the order set, as described.

<h3>SAMPLE OUTPUT (file kimbits.out)</h3><pre>10011<br /></pre><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89191.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-03 20:45 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/03/89191.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.2 Factorials</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/03/89186.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 03 Jul 2009 11:52:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/03/89186.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89186.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/03/89186.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89186.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89186.html</trackback:ping><description><![CDATA[
		<br />这题主要是去掉阶乘末尾的0。是个老题了，编程之美中就有讨论。因为0都是由2*5得来的。只要找出乘数中有多少个2*5对就行了。<br />因为2的个数远多于5，所以只要找出5的个数即可。因为n最大为4220,5的个数为:<br />n/5+n/5/5+n/5/5/5+n/5/5/5/5+n/5/5/5/5/5+n/5/5/5/5/5/5;<br />然后再去除相应数目的2。这样剩下的数只需两两相乘后取最后一位即可。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream in(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">fact4.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream out(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">fact4.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n;<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">n;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> numof5 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> n</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> res </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> tmp;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i;<br />        </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(tmp</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) tmp</span><span style="color: rgb(0, 0, 0);">/=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(tmp</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">numof5</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />            tmp</span><span style="color: rgb(0, 0, 0);">/=</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;<br />            numof5</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">;<br />        }<br />        res</span><span style="color: rgb(0, 0, 0);">*=</span><span style="color: rgb(0, 0, 0);">tmp;<br />        res</span><span style="color: rgb(0, 0, 0);">%=</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">res</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br />原题：<br /><center><b><font size="7">Factorials</font></b><br /></center><p> The factorial of an integer N, written N!, is the product of all
the integers from 1 through N inclusive. The factorial quickly becomes
very large: 13! is too large to store in a 32-bit integer on most
computers, and 70! is too large for most floating-point variables. Your
task is to find the rightmost non-zero digit of n!. For example, 5! =
1 * 2 * 3 * 4 * 5 = 120, so the rightmost non-zero digit of 5! is 2.
Likewise, 7! = 1 * 2 * 3 * 4 * 5 * 6 * 7 = 5040, so the rightmost non-zero
digit of 7! is 4.


</p><h3>PROGRAM NAME: fact4</h3><h3>INPUT FORMAT</h3>

A single positive integer N no larger than 4,220.

<h3>SAMPLE INPUT (file fact4.in) </h3><pre>7<br /></pre><h3>OUTPUT FORMAT</h3>

A single line containing but a single digit: the right most non-zero
digit of N! .

<h3>SAMPLE OUTPUT (file fact4.out)</h3><pre>4<br /></pre><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89186.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-03 19:52 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/03/89186.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.1 Contact</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/02/89112.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Thu, 02 Jul 2009 14:07:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/02/89112.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89112.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/02/89112.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89112.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89112.html</trackback:ping><description><![CDATA[
		<br />字符串的长度有限(最多只有12位)，我建一个trie树来存储所有字符串，然后遍历trie树，将对应的字符串存储到一个vector中，再排序输出。<br />ac以后，看usaco的分析，是用位串来做索引计数，这种方法比较简洁巧妙。为了解决前缀为0的问题，在每个字符串前面加了一个1，输出的时候再去掉。<br />我的解法如下：<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">vector</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">contact.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">contact.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> trie_node{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> cnt;<br />    trie_node</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> sons[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">];<br />    trie_node(){<br />        sons[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sons[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />        cnt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />};<br /><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> sort_node{<br />    </span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> str;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> cnt;<br /><br />    </span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">operator</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> sort_node</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">n2) </span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);">{<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(cnt</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">n2.cnt) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> cnt</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">n2.cnt;<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(str.size()</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">n2.str.size()) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> str.size()</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n2.str.size();<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">str.size();</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(str[i]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">n2.str[i])<br />                </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> str[i]</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n2.str[i];<br />        }<br />    }<br />};<br /><br /></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> buf[</span><span style="color: rgb(0, 0, 0);">200000</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> buf_len;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> a,b,n;<br />vector</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">sort_node</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">res;<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> insert_trie(trie_node</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">root,</span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">start,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br />{<br />    trie_node </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> next </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> root;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(next</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">sons[start[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL)<br />            next</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">sons[start[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> trie_node;<br />        next </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> next</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">sons[start[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">];<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">b)<br />            next</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">cnt</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> _traverse_trie(trie_node </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">root,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> ch,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> depth)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(root</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br /><br />    buf[depth]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ch;<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(root</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">cnt</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />        sort_node n;<br />        n.str </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">buf[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">],</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">buf[depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]);<br />        n.cnt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> root</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">cnt;<br />        res.push_back(n);<br />    }<br /><br />    _traverse_trie(root</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">sons[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">],</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />    _traverse_trie(root</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">sons[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">],</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> traverse_trie(trie_node </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">root)<br />{<br />    _traverse_trie(root</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">sons[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">],</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />    _traverse_trie(root</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">sons[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">],</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />}<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">b</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">n;<br /><br />    </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> ch;<br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 255);">get</span><span style="color: rgb(0, 0, 0);">(ch)){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(ch</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">ch</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />            buf[buf_len</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ch;<br />    }<br /><br />    trie_node root;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">b</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">buf_len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        insert_trie(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">root,</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">buf[i],b);<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">min(b</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,buf_len);i</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">a;</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">i){<br />        insert_trie(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">root,</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">buf[buf_len</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">i],i);<br />    }<br /><br />    traverse_trie(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">root);<br /><br />    sort(res.begin(),res.end());<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> freq_cnt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> last_cnt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> out_cnt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">res.size();</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(res[i].cnt</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">last_cnt){<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(out_cnt</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">res[i].str;<br />            out_cnt</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(out_cnt</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">(res.size()</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">res[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">].cnt</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">res[i].cnt)<br />                </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />        }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />            last_cnt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> res[i].cnt;<br />            freq_cnt</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(freq_cnt</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">n)<br />               </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">; <br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">res[i].cnt</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">res[i].str;<br />            out_cnt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />            out_cnt</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( (i</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">res.size()</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">res[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">].cnt</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">res[i].cnt)<br />                </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />        }<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br />附题：<br /><center><b><font size="7">Contact</font></b><br /><b>IOI'98</b></center><p>The cows have developed a new interest in scanning the universe
outside their farm with radiotelescopes.  Recently, they noticed a very
curious microwave pulsing emission sent right from the centre of the
galaxy. They wish to know if the emission is transmitted by some
extraterrestrial form of intelligent life or if it is nothing but the
usual heartbeat of the stars.

</p><p>Help the cows to find the Truth by providing a tool to analyze bit
patterns in the files they record. They are seeking bit patterns of
length <b>A</b> through <b>B</b> inclusive (1 &lt;= A &lt;= B &lt;= 12)
that repeat themselves most often in each day's data file. They are
looking for the patterns that repeat themselves most often.  An input
limit tells how many of the most frequent patterns to output.

</p><p> Pattern occurrences may overlap, and only patterns that occur at
least once are taken into account.

</p><h3>PROGRAM NAME: contact</h3><h3>INPUT FORMAT</h3><table border="1"><tbody><tr><td> Line 1: </td><td>Three space-separated integers: A, B, N; (1
&lt;= N &lt; 50) </td></tr><tr><td> Lines 2 and beyond: </td><td>A sequence of as many as 200,000
characters, all 0 or 1; the characters are presented 80 per line, except
potentially the last line.</td></tr></tbody></table><h3>SAMPLE INPUT (file contact.in) </h3><pre>2 4 10<br />01010010010001000111101100001010011001111000010010011110010000000<br /><br /></pre><p>In this example, pattern 100 occurs 12 times, and pattern 1000 occurs
5 times. The most frequent pattern is 00, with 23 occurrences.

</p><h3>OUTPUT FORMAT</h3><p>Lines that list the N highest frequencies (in descending order of
frequency) along with the patterns that occur in those frequencies.
Order those patterns by shortest-to-longest and increasing binary number
for those of the same frequency.  If fewer than N highest frequencies
are available, print only those that are.

</p><p>Print the frequency alone by itself on a line.  Then print the actual
patterns space separated, six to a line (unless fewer than six remain).

</p><h3>SAMPLE OUTPUT (file contact.out)</h3><pre>23<br />00<br />15<br />01 10<br />12<br />100<br />11<br />11 000 001<br />10<br />010<br />8<br />0100<br />7<br />0010 1001<br />6<br />111 0000<br />5<br />011 110 1000<br />4<br />0001 0011 1100<br /></pre><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89112.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-02 22:07 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/02/89112.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.1 Stamps</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/30/88924.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Tue, 30 Jun 2009 12:37:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/30/88924.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88924.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/30/88924.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88924.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88924.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 动态规划题&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/30/88924.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-30 20:37 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/30/88924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.1 Humble Numbers</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/30/88846.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Mon, 29 Jun 2009 16:05:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/30/88846.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88846.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/30/88846.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88846.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88846.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 二分法&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/30/88846.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88846.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-30 00:05 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/30/88846.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.1 Score Inflation</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/29/88840.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Mon, 29 Jun 2009 14:21:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/29/88840.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88840.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/29/88840.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88840.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88840.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 背包问题&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/29/88840.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88840.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-29 22:21 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/29/88840.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.1 Agri-Net</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Mon, 29 Jun 2009 13:48:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88836.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88836.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88836.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最小生成树问题<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88836.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-29 21:48 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.4 Fractions to Decimals</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/28/88708.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sun, 28 Jun 2009 10:49:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/28/88708.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88708.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/28/88708.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88708.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88708.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 模拟题，将分数转换成带循环节小数形式。&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/28/88708.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88708.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-28 18:49 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/28/88708.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.4 Bessie Come Home</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sun, 28 Jun 2009 09:25:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88701.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88701.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88701.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最短路径问题&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88701.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-28 17:25 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.4 Cow Tours</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/27/88662.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sat, 27 Jun 2009 14:10:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/27/88662.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88662.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/27/88662.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88662.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88662.html</trackback:ping><description><![CDATA[
		<br />这题综合了dfs,floyd算法。<br /><br />首先用 floyd计算出两两之间的最短路径。然后用dfs将图着色。对每两两不同颜色的结点，我们把它们相连，然后看产生的图的直径的大小。<br />直径的大小只可能仍为原两连通图直径之一，或者是包括新添加的路径所产生的最长路径，取这三者中的最大值。新路径产生的最长路径只可能是两点的距离加上两点到其他点的最长距离。最开始的时候只考虑了新添加的路径，没考虑原直径会比新路径大的情况，wa了n次。。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cfloat</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cmath</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">cowtour.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">cowtour.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> path[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> graph[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> y[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n;<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> color[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> diameter[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> maxPath[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> floyd()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;k</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">k)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">j){<br />                    path[i][j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">min(path[i][j],path[i][k]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">path[k][j]);<br />                }<br />            }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> make_color(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> c)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( color[i]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> )<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br /><br />    color[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> c;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(color[j]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">graph[i][j])<br />            make_color(j,c);<br />    }<br />}<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">n;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">x[i]</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">y[i];<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> s;<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">s;<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            graph[i][j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">s[j]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(graph[i][j]){<br />                path[i][j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqrt( (x[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">x[j])</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(x[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">x[j])<br />                    </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">(y[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">y[j])</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(y[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">y[j]) );<br />            }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />                path[i][j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DBL_MAX</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">;<br />            }<br />        }<br />    }<br /><br />    floyd();<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(color[i]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />            make_color(i,</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">c);<br /><br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        maxPath[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">color[i]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">color[j]){<br />                maxPath[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> max(maxPath[i],path[i][j]);<br />            }<br />        }<br /><br />        diameter[color[i]] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> max(diameter[color[i]],maxPath[i]);<br />    }<br /><br /><br />    </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> dia </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DBL_MAX;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br /><br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(color[i]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">color[j]) </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);">;<br /><br />            </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> d1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> p</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;p</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">p){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">p</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">color[i]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">color[p])<br />                    d1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> max(d1,path[i][p]);<br />            }<br /><br />            </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> d2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> p</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;p</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">p){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(j</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">p</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">color[j]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">color[p])<br />                    d2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> max(d2,path[j][p]);<br />            }<br /><br />            </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> dist </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqrt( (x[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">x[j])</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(x[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">x[j])</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                    (y[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">y[j])</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(y[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">y[j]) );<br /><br />            dia </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> min(dia,max(d1</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">d2</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">dist,max(diameter[color[i]],diameter[color[j]])) );<br />        }<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">.setf(ios::</span><span style="color: rgb(0, 0, 255);">fixed</span><span style="color: rgb(0, 0, 0);">,ios::floatfield);<br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">.precision(</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">);<br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">dia</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/88662.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-27 22:10 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/27/88662.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.4 Overfencing</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/26/88607.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 26 Jun 2009 14:25:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/26/88607.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88607.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/26/88607.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88607.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88607.html</trackback:ping><description><![CDATA[
		<br />找出两个迷宫的出口，然后分别进行dfs，求出到每一个点的最短距离。<br />然后对每一个点，求到最短的那个出口的距离，然后再求这个值的最大值即可。<br />找出口写得比较繁琐。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">queue</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">maze1.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">maze1.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> maze[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">38</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> visited[</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">38</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> shortest1[</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">38</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> shortest2[</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">38</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> get_first;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> exit1r,exit1c;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> exit2r,exit2c;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> w,h;<br /><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> queue_node{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i,j,depth;<br />    queue_node(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> depth){<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i;<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">j </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> j;<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">depth </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> depth;<br />    }<br />};<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> bfs(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> shortest[</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">38</span><span style="color: rgb(0, 0, 0);">])<br />{<br />    queue</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">queue_node</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">q;<br /><br />    q.push(queue_node(i,j,</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">));<br /><br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">q.empty()){<br /><br />        queue_node node </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> q.front();<br />        q.pop();<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(visited[node.i][node.j])<br />            </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);">;<br /><br />        visited[node.i][node.j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br /><br />        shortest[node.i][node.j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> node.depth;<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(node.i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">maze[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node.i][</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node.j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">){<br />            q.push(queue_node(node.i</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,node.j,node.depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">));<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(node.i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">h</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">maze[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node.i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node.j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">){<br />            q.push(queue_node(node.i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,node.j,node.depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">));<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(node.j</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">maze[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node.i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node.j]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">){<br />            q.push(queue_node(node.i,node.j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,node.depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">));<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(node.j</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">w</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">maze[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node.i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">node.j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">){<br />            q.push(queue_node(node.i,node.j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,node.depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">));<br />        }<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">w</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">h;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">h</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">w</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br /><br />            </span><span style="color: rgb(0, 0, 255);">do</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 255);">get</span><span style="color: rgb(0, 0, 0);">(maze[i][j]);<br />            }</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(maze[i][j]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">);<br /><br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">((i</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">h</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">w)</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">maze[i][j]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">get_first){<br />                    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">h)<br />                        exit1r </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> h</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />                    </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />                        exit1r </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;<br />                    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(j</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">w)<br />                        exit1c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> w</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />                    </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />                        exit1c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;<br />                    get_first </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />                }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />                    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">h)<br />                        exit2r </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> h</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />                    </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />                        exit2r </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;<br />                    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(j</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">w)<br />                        exit2c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> w</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />                    </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />                        exit2c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;<br />                }<br />            }<br />        }<br /><br />    memset(visited,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(visited));<br />    bfs(exit1r,exit1c,shortest1);<br />    memset(visited,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(visited));<br />    bfs(exit2r,exit2c,shortest2);<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> res </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> INT_MIN;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">h;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">w;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />         res </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> max(res, min(shortest1[i][j],shortest2[i][j]) );<br />     }<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">res</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/88607.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-26 22:25 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/26/88607.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.4 The Tamworth Two</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/26/88597.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 26 Jun 2009 12:48:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/26/88597.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88597.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/26/88597.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88597.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88597.html</trackback:ping><description><![CDATA[
		<br />模拟题。按题目要求编码即可，每过一分钟，更新一下农夫和奶牛的状态。如果该状态以前出现过，说明有循环，不可能到达，输出0.<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ttwo.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ttwo.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> grid[</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> visited[</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> north </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> east </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> south </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> west </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">;<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">方向为0,1,2,3时向前走一步row和column的增加值</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> delr[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {<br />    </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"><br />};<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> delc[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {<br />    </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />};<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> farmer的当前row,column,direction.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> fc,fr,fd;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> cc,cr,cd;<br /><br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> available(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">grid[i][j]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> one_step()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( available( fr</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">delr[fd],fc</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">delc[fd] ) ){<br />        fr </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> delr[fd];<br />        fc </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> delc[fd];<br />    }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />        fd</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />        fd</span><span style="color: rgb(0, 0, 0);">%=</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( available( cr</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">delr[cd],cc</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">delc[cd] ) ){<br />        cr </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> delr[cd];<br />        cc </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> delc[cd];<br />    }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />        cd</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />        cd</span><span style="color: rgb(0, 0, 0);">%=</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">grid[i][j];<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(grid[i][j]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">F</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />                fr</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i,fc</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">j,fd</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(grid[i][j]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">C</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />                cr</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i,cc</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">j,cd</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />        }<br /><br />    memset(visited,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(visited));<br /><br />    visited[fc][fr][fd][cc][cr][cd] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> res </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">){<br /><br />        one_step();<br />        res</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( fc</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">cc</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">fr</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">cr ){<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">res</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />            exit(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />        }<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(visited[fc][fr][fd][cc][cr][cd]){<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />            exit(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />        }<br /><br />        visited[fc][fr][fd][cc][cr][cd]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/88597.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-26 20:48 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/26/88597.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.3 Controlling Companies</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/26/88596.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 26 Jun 2009 12:01:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/26/88596.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88596.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/26/88596.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88596.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88596.html</trackback:ping><description><![CDATA[每加一个新结点，更新一下对各公司控制值,递归地加入新控制的结点即可。用一个布尔向量记录一下已经控制的公司，以免无穷递归。<br /><br /><br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">concom.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">concom.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> stock[</span><span style="color: rgb(0, 0, 0);">101</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">101</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n;<br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> control[</span><span style="color: rgb(0, 0, 0);">101</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> add_control(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> cr,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> ce)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(control[ce]) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br /><br />    control[ce] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">; i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">cr</span><span style="color: rgb(0, 0, 0);">&amp;&amp;!</span><span style="color: rgb(0, 0, 0);">control[i]){<br />            stock[cr][i]</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">stock[ce][i];<br /><br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(stock[cr][i]</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">50</span><span style="color: rgb(0, 0, 0);">)<br />                add_control(cr,i);<br />        }<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    memset(stock,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(stock));<br />    <br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">n;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i,j,p;<br /><br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(n</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">){<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">p;<br />        stock[i][j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> p;<br />    }<br />    <br /><br />   </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br /><br />       memset(control,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(control));<br /><br />       </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br /><br />           </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">j) </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);">;<br /><br />           </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( stock[i][j]</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">50</span><span style="color: rgb(0, 0, 0);">){<br />               add_control(i,j);<br />           }<br />       }<br /><br />       </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />           </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(control[j]){<br />               </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />           }<br />       }<br />   } <br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/88596.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-26 20:01 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/26/88596.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.3 Money Systems</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/25/88530.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Thu, 25 Jun 2009 14:16:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/25/88530.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88530.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/25/88530.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88530.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88530.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 动态规划经典题&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/25/88530.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88530.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-25 22:16 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/25/88530.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.3 Zero Sum</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/24/88457.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Wed, 24 Jun 2009 13:35:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/24/88457.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88457.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/24/88457.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88457.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88457.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 简单的回溯题&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/24/88457.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88457.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-24 21:35 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/24/88457.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.3 Cow Pedigrees</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/24/88453.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Wed, 24 Jun 2009 12:55:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/24/88453.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88453.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/24/88453.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88453.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88453.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: memoization dp, 树的计数&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/24/88453.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88453.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-24 20:55 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/24/88453.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.3 Longest Prefix</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/23/88394.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Tue, 23 Jun 2009 13:13:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/23/88394.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88394.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/23/88394.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88394.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88394.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Trie树+简单DP&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/23/88394.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88394.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-23 21:13 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/23/88394.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.2 Runaround Numbers</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/20/88197.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sat, 20 Jun 2009 14:35:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/20/88197.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88197.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/20/88197.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88197.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88197.html</trackback:ping><description><![CDATA[
		<br />生成所有长度小于9的排列数，然后判断是否为runaround数且大于m，输出第一个大于m的直接exit即可。<br />因为9! = 362880，数据较小，不会超时。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">runround.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">runround.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> m;<br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> mark[</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> figures[</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">];<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve();<br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> permutation(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> max_dep);<br />unsigned </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> get_value(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len);<br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> isok(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len);<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">m;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> start </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> m;<br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(tmp){<br />        tmp</span><span style="color: rgb(0, 0, 0);">/=</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">;<br />        start</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">start;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        permutation(i);<br />    }<br />}<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> _permutation(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> depth,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> max_dep)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(depth</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">max_dep){<br />      </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isok(max_dep)){<br /> </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);">        for(int i=0;i&lt;max_dep;++i)<br />            cout&lt;&lt;figures[i]&lt;&lt;' ';<br />        cout&lt;&lt;endl;<br />  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);">       unsigned </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> t </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> get_value(max_dep);<br /><br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(t</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">m){<br />                </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">t</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />                exit(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />            }<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">mark[i]){<br />            mark[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />            figures[depth] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i;<br />            _permutation(depth</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,max_dep);<br />            mark[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />        }<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">生成长度为len的全排列</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> permutation(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br />{<br />    memset(mark,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(mark));<br />    _permutation(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,len);<br />}<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">是runaround数</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> isok(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> unvisited </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> len;<br />    </span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> mark[</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">];<br />    memset(mark,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(mark));<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(unvisited</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">){<br />       i</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">figures[i]; i</span><span style="color: rgb(0, 0, 0);">%=</span><span style="color: rgb(0, 0, 0);">(len);<br />       </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mark[i]) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />       mark[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">将数组转化成unsigned long</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">unsigned </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> get_value(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br />{<br />    unsigned </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> res </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        res</span><span style="color: rgb(0, 0, 0);">*=</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">;<br />        res</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">figures[i];<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> res;<br />}<br /><br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/88197.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-20 22:35 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/20/88197.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.2 Party Lamps</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/20/88188.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sat, 20 Jun 2009 12:33:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/20/88188.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88188.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/20/88188.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88188.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88188.html</trackback:ping><description><![CDATA[
		<br />关键在于发现几个规律：<br />某一按钮，按偶数次等同于没按。<br />按钮的先后次序不影响结果。<br />灯的状态6位一组不变(解题时没发现这个规律，开了个100大小的bool数组)<br /><br />剩下就是几个操作的排列组合了。代码写得很繁...<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">vector</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">algorithm</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">lamps.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">lamps.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> state_node{<br />    </span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> states[</span><span style="color: rgb(0, 0, 0);">101</span><span style="color: rgb(0, 0, 0);">];<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len;<br />    </span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">operator</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> state_node </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> n1) </span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);">{<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(states[i]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">n1.states[i])<br />                </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> states[i]</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n1.states[i];<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">operator</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> state_node </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> n1) </span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);">{<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">len;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(states[i]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">n1.states[i])<br />                </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />};<br /><br />state_node node;<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n,c;<br />vector</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">finalon,finaloff;<br /><br />vector</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">state_node</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> result;<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> op(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> kind)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">switch</span><span style="color: rgb(0, 0, 0);">(kind){<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />                 node.states[i]</span><span style="color: rgb(0, 0, 0);">=!</span><span style="color: rgb(0, 0, 0);">node.states[i];<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />                    node.states[i]</span><span style="color: rgb(0, 0, 0);">=!</span><span style="color: rgb(0, 0, 0);">node.states[i];<br />            }<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">:<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">))<br />                    node.states[i]</span><span style="color: rgb(0, 0, 0);">=!</span><span style="color: rgb(0, 0, 0);">node.states[i];<br />            }<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">:<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">){<br />                    node.states[i]</span><span style="color: rgb(0, 0, 0);">=!</span><span style="color: rgb(0, 0, 0);">node.states[i];<br />                }<br />            }<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> isok()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">finalon.size();</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">node.states[finalon[i]])<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">finaloff.size();</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(node.states[finaloff[i]])<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> do0()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isok())<br />        result.push_back(node);<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> do1()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        op(i);<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isok()){<br />            result.push_back(node);<br />        }<br />        op(i);<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> do2()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            op(i);<br />            op(j);<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isok()){<br />                result.push_back(node);<br />            }<br />            op(i);<br />            op(j);<br /><br />        }<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> do3()<br />{<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">操作1,2,3,4各执行一次，前三个操作抵消</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    op(</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">);<br />    do1();<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> do4()<br />{<br />    op(</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">);<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isok()){<br />        result.push_back(node);<br />    }<br />    op(</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">);<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> action(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">switch</span><span style="color: rgb(0, 0, 0);">(i){<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">: do0();</span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">: do1();</span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">: do2();</span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">: do3();</span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">: do4();</span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">c;<br /><br />    node.len </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> n;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i) <br />        node.states[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> t;<br />    </span><span style="color: rgb(0, 0, 255);">do</span><span style="color: rgb(0, 0, 0);">{<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">t;<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(t</span><span style="color: rgb(0, 0, 0);">!=-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />            finalon.push_back(t);<br />    }</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(t</span><span style="color: rgb(0, 0, 0);">!=-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /><br />     </span><span style="color: rgb(0, 0, 255);">do</span><span style="color: rgb(0, 0, 0);">{<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">t;<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(t</span><span style="color: rgb(0, 0, 0);">!=-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />            finaloff.push_back(t);<br />    }</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(t</span><span style="color: rgb(0, 0, 0);">!=-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /><br />    </span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> has_result </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(c</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">(c</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">i)</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />            action(i);<br />        }<br />    }<br /><br />    sort(result.begin(),result.end());<br />    vector</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">state_node</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">::iterator end </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> <br />        unique(result.begin(),result.end());<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(vector</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">state_node</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">::iterator i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> result.begin();<br />            i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">end; </span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        has_result </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">states[j];<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">has_result){<br />        </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">IMPOSSIBLE</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /><br /></span></div><br /><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/88188.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-20 20:33 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/20/88188.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>