﻿<?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++博客-止于自娱</title><link>http://www.cppblog.com/yuziyu/</link><description>每天进步一点点,Coding Everyday!</description><language>zh-cn</language><lastBuildDate>Sat, 04 Apr 2026 08:33:56 GMT</lastBuildDate><pubDate>Sat, 04 Apr 2026 08:33:56 GMT</pubDate><ttl>60</ttl><item><title>[备忘]函数调用约定</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/23/90900.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Thu, 23 Jul 2009 02:58:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/23/90900.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90900.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/23/90900.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90900.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90900.html</trackback:ping><description><![CDATA[常见的调用约定有： 
stdcall 
cdecl 
fastcall 
thiscall 
naked call<br />stdcall是pascal语言的默认调用约定，从右向左压栈，由被调用者负责清栈<br />cdecl是c语言的默认调用约定，从右向左压栈，由调用者负责清栈。<br /><img src ="http://www.cppblog.com/yuziyu/aggbug/90900.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-23 10:58 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/23/90900.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[基础算法复习]基数排序</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/17/90382.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 17 Jul 2009 11:49:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/17/90382.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90382.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/17/90382.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90382.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90382.html</trackback:ping><description><![CDATA[
		<br />基数排序每一遍对待排数的某一位进行计数排序，依次从最低位到最高位。<br />下面程序把非负数按16进制处理，每次取16进制的一位。这样比用10进制方便快捷很多。<br />缺点是不能处理负数。可以将所有数都增加一个基数所其成为正数。排序完成后，再减去这个基数。<br />但是对于32位最小的负数1&lt;&lt;31这样一个特例，是不行的。<br />用一个中间数组保存中间结果，每一遍排完后，交换两指针，这样可以避免多次数据复制。由于一共有8遍，结束后，array中为最后一次排完序的结果。<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, 255);">void</span><span style="color: rgb(0, 0, 0);"> _radix_sort(</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);">src,</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);">dst,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> offset);<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> radix_sort(</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);">array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> begin,</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);">(array</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">begin</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">end) </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><span style="color: rgb(0, 0, 255);">int</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);"> end</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">begin</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);"> </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);"> malloc(</span><span style="color: rgb(0, 0, 255);">sizeof</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);">)</span><span style="color: rgb(0, 0, 0);">*</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);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">src,</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">dst;<br /><br />    src </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array;<br />    dst </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</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);">(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);">;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);">){<br />        _radix_sort(src,dst,len,i);<br />        tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> src;<br />        src </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> dst;<br />        dst </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />    }<br /><br />    free(dst);<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, 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);"> _radix_sort(</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);">src,</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);">dst,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> offset)<br />{<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);">16</span><span style="color: rgb(0, 0, 0);">];<br />    memset(cnt,</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);">(cnt));<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> mask </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0xF</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">offset;<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);">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);">(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 />        cnt[ (src[i]</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">mask)</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offset ] </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);">(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);">16</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 />        cnt[i]</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">cnt[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 /><br />    </span><span style="color: rgb(0, 0, 255);">for</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);">len</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);">&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);">i){<br />        dst[</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">cnt[(src[i]</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">mask)</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offset]] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> src[i]; <br />    }<br />}<br /><br /></span></div><br /><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/90382.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 19:49 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/17/90382.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[备忘]'#'号的预处理</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/17/90356.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 17 Jul 2009 08:55:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/17/90356.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90356.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/17/90356.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90356.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90356.html</trackback:ping><description><![CDATA[
		<br />#define macro(x) {#x,do_##x}<br />macro(hello)<br />预处理后展开为:<br />{"hello",do_hello}<br /><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/90356.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 16:55 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/17/90356.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><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>[基础算法复习]原地置换的间接排序</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/16/90212.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Thu, 16 Jul 2009 03:52:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/16/90212.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90212.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/16/90212.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90212.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90212.html</trackback:ping><description><![CDATA[对于复制代价很高的元素，通过某种排序算法进行间接排序。<br />排序完成后，再一次复制回去。<br />这样需要一个中间数组，进行2N次复制。<br />通过原地置换，我们可以只使用一个中间变量，最多进行3N/2次复制即可达到目的。<br /><br />如index[1]==3。那么，说明array[1]这个位置应该放的是array[3].我们将array[1]保存到tmp中。<br />然后array[1]=array[3].现在array[3]是可以放置的了。那么我们看array[3]应该放什么，如果index[3]==2,刚好我们把tmp放回去。<br />不然，我们继续按这个链找下去。<br />如果链长为x.那么我们需要x+1次复制。<br />链长最小为2.所以我们最多只需要3N/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, 255);">int</span><span style="color: rgb(0, 0, 0);"> indirect_sort(</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);">array,</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);">if</span><span style="color: rgb(0, 0, 0);">(array</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">len</span><span style="color: rgb(0, 0, 0);">&lt;=</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);">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><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">索引数组</span><span style="color: rgb(0, 128, 0);"><br /></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);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">index </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> malloc(</span><span style="color: rgb(0, 0, 255);">sizeof</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);">)</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);">(index</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);"> </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);"> i,j,largest,tmp,tmp2;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</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 />        index[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i;<br />    }<br /><br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">插入排序</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</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 />        tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> index[i];<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);">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);">&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);">array[index[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);">&gt;</span><span style="color: rgb(0, 0, 0);">array[tmp];</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">j){<br />            index[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> index[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 />        index[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</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, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">如果index[i]==i，说明array[i]已经放到了最终的地方。</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( index[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);">continue</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 />            tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[i];<br />            j </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);">( index[j]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">i ){<br />                </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">array[j]应该放的是array[index[j]]</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">                array[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[index[j]];<br />                tmp2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> j;<br />                j </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> index[j];<br />                </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">原来的array[j]已经放好了</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">                index[tmp2] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp2;<br />            }<br />            array[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />            index[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> j;<br />        }<br />    }<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, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/90212.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-16 11:52 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/16/90212.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[基础算法复习]Shell排序</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/16/90201.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Thu, 16 Jul 2009 01:33:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/16/90201.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90201.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/16/90201.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90201.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90201.html</trackback:ping><description><![CDATA[
		<br />Shell排序使用一个递增序列h1,h2,h3...hk. h1==1。<br />从hk开始，每次将间隔hx的序列排好序，直到h1。间隔hx的序列排好序的数组可以称之为hx有序。<br />Shell排序有一个重要的性质是一个hx有序数组，必然是一个hx+1有序数组。<br />每一遍排序过程可以使用插入排序。<br /><br />Shell排序的性能取决于递增序列的选择。下面代码的递增序列是len/2,len/4...,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, 255);">int</span><span style="color: rgb(0, 0, 0);"> shell_sort(</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);">array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> begin,</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);">(array</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">begin</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">end) </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><span style="color: rgb(0, 0, 255);">int</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);"> end</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">begin</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);"> i,j,gap,tmp;<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(gap</span><span style="color: rgb(0, 0, 0);">=</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);">2</span><span style="color: rgb(0, 0, 0);">;gap</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);">;gap</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);">(i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">begin</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">gap;i</span><span style="color: rgb(0, 0, 0);">&lt;=</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 />           j </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);"> array[j];<br />           </span><span style="color: rgb(0, 0, 255);">while</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);">gap</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">begin</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">array[j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">gap]</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">tmp ){<br />               array[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">gap];<br />               j</span><span style="color: rgb(0, 0, 0);">-=</span><span style="color: rgb(0, 0, 0);">gap;<br />           }<br />           array[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />       }<br />    }<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, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/90201.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-16 09:33 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/16/90201.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[备忘]内存对齐问题</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/15/90145.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Wed, 15 Jul 2009 08:37:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/15/90145.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90145.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/15/90145.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90145.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90145.html</trackback:ping><description><![CDATA[
		<br />昨天和wub讨论，为什么struct对齐的时候，double类型的地址要以8对齐。<br />对齐的目的就是为了方便和加速处理器读取，那么只需要和字长对齐就可以了。<br />根据这两篇文章：<a href="http://blog.csdn.net/warrior3/archive/2006/11/20/1399591.aspx"><font size="3">《内存对齐与ANSI C中struct型数据的内存布局》</font></a>vs<a href="http://hi.baidu.com/phps/blog/item/f03eb93ee12f49fa838b1365.html">《难以理解的《内存对齐与ANSI C中struct型数据的内存布局》</a><a href="http://hi.baidu.com/phps/blog/item/f03eb93ee12f49fa838b1365.html">》</a><font size="3">gcc和vc下是不同的。<span style="font-size: 12pt; color: black;">对于double类型，默认gcc的</span><span style="font-size: 12pt; color: black;">对齐模数</span></font><span style="font-size: 12pt; color: black;"><font size="3">是4,VC是8.<br />内存对齐是与处理器，编译器，编译选项相关的，因此单纯给出一个struct，问其sizeof是多少，这样的问法是不严谨的。回答该问题，应该加以说明运行环境。<br />vc的默认对齐模数是：sizeof(该类型)<br />gcc的默认对齐规则是：byte为1,char为2,其他均为4.</font><br />由上可知<br />struct s{<br /> char c;<br /> double d;<br />};<br /><br />gcc下<br />sizeof(s) = 4+8 ＝ 12<br />vc下<br />sizeof(s) = 8+8 = 16<br /><br />那么 struct s{<br />double d;<br />char c;<br />};<br />起初我认为c以后不需要填充，因此gcc下sizeof(s)=8+1 = 9。但事实应该是sizeof(s)==8+4 = 12。<br />因为对于数组来说，要保证数组的大小等于数组的个数乘以元素的大小。因此c之后也是需要填充的,这样后续的元素的地址才会是字长的倍数。<br /></span><img src ="http://www.cppblog.com/yuziyu/aggbug/90145.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-15 16:37 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/15/90145.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[基础算法复习]冒泡排序和选择排序</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/15/90126.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Wed, 15 Jul 2009 03:59:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/15/90126.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90126.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/15/90126.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90126.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90126.html</trackback:ping><description><![CDATA[选择排序和冒泡排序很相似，时间复杂度相同，选择排序性能上要优于冒泡，一次选择过程中只交换一次，比较次数和冒泡相同。<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, 255);">int</span><span style="color: rgb(0, 0, 0);"> bubble_sort(</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);">array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> begin,</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);">(array</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">begin</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">end) </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><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i,j;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</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);">end;i</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">begin;</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);">(j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">begin;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 />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( array[j]</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">array[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);">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);"> array[j];<br />                array[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[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 />                array[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);"> tmp;<br />            }<br />        }<br />    }<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, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div>选择排序：<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, 255);">int</span><span style="color: rgb(0, 0, 0);"> select_sort(</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);">array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> begin,</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);">(array</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">begin</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">end) </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><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i,j,largest,tmp;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</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);">end;i</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">begin;</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">i){<br /><br />        largest </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);">(j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">begin;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 />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( array[largest]</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">array[j] )<br />                largest </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> j;<br />        }<br />        tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[i];<br />        array[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[largest];<br />        array[largest] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />    }<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, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/90126.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-15 11:59 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/15/90126.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[基础算法复习]归并排序</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/15/90122.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Wed, 15 Jul 2009 03:46:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/15/90122.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90122.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/15/90122.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90122.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90122.html</trackback:ping><description><![CDATA[
		<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, 255);">
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">void</span>
				<span style="color: rgb(0, 0, 0);"> _merge(</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);">src,</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> begin,</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);">int</span>
				<span style="color: rgb(0, 0, 0);"> merge_sort(</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);">array,</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> begin,</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);">(array</span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);">NULL</span>
				<span style="color: rgb(0, 0, 0);">||</span>
				<span style="color: rgb(0, 0, 0);">begin</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">end) </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);">;</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);"> mid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> begin</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);">begin)</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 />   merge_sort(src,begin,mid);<br />   merge_sort(src,mid</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 />   _merge(src,begin,end);<br /><br />    return 1;<br />}<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">void</span>
				<span style="color: rgb(0, 0, 0);"> _merge(</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);">src,</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> begin,</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);">int</span>
				<span style="color: rgb(0, 0, 0);"> mid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> begin</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);">begin)</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 />    </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> b1 </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> begin;<br />    </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> e1 </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> mid;<br />    </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> b2 </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> mid</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);"> e2 </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> end;<br /><br />    </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);">dest </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> malloc(</span>
				<span style="color: rgb(0, 0, 255);">sizeof</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);">)</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);">begin</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);">(dest</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 />    </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> i1;<br />    </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> i2;<br />    </span>
				<span style="color: rgb(0, 0, 255);">int</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);">(i1</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">b1,i2</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">b2,i</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">begin;i1</span>
				<span style="color: rgb(0, 0, 0);">&lt;=</span>
				<span style="color: rgb(0, 0, 0);">e1</span>
				<span style="color: rgb(0, 0, 0);">&amp;&amp;</span>
				<span style="color: rgb(0, 0, 0);">i2</span>
				<span style="color: rgb(0, 0, 0);">&lt;=</span>
				<span style="color: rgb(0, 0, 0);">e2</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);">end;</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);">(src[i1]</span>
				<span style="color: rgb(0, 0, 0);">&lt;</span>
				<span style="color: rgb(0, 0, 0);">src[i2]){<br />            dest[i</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">begin] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> src[i1];<br />            i1</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 />            dest[i</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">begin] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> src[i2];<br />            i2</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);">(;i</span>
				<span style="color: rgb(0, 0, 0);">&lt;=</span>
				<span style="color: rgb(0, 0, 0);">end</span>
				<span style="color: rgb(0, 0, 0);">&amp;&amp;</span>
				<span style="color: rgb(0, 0, 0);">i1</span>
				<span style="color: rgb(0, 0, 0);">&lt;=</span>
				<span style="color: rgb(0, 0, 0);">e1;</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);">i1)<br />       dest[i</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">begin] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> src[i1];<br />    </span>
				<span style="color: rgb(0, 0, 255);">for</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);">end</span>
				<span style="color: rgb(0, 0, 0);">&amp;&amp;</span>
				<span style="color: rgb(0, 0, 0);">i2</span>
				<span style="color: rgb(0, 0, 0);">&lt;=</span>
				<span style="color: rgb(0, 0, 0);">e2;</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);">i2)<br />       dest[i</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">begin] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> src[i2];<br /><br />    </span>
				<span style="color: rgb(0, 0, 255);">for</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);">begin;i</span>
				<span style="color: rgb(0, 0, 0);">&lt;=</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 />        src[i] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> dest[i</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">begin];<br /><br />    free(dest);<br />}<br /><br /></span>
		</div>
		<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, 255);">void</span><span style="color: rgb(0, 0, 0);"> _mergesort(</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);">array,</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);">tmp,</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);">void</span><span style="color: rgb(0, 0, 0);"> mergesort(</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);">array,</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);"> i,</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">tmp;<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(array</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL</span><span style="color: rgb(0, 0, 0);">||</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);">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 />    tmp </span><span style="color: rgb(0, 0, 0);">=</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);">*</span><span style="color: rgb(0, 0, 0);">)malloc(</span><span style="color: rgb(0, 0, 255);">sizeof</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);">)</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">len);<br /><br />    _mergesort(array,tmp,</span><span style="color: rgb(0, 0, 0);">0</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);">1</span><span style="color: rgb(0, 0, 0);">);<br /><br />    free(tmp);<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> _mergesort(</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);">array,</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);">tmp,</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);">int</span><span style="color: rgb(0, 0, 0);"> mid </span><span style="color: rgb(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);">end)</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);"> i,j,k;<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);">&gt;=</span><span style="color: rgb(0, 0, 0);">end)<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    <br />    _mergesort(array,tmp,start,mid);<br />    _mergesort(array,tmp,mid</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 />   i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> start;<br />   j </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> mid</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);">(k</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">start;k</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">end</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);">mid</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);">end;</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);">if</span><span style="color: rgb(0, 0, 0);">(array[i]</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">array[j]){<br />           tmp[k] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[i];<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);">else</span><span style="color: rgb(0, 0, 0);">{<br />           tmp[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[j];<br />           j</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);">(;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">mid;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />       tmp[k</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);">array[i];<br /><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);">&lt;=</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);">j)<br />       tmp[k</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);">array[j];<br />      <br />  memcpy(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">array[start],</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">tmp[start],</span><span style="color: rgb(0, 0, 255);">sizeof</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);">)</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);">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);">)); <br />}<br />    <br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/90122.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-15 11:46 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/15/90122.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[基础算法复习]堆排序</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/15/90109.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Wed, 15 Jul 2009 01:50:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/15/90109.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90109.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/15/90109.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90109.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90109.html</trackback:ping><description><![CDATA[
		<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, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">void</span>
				<span style="color: rgb(0, 0, 0);"> _build_heap(</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);">array,</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);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">void</span>
				<span style="color: rgb(0, 0, 0);"> _adjust_heap(</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);">array,</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> idx,</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);"> heap_sort(</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);">array,</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> begin,</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);">(array</span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);">NULL</span>
				<span style="color: rgb(0, 0, 0);">||</span>
				<span style="color: rgb(0, 0, 0);">begin</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">end) </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>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);">自此以后，index从1开始。</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    array</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);">int</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);"> end</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">begin</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 />    _build_heap(array,len);<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);">int</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);">(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 />        tmp </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> array[len</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 />        array[len</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);">] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> array[</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">];<br />        array[</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 />        _adjust_heap(array,</span>
				<span style="color: rgb(0, 0, 0);">1</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 />    }<br />}<br /><br /></span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);">input: 任意数组 output:大顶堆</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">void</span>
				<span style="color: rgb(0, 0, 0);"> _build_heap(</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);">array,</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);"> i;<br />   </span>
				<span style="color: rgb(0, 0, 255);">for</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);">len</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);">;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 />        _adjust_heap(array,i,len);<br />   }<br />}<br /><br /></span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);">使重新使array满足堆特性</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">void</span>
				<span style="color: rgb(0, 0, 0);"> _adjust_heap(</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);">array,</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> idx,</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);"> left;<br />    </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> right;<br />    </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> larger </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);">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);"> array[idx];<br /><br />    left </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> (idx</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 />    right </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> left</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);">while</span>
				<span style="color: rgb(0, 0, 0);">( left</span>
				<span style="color: rgb(0, 0, 0);">&lt;=</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);">(right</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);">&amp;&amp;</span>
				<span style="color: rgb(0, 0, 0);">array[right]</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">array[left]){<br />            larger </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> right; <br />        }</span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);">{<br />            larger </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> left;<br />        }<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);">( array[larger]</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">tmp ){<br />            array[idx] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> array[larger];<br />            idx </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> larger;<br />            left </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> (idx</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 />            right </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> left</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 />            </span>
				<span style="color: rgb(0, 0, 255);">break</span>
				<span style="color: rgb(0, 0, 0);">;<br />        }<br />    }<br /><br />    array[idx] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> tmp;<br />}<br /><br /></span>
		</div>
		<br />
<img src ="http://www.cppblog.com/yuziyu/aggbug/90109.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-15 09:50 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/15/90109.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[基础算法复习]快速排序</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/14/90079.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Tue, 14 Jul 2009 13:58:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/14/90079.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90079.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/14/90079.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90079.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90079.html</trackback:ping><description><![CDATA[最基础的快速排序<br />优点：编码简单，清晰<br />缺点：对于排好序的输入，时间复杂度为O(n^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);"><br /></span><span style="color: rgb(0, 0, 255);">static</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);"> partition(</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);">array,</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);">int</span><span style="color: rgb(0, 0, 0);"> quicksort(</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);">array,</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);">(array</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);">start</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">end) </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><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);"> partition(array,start,end);<br /><br />    quicksort(array,start,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 />    quicksort(array,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);">,end);<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, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">static</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);"> partition(</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);">array,</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);">int</span><span style="color: rgb(0, 0, 0);"> pivot </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[start];    <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);"> start;<br />    </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);"> end;<br /><br />    </span><span style="color: rgb(0, 0, 255);">while</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);">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, 0);"></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);">i</span><span style="color: rgb(0, 0, 0);">&amp;&amp; array[j]</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">pivot</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 />        array[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[j];<br />        </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">( j&gt;i&amp;&amp; array[i]</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">pivot</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);"> ) i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />        array[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[i];<br />    }<br /><br />    array[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> pivot;<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> i;<br />}<br /><br /></span></div><br />改进：小数组直接用插入排序实现，中枢值取(begin,mid,end)三者的中间值，对有序数组排序仍为O(nlogn)。减少了边界条件检查<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);">stdio.h</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);">stdlib.h</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);">memory.h</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);">#define</span><span style="color: rgb(0, 0, 0);"> SMALL_N 10</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">static</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);"> partition(</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);">array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> begin,</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);">void</span><span style="color: rgb(0, 0, 0);"> _quicksort(</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);">array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> begin,</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);">void</span><span style="color: rgb(0, 0, 0);"> insertsort(</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);">array,</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);"> i;<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(array</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL</span><span style="color: rgb(0, 0, 0);">||</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);">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 />    </span><span style="color: rgb(0, 0, 255);">for</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);">int</span><span style="color: rgb(0, 0, 0);"> temp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[i];<br />        </span><span style="color: rgb(0, 0, 255);">int</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);">(j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i;j</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);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">temp</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">array[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);">j){<br />            array[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[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 />        array[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> temp;<br />    }<br /><br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> quicksort(</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);">array,</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);">if</span><span style="color: rgb(0, 0, 0);">(array</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">NULL</span><span style="color: rgb(0, 0, 0);">||</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);">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 />    _quicksort(array,</span><span style="color: rgb(0, 0, 0);">0</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);">1</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);"> _quicksort(</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);">array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> begin,</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);">int</span><span style="color: rgb(0, 0, 0);"> pivot;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pivot_pos;<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</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);">begin</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);">SMALL_N){<br />        insertsort(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">array[begin],end</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">begin</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);">;<br />    }<br /><br />    pivot_pos </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> partition(array,begin,end);<br />    _quicksort(array,begin,pivot_pos</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 />    _quicksort(array,pivot_pos</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 /></span><span style="color: rgb(0, 0, 255);">static</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);">  mid3(</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);">array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> begin,</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);">int</span><span style="color: rgb(0, 0, 0);"> mid </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);">begin)</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);">begin;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> tmp;<br /><br />    tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[mid];<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(tmp</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">array[begin]){<br />        array[mid] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[begin];<br />        array[begin] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />    }<br /><br />    tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[end];<br /><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(tmp</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">array[mid]){<br />        array[end] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[mid];<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(tmp</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">array[begin]){<br />           array[mid] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[begin];<br />           array[begin] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />            array[mid] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />        }<br />    }<br /><br />    tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[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 />    array[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);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[mid];<br />    array[mid] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />    <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> array[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 />}<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">static</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);"> partition(</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);">array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> begin,</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);">int</span><span style="color: rgb(0, 0, 0);"> pivot </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> mid3(array,begin,end);<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i, j;<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> tmp;<br /><br />    i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> begin;<br />    j </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);">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);">1</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);">(array[</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);">&lt;</span><span style="color: rgb(0, 0, 0);">pivot) ;<br />        </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(array[</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);">&gt;</span><span style="color: rgb(0, 0, 0);">pivot) ;<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);">j)<br />          </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br /><br />       tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[j];<br />       array[j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[i];<br />       array[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tmp;<br />    }<br /><br />    tmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[i];<br />    array[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> array[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 />    array[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);">] </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);">return</span><span style="color: rgb(0, 0, 0);"> i;<br />}<br /><br /></span></div><br /><br /><br /><br /><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/90079.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-14 21:58 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/14/90079.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>［备忘］封装printf</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/12/89890.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sun, 12 Jul 2009 13:07:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/12/89890.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89890.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/12/89890.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89890.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89890.html</trackback:ping><description><![CDATA[写一个函数封装printf以trace<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, 255);">void</span><span style="color: rgb(0, 0, 0);"> my_trace(</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);">cmd,<img src="http://www.cppblog.com/images/dot.gif" />)<br />{<br />    va_list args;<br /><br />    va_start(args,cmd);<br />    vprintf(cmd,args); //必须用vprintf<br />    va_end(args);<br />}<br /></span></div><br /><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89890.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 21:07 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/12/89890.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>SRM 444</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/08/89565.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Wed, 08 Jul 2009 13:29:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/08/89565.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89565.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/08/89565.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89565.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89565.html</trackback:ping><description><![CDATA[又在第一题上花了很多时间，这次更夸张。。用了20多分钟。。。最后提交时只有150分了。。<br />第二题，一看挺简单，就是求质因数的个数，然后看能被4的几次幂整除。<br />一开始匆忙写了一个，然后提交，400分。<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);">              </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getLevel(</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> N) <br />                  { <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);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</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);">;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);">&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);">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);">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 />                                         res</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />                                         N</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);"> r</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);">(res</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);">0</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);">4</span><span style="color: rgb(0, 0, 0);">;<br />                                  r</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);">return</span><span style="color: rgb(0, 0, 0);"> r;<br />                  } </span></div>后来一看，i*i&lt;=N这时，N一直在变，应该保存下N或者直接求sqrt(N)。一开始不太确定sqrt能否处理long long，因此用相乘的方法。后来cha人的时候，发现一个也犯了同样的错误，马上构造56=2*2*2*7,cha掉<br />于是改成下面，提交，只有299分了。。。<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);">              </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getLevel(</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> N) <br />                  { <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);">long</span><span style="color: rgb(0, 0, 0);"> </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);"> N; </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">增加这一行            </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">                  </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);">long</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</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);">;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);">&lt;=</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);">i){                          <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);">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 />                                         res</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />                                         N</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);"> r</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);">(res</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);">0</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);">4</span><span style="color: rgb(0, 0, 0);">;<br />                                  r</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);">return</span><span style="color: rgb(0, 0, 0);"> r;<br />                  } </span></div><br />结果还是被cha掉了。因为犯了一个严重的错误，没有算上最后一个质因数，这个质因数可能大于sqrt(N)。如88==2*2*2*11。<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);">              </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getLevel(</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> N) <br />                  { <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);">long</span><span style="color: rgb(0, 0, 0);"> </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);"> N;          <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);">long</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</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);">;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);">&lt;=</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);">i){                          <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);">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 />                                         res</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />                                         N</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);">if</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);">1</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, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">增加这一行<br />                                  </span><span style="color: rgb(0, 128, 0);"><br /></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);"> r</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);">(res</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);">0</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);">4</span><span style="color: rgb(0, 0, 0);">;<br />                                  r</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);">return</span><span style="color: rgb(0, 0, 0);"> r;<br />                  } </span></div><br />最后房间只有一人过了第二题。<br />还好cha了4个，不然rating要跌不少了。。<br />还好rating还是涨了一点点。。<br />太粗心了，不然就应该能进div1了。。<br /><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89565.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 21:29 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/08/89565.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 Shopping Offers</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/07/89483.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Tue, 07 Jul 2009 11:16:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/07/89483.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89483.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/07/89483.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89483.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89483.html</trackback:ping><description><![CDATA[背包问题，dp解决。<br />dp[i][j][k][m][n]表示第0-4种物品分别购买i-n个时，所需的最小费用。<br />那么对于每一个offer<br />dp[i][j][k][m][n] = min ( dp[i][j][k][m][n], dp[i-o[0]][j-o[1]][k-o[2]][m-o[3]][n-o[4]]+offer.cost);<br /><br />analysis中提出用最短路径的方法来解，思路很巧妙。把”装有不同种数和数量的物品“的篮子看作结点，把offer作为边(把购买一件物品的原始价格看成一种退化的offer)，把价格作为边长度。这样就转换成从篮子(0,0,0,0,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);">shopping.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);">shopping.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);">6</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);">6</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);">6</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);"> offer{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pro_num;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> price;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> product[</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);"> amount[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />};<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> offer_num;<br />offer offers[</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);"> map[</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">]; //product id到”按物品出现顺序所给的编号“的映射<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> price[</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">];//product id对应的物品价格<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> product_num;//物品总数目<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> products[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];//存放product id<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> amount[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];//product 所需的数量<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);">offer_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);">offer_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);">offers[i].pro_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);"> 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);">offers[i].pro_num;</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);">offers[i].product[j]</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].amount[j];<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);">offers[i].price;<br />    }<br />    <br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pro_idx </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);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">product_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);">product_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);">products[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);">amount[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);">price[i];<br />        map[ products[i] ] </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);">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);">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);">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);">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);">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);"> 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);">;m</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);">m)<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);"> n</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);">;n</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);">n){<br />                        dp[i][j][k][m][n] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> <br />                            price[</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);">i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</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</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</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);">k</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</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);">m</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</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);">n;<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);">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);">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);">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);">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);">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);"> 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);">;m</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);">m)<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);"> n</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);">;n</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);">n){<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);">5</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);"> s</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);">;s</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offer_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">s){<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 />                            </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);"> t</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);">;t</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offers[s].pro_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">t){<br />                                tmp[map[offers[s].product[t]]] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> offers[s].amount[t];<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);">&gt;=</span><span style="color: rgb(0, 0, 0);">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, 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);">tmp[</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);">k</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</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);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</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);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]){<br />                                dp[i][j][k][m][n] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> min( dp[i][j][k][m][n],<br />                                        dp[i</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);">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);">tmp[</span><span style="color: rgb(0, 0, 0);">1</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);">tmp[</span><span style="color: rgb(0, 0, 0);">2</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);">tmp[</span><span style="color: rgb(0, 0, 0);">3</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);">tmp[</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 />                                        offers[s].price);<br />                            }<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);">dp[amount[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]][amount[</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);">&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 />上面代码有个严重的bug，谢谢网友“我也凑热闹"指出。由于map所有值都为0。所以未在商品列表中出现的商品的map值都为0，即都映射为第一个商品。现改成将map初始化为-1，并增加判断语句。此外将初始化dp的语句合并到后面，以简化代码。<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);">shopping.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);">shopping.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);">6</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);">6</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);">6</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);"> offer{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pro_num;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> price;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> product[</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);"> amount[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />};<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> offer_num;<br />offer offers[</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);"> map[</span><span style="color: rgb(0, 0, 0);">1000</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);"> price[</span><span style="color: rgb(0, 0, 0);">1000</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);"> product_num;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> products[</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);"> amount[</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);"> 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);">offer_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);">offer_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);">offers[i].pro_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);"> 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);">offers[i].pro_num;</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);">offers[i].product[j]</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].amount[j];<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);">offers[i].price;<br />    }<br />    <br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pro_idx </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);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">product_num;<br /><br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">2009.07.27修改</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    memset(map,</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);">(map));<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);">product_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);">products[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);">amount[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);">price[i];<br />        map[ products[i] ] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> 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);">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);">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);">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);">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);">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);"> 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);">;m</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);">m)<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);"> n</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);">;n</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);">n){<br /><br />                         dp[i][j][k][m][n] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> <br />                            price[</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);">i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</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</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</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);">k</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</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);">m</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</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);">n;<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);">5</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);"> s</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);">;s</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offer_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">s){<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 />                            </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);"> t</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);">;t</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offers[s].pro_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">t){<br />                                </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">2009.07.27修改</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">                                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( map[offers[s].product[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 />                                    tmp[map[offers[s].product[t]]] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> offers[s].amount[t];<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);">&gt;=</span><span style="color: rgb(0, 0, 0);">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, 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);">tmp[</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);">k</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</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);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</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);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]){<br />                                dp[i][j][k][m][n] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> min( dp[i][j][k][m][n],<br />                                        dp[i</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);">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);">tmp[</span><span style="color: rgb(0, 0, 0);">1</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);">tmp[</span><span style="color: rgb(0, 0, 0);">2</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);">tmp[</span><span style="color: rgb(0, 0, 0);">3</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);">tmp[</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 />                                        offers[s].price);<br />                            }<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);">dp[amount[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]][amount[</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);">&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 /><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89483.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 19:16 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/07/89483.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>［问题解决备忘］ping不通localhost</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/06/89406.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Mon, 06 Jul 2009 13:30:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/06/89406.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89406.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/06/89406.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89406.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89406.html</trackback:ping><description><![CDATA[今天写一个socket小程序，测试一下，居然连不上去。然后ping localhost发现ping不通，但是能上外网。<br />确实很奇怪，搜了下，后来在ubuntu论坛上发现，才想起是我的/etc/network/interfaces文件配置问题。<br />以前在手动改这个文件的时候，顺手把lo接口给删了。。。<br />难怪启动的时候x server一直报错，添加上lo接口，只/etc/init.d/networking restart还是不行的，重启马上就解决了。<br />记录一下，备忘。<br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89406.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 21:30 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/06/89406.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>2</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></channel></rss>