Greenmoon55's Blog 2017-06-09T03:45:44+00:00 http://greenmooon55.com greenmoon55 LeetCode 44 Wildcard Matching 2017-06-09T00:00:00+00:00 http://greenmooon55.com/leetcode-44-wildcard-matching <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Solution</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">isMatch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span> <span class="s">""" :type s: str :type p: str :rtype: bool """</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="k">for</span> <span class="n">ch</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span> <span class="k">if</span> <span class="n">ch</span> <span class="o">!=</span> <span class="s">'*'</span><span class="p">:</span> <span class="k">return</span> <span class="bp">False</span> <span class="k">return</span> <span class="bp">True</span> <span class="n">f</span> <span class="o">=</span> <span class="p">[[</span><span class="bp">False</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)]</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span> <span class="k">if</span> <span class="n">p</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="ow">or</span> <span class="n">p</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s">'?'</span><span class="p">:</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="k">elif</span> <span class="n">p</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s">'*'</span><span class="p">:</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span> <span class="k">if</span> <span class="n">f</span><span class="p">[</span><span class="n">k</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span> <span class="k">break</span> <span class="c">#print f</span> <span class="k">return</span> <span class="n">f</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)][</span><span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="p">)]</span> </code></pre> </div> <p>第一个思路是<code class="highlighter-rouge">f[i][j]</code>表示<code class="highlighter-rouge">s[i]</code>匹配到<code class="highlighter-rouge">p[j]</code>能否实现,两重循环计算,遇到*还要再加一层循环来确定p前一位匹配到了哪里。提交之后TLE。</p> <p>可以发现有两个可以优化的地方,第一个是<code class="highlighter-rouge">f[i][j]</code>第二维只需要j-1,因此可以降成一维数组。第二个是当p[j]是*时,任意<code class="highlighter-rouge">f[k][j]</code>(k&lt;=i)为True都可以,不需要每个i都向前搜索一遍,而应该遇到<code class="highlighter-rouge">f[k][j]</code>反过来填后面的<code class="highlighter-rouge">f[i][j]</code>(i&gt;=k)。</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Solution</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">isMatch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span> <span class="s">""" :type s: str :type p: str :rtype: bool """</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="k">for</span> <span class="n">ch</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span> <span class="k">if</span> <span class="n">ch</span> <span class="o">!=</span> <span class="s">'*'</span><span class="p">:</span> <span class="k">return</span> <span class="bp">False</span> <span class="k">return</span> <span class="bp">True</span> <span class="n">f</span> <span class="o">=</span> <span class="p">[</span><span class="bp">False</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span> <span class="k">for</span> <span class="n">ch</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span> <span class="k">if</span> <span class="n">ch</span> <span class="o">!=</span> <span class="s">'*'</span><span class="p">:</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span> <span class="k">if</span> <span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">ch</span> <span class="ow">or</span> <span class="n">ch</span> <span class="o">==</span> <span class="s">'?'</span><span class="p">:</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="k">else</span><span class="p">:</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="bp">False</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="bp">False</span> <span class="k">else</span><span class="p">:</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)):</span> <span class="k">if</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span> <span class="n">f</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span> <span class="k">break</span> <span class="k">return</span> <span class="n">f</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)]</span> </code></pre> </div> <p>此时f[i]表示截止到当前ch,能否匹配到第i位。</p> Codeforces Round #418 (Div. 2) 2017-06-08T00:00:00+00:00 http://greenmooon55.com/codeforces-round-418-div-2 <p>又是发挥很差的一场,只过了A..</p> <h1 id="a-an-abandoned-sentiment-from-past">A. An abandoned sentiment from past</h1> <p>逆序填入数字再判断是否还是递增的序列</p> <h1 id="b-an-express-train-to-reveries">B. An express train to reveries</h1> <p>这道题看了样例想了想就写了个算法,居然还过了pretest,最后WA了。说明要认真想算法的正确性。。</p> <p>分两种情况讨论:</p> <ol> <li>a, b只有一个数字不同,那么说明a[i]和b[i]都不可能是要找的数字,找一找剩下n-1个数中未出现的数字即可。</li> <li>a, b在第i位和第j位不同</li> </ol> <p>如果是</p> <div class="highlighter-rouge"><pre class="highlight"><code>1 2 3 4 3 1 2 5 4 5 </code></pre> </div> <p>那么i位和j位选哪个都无所谓。</p> <p>如果是</p> <div class="highlighter-rouge"><pre class="highlight"><code>5 4 5 3 1 4 4 2 3 1 </code></pre> </div> <p>就要先去掉出现两次的数,例如上面第3位先选b,因为2只出现了一次,接下来第一位选5,因为a中第三位的5已经被替换成2了,5在a中只出现1次。</p> <p>再举个例子</p> <div class="highlighter-rouge"><pre class="highlight"><code>5 4 3 5 2 5 4 1 1 2 </code></pre> </div> <p>答案是:</p> <div class="highlighter-rouge"><pre class="highlight"><code>5 4 3 1 2 </code></pre> </div> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">input</span><span class="p">())</span> <span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">input</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span> <span class="n">b</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">input</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span> <span class="n">x</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">a_used</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span> <span class="n">b_used</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)):</span> <span class="n">a_used</span><span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">+=</span> <span class="mi">1</span> <span class="n">b_used</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">+=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span> <span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span> <span class="n">x_done</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">while</span> <span class="n">x_done</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)):</span> <span class="k">if</span> <span class="n">a_used</span><span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">b_used</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="n">x_done</span> <span class="o">+=</span> <span class="mi">1</span> <span class="n">a_used</span><span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span> <span class="o">-=</span> <span class="mi">1</span> <span class="n">a</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="n">b</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="k">elif</span> <span class="n">a_used</span><span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">b_used</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span> <span class="n">x_done</span> <span class="o">+=</span> <span class="mi">1</span> <span class="n">b_used</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span> <span class="o">-=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">x_done</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="n">a</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">b</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="k">break</span> <span class="k">else</span><span class="p">:</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span> <span class="k">if</span> <span class="n">a_used</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="n">a</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">i</span> <span class="k">break</span> <span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">end</span><span class="o">=</span><span class="s">""</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)):</span> <span class="k">print</span><span class="p">(</span><span class="s">" "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]),</span> <span class="n">end</span><span class="o">=</span><span class="s">""</span><span class="p">)</span> </code></pre> </div> <h1 id="c-an-impassioned-circulation-of-affection">C. An impassioned circulation of affection</h1> <p>当时把自己带到沟里了,想着先split,然后就卡在“aaaaaa”这种情况。今天发现只要暴力扫一遍就好,不需要split啥的。。注意需要先算好所有答案,因为q太大了。 卡python也是无语,只好再用c++写一下。我这里循环用滑动窗口来做,j先确定当前串的长度,另一种做法是直接两重循环串的起点和终点,两种做法都是O(n^2),没有区别。官方题解还提到了有复杂度更低的算法,目前没有动力去看。。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code><span class="cp">#include &lt;iostream&gt; #include &lt;string&gt; </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="n">f</span><span class="p">[</span><span class="mi">26</span><span class="p">][</span><span class="mi">1510</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="kt">char</span> <span class="n">c</span><span class="p">;</span> <span class="n">string</span> <span class="n">s</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">s</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">26</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">char</span> <span class="n">ch</span> <span class="o">=</span> <span class="sc">'a'</span> <span class="o">+</span> <span class="n">i</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">l</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">r</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="n">r</span><span class="p">]</span> <span class="o">!=</span> <span class="n">ch</span><span class="p">)</span> <span class="p">{</span> <span class="n">count</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="n">count</span> <span class="o">&gt;</span> <span class="n">j</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="n">l</span><span class="p">]</span> <span class="o">!=</span> <span class="n">ch</span><span class="p">)</span> <span class="p">{</span> <span class="n">count</span><span class="o">--</span><span class="p">;</span> <span class="p">}</span> <span class="n">l</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">],</span> <span class="n">r</span><span class="o">-</span><span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span> <span class="n">r</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">q</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">q</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">m</span> <span class="o">&gt;&gt;</span> <span class="n">c</span><span class="p">;</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">f</span><span class="p">[</span><span class="kt">int</span><span class="p">(</span><span class="n">c</span><span class="o">-</span><span class="sc">'a'</span><span class="p">)][</span><span class="n">m</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="n">q</span><span class="o">--</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> LeetCode 600 Non-negative Integers without Consecutive Ones 2017-06-07T00:00:00+00:00 http://greenmooon55.com/leetcode-600-non-negative-integers-without-consecutive-ones <p>第一次ak哈哈,可能是因为这周的比赛比较简单。</p> <p>先说第一个方法,也是我当时想出来的。分情况讨论,一种是二进制表示的长度小于n的长度,另一种是跟n长度相同。第一种情况任意不含11的数都符合条件,第二种要搜索判断。</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Solution</span><span class="p">:</span> <span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">size</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span> <span class="k">return</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span> <span class="c"># top: whether current value is the same as a</span> <span class="k">def</span> <span class="nf">countLimit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">top</span><span class="p">):</span> <span class="k">if</span> <span class="n">top</span> <span class="ow">and</span> <span class="n">num</span> <span class="o">&gt;</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span> <span class="k">return</span> <span class="mi">0</span> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">size</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span> <span class="k">return</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">num</span> <span class="o">&lt;</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span> <span class="n">top</span> <span class="o">=</span> <span class="bp">False</span> <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="k">if</span> <span class="n">top</span> <span class="o">==</span> <span class="bp">False</span><span class="p">:</span> <span class="c"># we can choose whatever we want</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="nb">all</span><span class="p">[</span><span class="n">size</span><span class="o">-</span><span class="n">i</span><span class="p">]</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">countLimit</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">top</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">countLimit</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">top</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">countLimit</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">top</span><span class="p">)</span> <span class="k">def</span> <span class="nf">findIntegers</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="p">):</span> <span class="s">""" :type num: int :rtype: int """</span> <span class="c"># possible solutions starting with 1</span> <span class="bp">self</span><span class="o">.</span><span class="nb">all</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">31</span> <span class="bp">self</span><span class="o">.</span><span class="nb">all</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="bp">self</span><span class="o">.</span><span class="nb">all</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">31</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="nb">all</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="nb">all</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="nb">all</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="n">a</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">while</span> <span class="n">num</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span> <span class="n">a</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">num</span> <span class="o">%</span> <span class="mi">2</span><span class="p">)</span> <span class="n">num</span> <span class="o">=</span> <span class="n">num</span> <span class="o">//</span> <span class="mi">2</span> <span class="n">a</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span> <span class="n">length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c">#print(a)</span> <span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">countLimit</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span> <span class="c">#print(res)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">length</span><span class="p">):</span> <span class="n">res</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="nb">all</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="c"># add 0 to solutions</span> <span class="k">return</span> <span class="n">res</span> <span class="o">+</span> <span class="mi">1</span> </code></pre> </div> <p>all数组表示1开头符合条件的数的数量,本来是通过count函数来搜索的,后来发现是斐波那契数列就直接算了。countLimit用来搜索长度与n相同的数,top用来标记当前位是否与n一样大,如果当前第i位是1并且已经不是最大的,那就说明从第i位开始就可以随意选1和0了,加了这个剪枝就过了。</p> <p>下面是官方题解的思路,另外参考<a href="http://blog.csdn.net/u014688145/article/details/72812515">这个解释</a>。 这个f[i]包含以0开头的情况,所以如果发现n的第i位是1,那么当前位是0,后面就可以任意选数字了。如果遇到连续的1,选完第i位是0,就可以退出了,因为此时第i位只能为0。最后返回时加上n本身(如果遇到11要限减掉,因为不可能出现n这个数)。</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Solution</span><span class="p">:</span> <span class="k">def</span> <span class="nf">findIntegers</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="p">):</span> <span class="s">""" :type num: int :rtype: int """</span> <span class="n">a</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">while</span> <span class="n">num</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span> <span class="n">a</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">num</span> <span class="o">%</span> <span class="mi">2</span><span class="p">)</span> <span class="n">num</span> <span class="o">=</span> <span class="n">num</span> <span class="o">//</span> <span class="mi">2</span> <span class="n">a</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span> <span class="n">length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c"># number of available solutions, i is the length of binary representations</span> <span class="c"># f[1]: 0, 1; f[2]: 00, 10, 01</span> <span class="n">f</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">length</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="n">f</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">f</span><span class="p">)):</span> <span class="c"># f[i] = (f[i-1] + '0') + (f[i-2] + '01')</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="n">res</span> <span class="o">=</span> <span class="mi">0</span> <span class="c">#print(length,res)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">length</span><span class="p">):</span> <span class="k">if</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="n">l</span> <span class="o">=</span> <span class="n">length</span> <span class="o">-</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span> <span class="n">res</span> <span class="o">+=</span> <span class="n">f</span><span class="p">[</span><span class="n">l</span><span class="p">]</span> <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="c"># num itself is invalid and will be deleted later</span> <span class="n">res</span> <span class="o">-=</span> <span class="mi">1</span> <span class="c"># res was updated in previous i</span> <span class="k">break</span> <span class="c"># plus num itself</span> <span class="k">return</span> <span class="n">res</span><span class="o">+</span><span class="mi">1</span> </code></pre> </div> 我的2016 2016-12-31T00:00:00+00:00 http://greenmooon55.com/2016-year-in-review <p>2016年发生了很多事情,年前一二月在公司忙重构,四月决定出国,六月离职,七八月复习GRE,后面在学校申请+看看论文。</p> <p>今年做的最重要决定肯定是离职准备申请master,可能在别人看来是比较冲动的决定,这次也就上网搜了一圈资料就决定了,其实本科的时候有过一点点想法,只是后来完全放弃了。</p> <p>不记得在哪里看到一篇文章,不管从事什么高薪工作,多年后很多人还是会选择做自己最想做的事情,过自己想要的生活方式。初中的时候有次跟同学聊天,讲以后想做什么,我就想做一个平凡的人,白天做有意义的工作,空闲时间可以读读书,这就是最理想的人生了,希望以后能实现这个目标。(有意义 = 影响的人数 * 贡献度,比如让电脑开机耗时-1s,如果影响的人多,也是一件有意义的工作。)接下来读研希望也有时间见识更大的世界,思考人生目标。</p> <p>第一次回到软院很想哭,很喜欢最近在嘉定的生活。申请季到现在还比较顺利,最紧张的时候是八月初担心GRE考不出来和十一月底担心文书写不好,等明年结果都出来再好好总结一下吧。完全没想到最近这半年会这么忙,近三个月的节奏是这样的:每周实验室开会之后先自己看看公开课放松一两天,然后再花两三天做本周的主要任务,比如复习托福或者网申几所学校,最后开会前一两天再去看论文或者折腾代码,这一两天才是每周最紧张的时候,所以其实每次去考托福或者GRE我都非常放松。。</p> <p>今年公开课看了宾大的《城市规划》,感觉好多设计很有特色,很强调构建一个walkable neighbourhood,然而真的跟中国国情差距太大,人太多了= = 第一次体验peer review的公开课,大家做presentation都挺认真的。专业相关的就是Andrew Ng的Machine Learning,Geoffrey Hinton的神经网络,再加上Udacity的自动驾驶,都各有侧重,明年学完再来总结。</p> <p>虽然不是学霸,对于我来说,学习带来的快感要比上班写代码爽得多,可能因为码农不能决定产品方向?(其实还算幸运一直在做还算感兴趣的东西,没有遇到过新浪微博那种脑残PM。)挺喜欢读paper的,申请季也在考虑以后是做科研还是工作,工作的话要是去不了好玩的组,就算在FLAG又如何,科研我不明确做什么方向,也不知道自己是否天赋、智商足够,要知道科研选错方向可比在公司进错组惨多了。(推荐这篇研究生后出国去Google的思考《<a href="https://laike9m.com/blog/shi-shi-hou-liao,94/">是时候了</a>》,我也比较看重技术氛围和国内环境)</p> <p>最近在一亩三分地上看到两篇讨论这类问题的帖子,一篇是<a href="http://www.1point3acres.com/bbs/thread-216592-1-1.html">top5硕士毕业工作三年后想做科研</a>,另外一篇是<a href="http://www.1point3acres.com/bbs/thread-217580-1-1.html">想做CV但是master读了一年之后有些迷茫</a>,第二篇很可能就是明年的我。我发现性格因素其实也挺重要,上大学测过自己是INTJ,这两个帖子的作者也是INTJ/INTP,我昨天测了下还是INTJ,I最明显,NTJ其实倾向不大,或许这种测试还是有点道理的?INTJ也就是内倾、直觉、理性、判断。内向是肯定的,我是属于充电(跟人聊天)五分钟可以放电(一个人呆着)很久的,一直充电就会爆炸,大一对门的时候就不理结果为啥希望寝室没有人。还有一点是强烈兴趣驱动,完全无法忍受做自己不想做的事情,比如当年不保研的原因之一是不想再上政治课,现在申请也不想选MSIM甚至MSIN这种课程表不完美的项目。追求完美?可能是有点吧。。我也越来越不在意别人的想法了。“INTJ是十六种人格类型中最稀有的类型,约占女性人口比例0.5%,男性人口比例1%”,来自<a href="https://zh.wikipedia.org/zh-hans/INTJ">维基百科</a>,真是可怜。</p> <p>今年的主要娱乐是看youtube的游戏视频…F1(播主是工科生)、模拟卡车(播主居然是cs毕业的大叔,有次还聊他喜欢操作系统讨厌数学课。。),还有bilibili的视频吧。。书几乎没看,kindle已经积灰很久了。每天晚上从十一点多躺一两个小时睡不着也是醉醉的。豆瓣告诉我今年电影居然看得比去年少,额是更宅了还是更忙了?</p> <p>申请季仅仅是个开始,不管找工还是科研,后面的压力会更大。新年计划就不写了,听说不告诉别人计划会更好?越长大越想家,也希望能多回家看看。</p> <p>新年快乐!</p> <p>最后放一张调过色的嘉定。</p> <p><img src="/images/jiading.png" alt="" /></p> Coursera Machine Learning 2016-11-18T00:00:00+00:00 http://greenmooon55.com/coursera-machine-learning <p>这门课也是本科的时候尝试然后放弃的课,今年五月重新捡了起来还买了证书,夏天复习GRE没看,赶在可以获得证书的最后几天(要半年内学完)终于学完了。</p> <p>这门课是 <a href="http://cs229.stanford.edu/">CS229</a> 的 <a href="https://www.quora.com/What-are-the-differences-between-the-Andrew-Ngs-Machine-Learning-courses-offered-on-Coursera-and-iTunes-U?srid=bMF">“watered down”版本</a>。 剔除了几乎所有需要数学知识的部分,只需要知道如何求导以及矩阵乘法。编程作业里的所有数学公式都给出来了,只需要在MATLAB里实现就可以了。这门课很奇葩的一点是入门比较难…后面越来越简单。</p> <p>总结一下这门课都讲了啥?</p> <ol> <li> <p>线性回归,cost function, gradient descent, vectorized implementation</p> </li> <li> <p>Feature scaling, mean normalization, normal equation(但是数据量大的时候很慢), regularization</p> </li> <li> <p>Logistic Regression(用到sigmoid function和取对数的cost function), Advanced optimization, one-vs-all</p> </li> <li> <p>Neural Networks: Forward Propagation, bias unit, Backpropagation(可以用gradient checking确定backpropagation算出的导数是否正确), Random initialization: Symmetry breaking</p> </li> <li> <p>Training/validation/test sets</p> <p><img src="/images/ml-bias-variance.png" alt="" /> <img src="/images/ml-learning-curve.png" alt="" /></p> </li> <li> <p>Spam classification, Precision/Recall, F Score</p> </li> <li> <p>SVM:修改Logistic Regression的model,优化theta即可获得比较好的结果。</p> <p>参考:<a href="http://blog.pluskid.org/?p=632">支持向量机: Maximum Margin Classifier</a></p> <p>Kernel: 生成feature的好方法,可以选择数据作为landmarks,选择基于到landmark的距离的similarity methods来生成新的数据。</p> <p><img src="/images/ml-logistic-regression-svm.png" alt="" /></p> </li> <li> <p>KMeans</p> </li> <li> <p>Demension Reduction: PCA(SVD分解)(Bad use: prevent overfitting)</p> </li> <li> <p>Anomaly Detection,这里用的是高斯分布,判断数据点是否离均值太远</p> <p><img src="/images/ml-anomaly-detection.png" alt="" /></p> </li> <li> <p>Collabrative Filtering</p> <p><img src="/images/ml-collabrative-filtering.png" alt="" /></p> </li> <li> <p>Stochastic gradient descent(random shuffle first, can slowly decrease alpha), mini-batch gradient descent, MapReduce</p> </li> <li> <p>Pipline, Ceiling analysis</p> </li> </ol> <p>嗯只是给我自己看的….有人写了很详细的笔记,强烈推荐:<a href="http://www.holehouse.org/mlclass/index.html">http://www.holehouse.org/mlclass/index.html</a></p> <p>接下来我现在在看Hilton的Neural Network,很详细地讲神经网络,上周学了CNN,这周讲RNN。等明年有空去搞搞Kaggle,学完不动手就全忘了。或许看下CS229或者231n吧…</p> 回到嘉定 2016-07-06T00:00:00+00:00 http://greenmooon55.com/back-to-jiading <p>像我这么不敏感的人,并没有什么感觉,一进图书馆开始看书就把自己当做准备期末考的大二学生了,没有任何违和感。然而这都是考试周的第二周了,图书管理依旧都是大二电气的复习考试呢。。</p> <p>倒是睡前刷邮箱刷到LinkedIn,看到好几个大一学CS的同学,现在都挺不错的,去了好公司or去了好学校,突然发现真的过了好多年了。</p> <p><a href="https://www.shanbay.com/weekly/article/1051/">笑来寄语:人生是一场长跑,但你想明白了么?</a></p> 猴年快乐 2016-02-09T00:00:00+00:00 http://greenmooon55.com/160209 <p>又是很匆忙的一年,除夕下午出去买东西,头一次看到没人的河南街。。回到家晚会都开始了,都没来得及贴春联。</p> <p>想想 2015 也发生了很多,首先是换了个工作,当时想着扇贝的一句话 I know it’s going to be very busy, but I’m ready to welcome that with open arms. 结果到了 2016 才是真忙,一月份忙到都没有生活了= =</p> <p>技术上么,终于有了MBP,系统是挺好用的,不过在我手上经常五国- - 开始用了 PyCharm,终于开始 coding 了,有机会开发新功能&amp;重构感觉很爽的。不过一直没机会折腾 docker 相关的东西,今年要搞起来咯,还要看看旧代码,多向同事学东西~ 其实现在并发量还没上来,以后有更多挑战。</p> <p>入了 steam 大坑,买了好多游戏,玩得多的是不用动脑的卡车模拟和Mini Metro,周末就想在家宅着好好休息打游戏= =</p> <p>终于从闵行回到了大杨浦^_^。尽管上半年去了西安,后面去了两次杭州,参加了一次 8km 跑步比赛,不过后面感觉更宅了,健身一点都没搞了,Keep 也没做,口琴半年木有拿出来了。。Todoist 和扇贝倒是坚持下来啦。</p> <p>新年希望自己更开心咯,要多花时间想想计划了。</p> Raspberry Pi 上手 2015-11-19T00:00:00+00:00 http://greenmooon55.com/hands-on-with-raspberry-pi <p>从公司拿来一台树莓派折腾玩,下面记录一下折腾的的过程吧。</p> <h1 id="买-sd-卡装系统">买 SD 卡装系统</h1> <p>京东买了个 <a href="http://item.jd.com/1875996.html">Sandisk 16G卡</a>,插在读卡器上就可以用了。官网下载 <a href="https://www.raspberrypi.org/downloads/raspbian/">RASPBIAN JESSIE</a>, 装好之后懒得接显示器,nmap 查一下 ip, ssh pi@<ip>,密码是 raspberry。配了 192.168.1.233 这个静态 ip,这样以后就可以直接连了233。</ip></p> <h1 id="连接显示器">连接显示器</h1> <p>HDMI连上我的 Dell U2312HM 显示器,居然没反应。。还好搜到<a href="http://www.chriszh.com/?p=562">解决方法</a></p> <blockquote> <p>在网上找了一下,说是将/boot/config.txt的HDMI_SAFE的注释删掉就好了,于是照做。发现显示器能亮了,但是只有640*480的分辨率。再经过若干折腾,发现将HDMI_SAFE还是注掉,将hdmi_force_hotplug=1打开就可以完美1080p了。不过用hdmi接电视的时候需要再关上。</p> </blockquote> <h1 id="usb网卡">USB网卡</h1> <p>开始折腾的时间到了。。买了个<a href="http://item.jd.com/123836.html">TP-LINK TL-WN821N 300M无线USB网卡</a>,插上去之后 lsusb 没识别。</p> <div class="highlighter-rouge"><pre class="highlight"><code>pi@raspberrypi ~ $ lsusb Bus 001 Device 004: ID 0bda:818b Realtek Semiconductor Corp. Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub </code></pre> </div> <p>能搜到需要 rtl8192eu 驱动,谢天谢地有人编译好了,从<a href="http://tieba.baidu.com/p/4161599392">百度贴吧</a>找到的链接: https://www.raspberrypi.org/forums/viewtopic.php?f=28&amp;t=124509&amp;p=835823&amp;hilit=8192eu#p835823</p> <p>根据规则从 Dropbox 下载就好了,好幸福。。就怕他以后不更新了,不敢升级系统了。。</p> <h1 id="路由器">路由器</h1> <p>参考这篇文章把树莓派搞成AP吧:</p> <p>http://jacobsalmela.com/raspberry-pi-and-routing-turning-a-pi-into-a-router/</p> <p>注意 hostapd 不能用 debian 自带的,需要自己编译,我直接用的 master 分支,参考:http://www.jenssegers.be/43/realtek-rtl8188-based-access-point-on-raspberry-pi</p> <p>hostapd.conf</p> <div class="highlighter-rouge"><pre class="highlight"><code># Basic configuration interface=wlan0 ssid=RPiAP channel=7 #bridge=br0 # WPA and WPA2 configuration macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=12345678 wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP # Hardware configuration driver=rtl871xdrv ieee80211n=1 hw_mode=g device_name=RTL8192CU manufacturer=Realtek </code></pre> </div> <div class="highlighter-rouge"><pre class="highlight"><code>pi@raspberrypi ~ $ cat /etc/dnsmasq.conf | grep -v "#" | sed '/^$/d' domain-needed interface=wlan0 listen-address=127.0.0.1,192.168.2.1 dhcp-range=192.168.2.20,192.168.2.200,12h </code></pre> </div> <div class="highlighter-rouge"><pre class="highlight"><code>pi@raspberrypi ~ $ cat /etc/network/interfaces # Please note that this file is written to be used with dhcpcd. # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'. auto lo iface lo inet loopback auto eth0 allow-hotplug eth0 #iface eth0 inet manual iface eth0 inet static address 192.168.1.233 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255 gateway 192.168.1.1 allow-hotplug wlan0 iface wlan0 inet static address 192.168.2.1 netmask 255.255.255.0 network 192.168.2.0 gateway 192.168.2.1 </code></pre> </div> <p>iptables 配置参考这篇文章:http://liberize.me/tech/turn-raspberry-pi-into-a-router.html</p> <div class="highlighter-rouge"><pre class="highlight"><code>sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT </code></pre> </div> <p>Done! Have fun!</p> Nova scheduler 2015-07-25T00:00:00+00:00 http://greenmooon55.com/nova-scheduler <p>Nova部署、迁移VM都会用到scheduler来调度。本文内容基于Icehouse。</p> <p>#Filters and Weights</p> <p><img src="http://greenmoon55.com/images/scheduler.png" alt="Filters and Weights" title="Filters and Weights" /></p> <p>Filters会过滤不符合条件的宿主机,然后经过Weights获得权重,最终选择权重最大的host。</p> <p>常用filter有RetryFilter,AvailabilityZoneFilter,RamFilter,ComputeFilter,ComputeCapabilitiesFilter,ImagePropertiesFilter。</p> <p>默认启用的weighter只有RAMWeigher。。会返回host的可用内存,最终选取可用内存最大的host。</p> <p>另外还有个MetricsWeigher,可以自定义各项metric的重要性,不过默认没有启用。</p> <p>#Resource tracker</p> <p>那么这些权重怎么获取呢?</p> <p>nova/compute/manager.py 有个periodic task update_available_resource,每分钟会更新一次host的资源使用情况。最终会调用到resource tracker里的_sync compute node,更新compute_nodes表。scheduler里的HostManager再从数据库里获取metrics。</p> <p>scheduler本身不会更新host metric,只会在filter_scheduler _provision_resource时更新instance的host, node及schedule_at。</p> <p>#是否有cache 没有,有个caching scheduler,可缓存all_host_states,默认没有启用。</p> <p>#retry如何实现?</p> <p>filter scheduler -&gt; def scheduler_run_instance -&gt; def _schedule -&gt; def _populate_retry</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="n">retry</span> <span class="o">=</span> <span class="p">{</span> <span class="s">'num_attempts'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'hosts'</span><span class="p">:</span> <span class="p">[]</span> <span class="c"># list of compute hosts tried</span> <span class="p">}</span> <span class="n">filter_properties</span><span class="p">[</span><span class="s">'retry'</span><span class="p">]</span> <span class="o">=</span> <span class="n">retry</span> </code></pre> </div> <p>compute/manager.py有_reschedule_or_error,如果失败会RPC调用scheduler。 另外有个RetryFilter,会过滤之前尝试(失败)过的host。</p> <p>相关资料:</p> <p><a href="http://www.openstack.cn/?p=3312">Nova Scheduler Intel 王庆</a></p> <p><a href="http://www.choudan.net/2013/08/11/Nova-Scheduler%E5%88%86%E6%9E%90.html">臭蛋 Nova Scheduler分析</a></p> <p><a href="https://www.mirantis.com/blog/nova-scheduler-database-interactions-how-to-nail-those-scalability-thwarters/">Mirantis: Nova Scheduler-Database Interactions: How to Nail Those Scalability Thwarters</a></p> <p>Update:</p> <p>最后补充个用 pyreverse 画的<a href="http://greenmoon55.com/images/scheduler_classes.png">类图</a></p> Coursera Cryptography 2015-06-22T00:00:00+00:00 http://greenmooon55.com/coursera-cryptography <p>大学的时候之前尝试过两次,都没跟下来,这次终于学完了。。</p> <p>密码学是什么?在学这门课之前,我只有这些印象:</p> <ol> <li>课程项目写用户注册的时候要 hash+salt</li> <li>Rails 会把 session 信息存在 cookie 里,为了防止被篡改,cookie 里还有个 HMAC,用到了secret key。<a href="http://stackoverflow.com/questions/13786320/rails-where-is-message-digest-of-sessions-using-default-cookiestore">link</a></li> <li>SSH keypair</li> </ol> <p>目标主要有两个:confidentiality and integrity。要保证confidentiality,就要选择各种加密方法,要保证integrity,就要用到MAC。 两种都保证了,就可以说实现了authenticated encryption。</p> <p>好吧,那接下来看看都讲了啥。。自己整理一下:</p> <p>第一周:Stream Cipher, One-time Pad, PRG</p> <p>第二周:Block Cipher, AES, DES, PRG, PRF, CBC, CTR</p> <p>第三周:Secure MAC, Birthday Attack, Merkle–Damgard iterated construction</p> <p>第四周:Authenticated Encryption, Encrypt-then-MAC, Padding Oracle, HKDF, PBKDF</p> <p>第五周:Diffie-Hellman protocol, Fermat, Euler, DLOG</p> <p>第六周:Trapdoor Functions, RSA, PKCS1 v1.5, PKCS1 v2.0: OAEP, EIGamal</p> <p><a href="http://naotu.baidu.com/viewshare.html?shareId=awh8z6rrpwk4">百度脑图</a></p> <p>现在感觉只是大概了解了这些概念和数学证明,习题做得太少,已经忘了一些了。。知道这些已经知足啦,期待Part 2:)</p> OpenStack Horizon Filter 功能的实现 2015-03-07T00:00:00+00:00 http://greenmooon55.com/horizon-filter-feature <p>怕自己以后忘了,随手记一下。</p> <p>下图是 admin dashboard 里的 filter,可根据 Project 和 Name 过滤 instance。</p> <p><img src="/images/horizon-filter-admin.png" alt="" /></p> <p>下图是 project dashboard 里的 filter,可以过滤表格里的任意一列</p> <p><img src="/images/horizon-filter-project.png" alt="" /></p> <p>代码以当前 stable/icehouse 分支为准</p> <p>首先从后端看起,<a href="https://github.com/openstack/horizon/blob/cfa611bedf14c9b1ef0b5689b6942aa6431e6313/horizon/tables/actions.py">horizon/tables/actions.py</a> 有 FilterAction class</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">FilterAction</span><span class="p">(</span><span class="n">BaseAction</span><span class="p">):</span> </code></pre> </div> <p><a href="https://github.com/openstack/horizon/blob/cfa611bedf14c9b1ef0b5689b6942aa6431e6313/openstack_dashboard/dashboards/project/instances/tables.py">openstack_dashboard/dashboards/project/instances/tables.py</a> 有</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">InstancesFilterAction</span><span class="p">(</span><span class="n">tables</span><span class="o">.</span><span class="n">FilterAction</span><span class="p">):</span> <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">instances</span><span class="p">,</span> <span class="n">filter_string</span><span class="p">):</span> <span class="s">"""Naive case-insensitive search."""</span> <span class="n">q</span> <span class="o">=</span> <span class="n">filter_string</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">return</span> <span class="p">[</span><span class="n">instance</span> <span class="k">for</span> <span class="n">instance</span> <span class="ow">in</span> <span class="n">instances</span> <span class="k">if</span> <span class="n">q</span> <span class="ow">in</span> <span class="n">instance</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span><span class="n">admin</span> </code></pre> </div> <p><a href="https://github.com/openstack/horizon/blob/cfa611bedf14c9b1ef0b5689b6942aa6431e6313/openstack_dashboard/dashboards/admin/instances/tables.py">openstack_dashboard/dashboards/admin/instances/tables.py</a></p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">AdminInstanceFilterAction</span><span class="p">(</span><span class="n">tables</span><span class="o">.</span><span class="n">FilterAction</span><span class="p">):</span> <span class="n">filter_type</span> <span class="o">=</span> <span class="s">"server"</span> <span class="n">filter_choices</span> <span class="o">=</span> <span class="p">((</span><span class="s">'project'</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s">"Project"</span><span class="p">)),</span> <span class="p">(</span><span class="s">'name'</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s">"Name"</span><span class="p">))</span> <span class="p">)</span> <span class="n">needs_preloading</span> <span class="o">=</span> <span class="bp">True</span> <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">instances</span><span class="p">,</span> <span class="n">filter_string</span><span class="p">):</span> <span class="s">"""Server side search. When filtering is supported in the api, then we will handle in view """</span> <span class="n">filter_field</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'instances__filter__q_field'</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_field</span> <span class="o">=</span> <span class="n">filter_field</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_string</span> <span class="o">=</span> <span class="n">filter_string</span> <span class="k">if</span> <span class="n">filter_field</span> <span class="o">==</span> <span class="s">'project'</span> <span class="ow">and</span> <span class="n">filter_string</span><span class="p">:</span> <span class="k">return</span> <span class="p">[</span><span class="n">inst</span> <span class="k">for</span> <span class="n">inst</span> <span class="ow">in</span> <span class="n">instances</span> <span class="k">if</span> <span class="n">inst</span><span class="o">.</span><span class="n">tenant_name</span> <span class="o">==</span> <span class="n">filter_string</span><span class="p">]</span> <span class="k">if</span> <span class="n">filter_field</span> <span class="o">==</span> <span class="s">'name'</span> <span class="ow">and</span> <span class="n">filter_string</span><span class="p">:</span> <span class="n">q</span> <span class="o">=</span> <span class="n">filter_string</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">return</span> <span class="p">[</span><span class="n">instance</span> <span class="k">for</span> <span class="n">instance</span> <span class="ow">in</span> <span class="n">instances</span> <span class="k">if</span> <span class="n">q</span> <span class="ow">in</span> <span class="n">instance</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span> <span class="k">return</span> <span class="n">instances</span> </code></pre> </div> <p>然后我们看下前端的实现,<a href="https://github.com/openstack/horizon/blob/cfa611bedf14c9b1ef0b5689b6942aa6431e6313/horizon/templates/horizon/common/_data_table_table_actions.html">horizon/templates/horizon/common/_data_table_table_actions.html</a></p> <p>可以看到这里有三种 <code class="highlighter-rouge">filter_type</code>,InstancesFilterAction 默认是 query,AdminInstanceFilterAction 是 server。query 和 server 有什么不同呢?我们去看看 js。</p> <div class="language-javascript highlighter-rouge"><pre class="highlight"><code><span class="nx">horizon</span><span class="p">.</span><span class="nx">datatables</span><span class="p">.</span><span class="nx">set_table_query_filter</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">parent</span><span class="p">)</span> <span class="p">{</span> <span class="nx">$</span><span class="p">(</span><span class="nx">parent</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">'table'</span><span class="p">).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="nx">elm</span><span class="p">)</span> <span class="p">{</span> <span class="kd">var</span> <span class="nx">input</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="nx">elm</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">'div.table_search.client input'</span><span class="p">)),</span> <span class="nx">table_selector</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="nx">input</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Disable server-side searcing if we have client-side searching since</span> <span class="c1">// (for now) the client-side is actually superior. Server-side filtering</span> <span class="c1">// remains as a noscript fallback.</span> <span class="c1">// TODO(gabriel): figure out an overall strategy for making server-side</span> <span class="c1">// filtering the preferred functional method.</span> <span class="nx">input</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'keypress'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">evt</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="nx">evt</span><span class="p">.</span><span class="nx">keyCode</span> <span class="o">===</span> <span class="mi">13</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span> <span class="p">}</span> <span class="p">});</span> <span class="nx">input</span><span class="p">.</span><span class="nx">next</span><span class="p">(</span><span class="s1">'button.btn-search'</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">'click keypress'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">evt</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span> <span class="p">});</span> <span class="c1">// Enable the client-side searching.</span> <span class="nx">table_selector</span> <span class="o">=</span> <span class="s1">'table#'</span> <span class="o">+</span> <span class="nx">$</span><span class="p">(</span><span class="nx">elm</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">'id'</span><span class="p">);</span> <span class="nx">input</span><span class="p">.</span><span class="nx">quicksearch</span><span class="p">(</span><span class="nx">table_selector</span> <span class="o">+</span> <span class="s1">' tbody tr'</span><span class="p">,</span> <span class="p">{</span> <span class="s1">'delay'</span><span class="p">:</span> <span class="mi">300</span><span class="p">,</span> <span class="s1">'loader'</span><span class="p">:</span> <span class="s1">'span.loading'</span><span class="p">,</span> <span class="s1">'bind'</span><span class="p">:</span> <span class="s1">'keyup click'</span><span class="p">,</span> <span class="s1">'show'</span><span class="p">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">show</span><span class="p">,</span> <span class="s1">'hide'</span><span class="p">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">hide</span><span class="p">,</span> <span class="na">onBefore</span><span class="p">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="kd">var</span> <span class="nx">table</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">table_selector</span><span class="p">);</span> <span class="nx">horizon</span><span class="p">.</span><span class="nx">datatables</span><span class="p">.</span><span class="nx">remove_no_results_row</span><span class="p">(</span><span class="nx">table</span><span class="p">);</span> <span class="p">},</span> <span class="na">onAfter</span><span class="p">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="kd">var</span> <span class="nx">template</span><span class="p">,</span> <span class="nx">table</span><span class="p">,</span> <span class="nx">colspan</span><span class="p">,</span> <span class="nx">params</span><span class="p">;</span> <span class="nx">table</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">table_selector</span><span class="p">);</span> <span class="nx">horizon</span><span class="p">.</span><span class="nx">datatables</span><span class="p">.</span><span class="nx">update_footer_count</span><span class="p">(</span><span class="nx">table</span><span class="p">);</span> <span class="nx">horizon</span><span class="p">.</span><span class="nx">datatables</span><span class="p">.</span><span class="nx">add_no_results_row</span><span class="p">(</span><span class="nx">table</span><span class="p">);</span> <span class="nx">horizon</span><span class="p">.</span><span class="nx">datatables</span><span class="p">.</span><span class="nx">fix_row_striping</span><span class="p">(</span><span class="nx">table</span><span class="p">);</span> <span class="p">},</span> <span class="na">prepareQuery</span><span class="p">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="s2">"i"</span><span class="p">);</span> <span class="p">},</span> <span class="na">testQuery</span><span class="p">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">query</span><span class="p">,</span> <span class="nx">txt</span><span class="p">,</span> <span class="nx">_row</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">query</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="nx">_row</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">'td:not(.hidden):not(.actions_column)'</span><span class="p">).</span><span class="nx">text</span><span class="p">());</span> <span class="p">}</span> <span class="p">});</span> <span class="p">}</span> <span class="p">});</span> <span class="p">};</span> </code></pre> </div> <p>如果 filter_type 是 query,就直接调用 js 来 filter,用的是 <a href="https://github.com/DeuxHuitHuit/quicksearch">jQuery quicksearch</a>。(Icehouse 好像用的不是这个版本)</p> <p>那么服务端的 filter 是怎么实现的呢?</p> <p><a href="https://github.com/openstack/horizon/blob/cfa611bedf14c9b1ef0b5689b6942aa6431e6313/horizon/tables/base.py">horizon/tables/base.py</a></p> <p>class DataTable 里的 filtered_data 会调用 action 的 filter 函数,而 get_rows 函数会从 filtered_data 里读数据。</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code> <span class="nd">@property</span> <span class="k">def</span> <span class="nf">filtered_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c"># This function should be using django.utils.functional.cached_property</span> <span class="c"># decorator, but unfortunately due to bug in Django</span> <span class="c"># https://code.djangoproject.com/ticket/19872 it would make it fail</span> <span class="c"># when being mocked by mox in tests.</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">'_filtered_data'</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">_filtered_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="nb">filter</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">_filter_action</span><span class="p">:</span> <span class="n">action</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">_filter_action</span> <span class="n">filter_string</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_filter_string</span><span class="p">()</span> <span class="n">request_method</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="n">needs_preloading</span> <span class="o">=</span> <span class="p">(</span><span class="ow">not</span> <span class="n">filter_string</span> <span class="ow">and</span> <span class="n">request_method</span> <span class="o">==</span> <span class="s">'GET'</span> <span class="ow">and</span> <span class="n">action</span><span class="o">.</span><span class="n">needs_preloading</span><span class="p">)</span> <span class="n">valid_method</span> <span class="o">=</span> <span class="p">(</span><span class="n">request_method</span> <span class="o">==</span> <span class="n">action</span><span class="o">.</span><span class="n">method</span><span class="p">)</span> <span class="k">if</span> <span class="n">valid_method</span> <span class="ow">or</span> <span class="n">needs_preloading</span><span class="p">:</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">mixed_data_type</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_filtered_data</span> <span class="o">=</span> <span class="n">action</span><span class="o">.</span><span class="n">data_type_filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">filter_string</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_filtered_data</span> <span class="o">=</span> <span class="n">action</span><span class="o">.</span><span class="nb">filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">filter_string</span><span class="p">)</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_filtered_data</span> <span class="k">def</span> <span class="nf">get_rows</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="s">"""Return the row data for this table broken out by columns."""</span> <span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">try</span><span class="p">:</span> <span class="k">for</span> <span class="n">datum</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">filtered_data</span><span class="p">:</span> <span class="n">row</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">row_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">datum</span><span class="p">)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_object_id</span><span class="p">(</span><span class="n">datum</span><span class="p">)</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">current_item_id</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">selected</span> <span class="o">=</span> <span class="bp">True</span> <span class="n">row</span><span class="o">.</span><span class="n">classes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">'current_selected'</span><span class="p">)</span> <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row</span><span class="p">)</span> <span class="k">except</span> <span class="nb">Exception</span><span class="p">:</span> <span class="c"># Exceptions can be swallowed at the template level here,</span> <span class="c"># re-raising as a TemplateSyntaxError makes them visible.</span> <span class="n">LOG</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="s">"Error while rendering table rows."</span><span class="p">)</span> <span class="n">exc_info</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()</span> <span class="k">raise</span> <span class="n">template</span><span class="o">.</span><span class="n">TemplateSyntaxError</span><span class="p">,</span> <span class="n">exc_info</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">exc_info</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="k">return</span> <span class="n">rows</span> </code></pre> </div> 20150228 2015-02-28T00:00:00+00:00 http://greenmooon55.com/150228 <p>想着要写篇 blog,现在不知道该写什么了。</p> <p>春节就算过完了,在家最短的一年,十天。春节过得跟十一没什么区别。。没放鞭炮、没怎么见亲戚,连春联都是三十晚上才贴上的。从大一春节回学校开始,每次回家都这么匆忙,我也没有很想家。在家改了改 Leancloud 的 Android todo list demo。</p> <p>这次给家里买了米4、小米手环、小米路由 mini、米键,都物超所值啊。米4没什么亮点,手环计步、来电震动、解锁手机挺好用的,路由比 TP-Link 信号强多了,再也不用我的迷你路由做中继了,米键截图很好用。明年看看能不能给家里买个空气净化器…</p> <p>过年见高中同学最神奇了,本来只有四个宅男见面,吃着饭遇到几个妹子,聊着聊着又遇到个,于是我们就在美食广场从午饭坐到晚饭,这些妹子毕业之后都没见过…</p> <p>大家都挺拼的,在扬州工作的同学网上投了个简历,就来上海工作了。在哈尔滨的同学看到上海的招聘会,直接飞过来面试。。就这样来上海了。不知道我有没有这勇气诶。某童鞋问了我之后开始在扇贝上被单词,现在已经是66天 * 150词/天了,我都几年没背过了= =</p> <p>初七,第一次看到这么冷清的上海,好多店铺都没开。虽然有喜欢做的任务,这周还是过得好慢,终于到周末了~新的一年开始了!</p> 20150218新春快乐 2015-02-18T00:00:00+00:00 http://greenmooon55.com/150218 <p>羊年就要来了,新年快乐!</p> <p>我现在完全会想不出14年下半年我都干嘛了,好像生命中少了几个月一样。工作效率不高,心情也不太好。只能想到骑车去了趟嘉定,水了一场hackathon。</p> <p>去年年底好好去玩了一趟,回来之后就不知道为啥就觉得世界很美好了。。2015年感觉很好,工作虽然很忙,但是很开心,发现真的有好多可以学的。</p> <p>执行力太重要了,比如说要不是某童鞋,去年年底肯定不会出去玩。想做什么就花时间去做嘛,不然以后会后悔的。就像<a href="cntrains.com">cntrains</a>做出来没花多久:)现在在写Android版yaml的demo(不知道能写到啥程度。。), 口琴也到手了,还没开始玩。还想看看<a href="https://github.com/gitblog-io/gitblog-io.github.io">gitblog</a>呢。</p> <p>现在感觉找到了大一时候的感觉:) 万一实现了呢~</p> Upgrade Openstack From Grizzly To Icehouse 2015-02-14T00:00:00+00:00 http://greenmooon55.com/upgrade-openstack-from-grizzly-to-icehouse <p>首先,我们在Meetup的时候介绍过升级,请看<a href="http://techshow.ctrip.com/archives/810.html">这里</a>的Session 3。说到升级,想起Paris summit的一个视频:<a href="https://www.openstack.org/summit/openstack-paris-summit-2014/session-videos/presentation/upgrading-in-place-from-grizzly-to-icehouse-a-cautionary-tale">Upgrading in Place from Grizzly to Icehouse: A Cautionary Tale</a>,具体说什么没啥印象了,只记得里面那句吐槽:one developer, two weeks,事实证明不可能那么快= =</p> <p>网上有很多种升级方案,例如<a href="https://openstack.redhat.com/Upgrading_RDO">RDO的分类</a>就很不错。我们这边的环境对cloud的可用性要求不高,只要已经部署好的VM不受影响,可以接受较长时间不部署/管理虚拟机,所以我们最终选择了pdf里的方案一,就是controller原地升级,其实最后我们觉得换controller也不错…能降低点风险,还好升级时用方案一没遇到太多问题。</p> <p>升级第一关就是db migration了,理论上来说上游代码里会考虑到升级的问题,每步db migration应该都在代码里面。其实这里还是有些问题的,我们有自己的改动,要先downgrade放弃自己的修改再upgrade,我们旧版上所有代码改动都先放弃了。。</p> <p>首先就是grizzly的keystone client不支持降级(db sync指定版本),这个很好解决,改改代码咯。还有icehouse的nova db migration只支持从H版升上来,解决办法就是从H版把从G升到H的代码搞过来。。</p> <p>难点在于neutron的db migration。I版的neutron经过重构,有了ml2 plugin,我们db sync之后还需要用官方提供的<a href="https://github.com/openstack/neutron/blob/3116ffafdca72e9eef70f4eaf90626982ae51d4c/neutron/db/migration/migrate_to_ml2.py">migrate to ml2的脚本</a>来改数据库,以支持ml2 plugin。H版的neutron在建VM的时候会把host和port插入portbindingports表,migrate to ml2脚本就会从这个表读数据并插入到<code class="highlighter-rouge">ml2_port_bindings</code>表里,network segment的信息是由<code class="highlighter-rouge">ovs_network_bindings</code>表搞出来的。(待补充)。。</p> <p>下面问题来了,portbindingports是H版才有的表,G版根本没有啊,从grizzly升上来也不会建这个表,这样migrate to ml2就会有问题。不过用ovs plugin跑起来没啥问题…最终我们的办法是在db sync之后,migrate to ml2之前把host和port插入进去,这样看起来数据ok!网上没搜到这种问题,很奇怪。还有I版用ovs plugin的时候,如果是手工建的port,neutron不会插入到portbindingports里,migrate to ml2就不会插入<code class="highlighter-rouge">ml2_port_bindings</code>表里,但是ml2 plugin会把这种port插入<code class="highlighter-rouge">ml2_port_bindings</code>。Anyway,我们最后也把这种unbound的port都插入进去了。</p> <p>以上就是db migration的问题,我们在测试升级和实际操作的时候还遇到一些问题。</p> <ol> <li> <p>mysql备份出来的sql,注释的地方也是有用的,不能随便删掉。</p> </li> <li> <p>更新包的时候有些包没有依赖,不会自动更新,要手动搞。</p> </li> <li> <p>neutron agents由fqdn改成hostname,要删掉inactive的那些(这个文档也写了)</p> </li> <li> <p>太多坑了= =!</p> </li> </ol> <p>我们整个升级用时是估算时间*2…</p> <p>下次升级一定要准备更充分一点,估计是升到Kilo或者Liberty了…最好neutron的代码也有人认真看看。其实I版上游的代码还有挺多bug的,要是有能力的话有很多机会贡献代码= =…</p> <p>研究升级方案时发现有好多好多可以学的东西啊~之前一直有其他需求,没空搞升级,终于在年前搞定这件大事了。(似乎还有坑没发现T_T)</p> Novaclient 载入 extensions 的过程 2014-10-28T00:00:00+00:00 http://greenmooon55.com/python-novaclient <p><code class="highlighter-rouge">novaclient/v1_1/contrib</code>里可以加入新的指令,例如:</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="nd">@utils.arg</span><span class="p">(</span><span class="s">'server'</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">'&lt;server&gt;'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">'Name or UUID of the server to list actions for.'</span><span class="p">))</span> <span class="k">def</span> <span class="nf">do_instance_action_list</span><span class="p">(</span><span class="n">cs</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span> <span class="s">"""List actions on a server."""</span> <span class="n">server</span> <span class="o">=</span> <span class="n">utils</span><span class="o">.</span><span class="n">find_resource</span><span class="p">(</span><span class="n">cs</span><span class="o">.</span><span class="n">servers</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">server</span><span class="p">)</span> <span class="n">actions</span> <span class="o">=</span> <span class="n">cs</span><span class="o">.</span><span class="n">instance_action</span><span class="o">.</span><span class="nb">list</span><span class="p">(</span><span class="n">server</span><span class="p">)</span> <span class="n">utils</span><span class="o">.</span><span class="n">print_list</span><span class="p">(</span><span class="n">actions</span><span class="p">,</span> <span class="p">[</span><span class="s">'Action'</span><span class="p">,</span> <span class="s">'Request_ID'</span><span class="p">,</span> <span class="s">'Message'</span><span class="p">,</span> <span class="s">'Start_Time'</span><span class="p">],</span> <span class="n">sortby_index</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> </code></pre> </div> <p>那这个函数是怎么被调用的呢</p> <p>在 <code class="highlighter-rouge">novaclient/shell.py</code> 的 <code class="highlighter-rouge">main</code> 里有</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="c"># build available subcommands based on version</span> <span class="bp">self</span><span class="o">.</span><span class="n">extensions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_discover_extensions</span><span class="p">(</span> <span class="n">options</span><span class="o">.</span><span class="n">os_compute_api_version</span><span class="p">)</span> </code></pre> </div> <p>然后找到这个函数</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">def</span> <span class="nf">_discover_extensions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">version</span><span class="p">):</span> <span class="n">extensions</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">module</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">_discover_via_python_path</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_discover_via_contrib_path</span><span class="p">(</span><span class="n">version</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_discover_via_entry_points</span><span class="p">()):</span> <span class="n">extension</span> <span class="o">=</span> <span class="n">novaclient</span><span class="o">.</span><span class="n">extension</span><span class="o">.</span><span class="n">Extension</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">module</span><span class="p">)</span> <span class="n">extensions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">extension</span><span class="p">)</span> <span class="k">return</span> <span class="n">extensions</span> </code></pre> </div> <p><code class="highlighter-rouge">self._discover_via_python_path()</code> 会找以 <code class="highlighter-rouge">python_novaclient_ext</code> 结尾的 module</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">def</span> <span class="nf">_discover_via_contrib_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">version</span><span class="p">):</span> <span class="n">module_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">__file__</span><span class="p">))</span> <span class="n">version_str</span> <span class="o">=</span> <span class="s">"v</span><span class="si">%</span><span class="s">s"</span> <span class="o">%</span> <span class="n">version</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">'.'</span><span class="p">,</span> <span class="s">'_'</span><span class="p">)</span> <span class="n">ext_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">module_path</span><span class="p">,</span> <span class="n">version_str</span><span class="p">,</span> <span class="s">'contrib'</span><span class="p">)</span> <span class="n">ext_glob</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">ext_path</span><span class="p">,</span> <span class="s">"*.py"</span><span class="p">)</span> <span class="k">for</span> <span class="n">ext_path</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">iglob</span><span class="p">(</span><span class="n">ext_glob</span><span class="p">):</span> <span class="n">name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">ext_path</span><span class="p">)[:</span><span class="o">-</span><span class="mi">3</span><span class="p">]</span> <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s">"__init__"</span><span class="p">:</span> <span class="k">continue</span> <span class="n">module</span> <span class="o">=</span> <span class="n">imp</span><span class="o">.</span><span class="n">load_source</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">ext_path</span><span class="p">)</span> <span class="k">yield</span> <span class="n">name</span><span class="p">,</span> <span class="n">module</span> </code></pre> </div> <p>其中 <code class="highlighter-rouge">glob.iglob(ext_glob)</code> 会生成一个 iterator 循环所有 <code class="highlighter-rouge">contrib/*.py</code>,<code class="highlighter-rouge">imp.load_source(name, ext_path)</code> 再加载 module。</p> <p>最后载入 Entry Points <code class="highlighter-rouge">novaclient.extensions</code>,参考 <a href="http://stackoverflow.com/a/9615473">http://stackoverflow.com/a/9615473</a></p> Cntrains.com Docker 自动部署 2014-09-20T00:00:00+00:00 http://greenmooon55.com/cntrains <p>很久之前就发现<a href="http://yupiao.info">余票信息网yupiao.info</a> 上能查到余票、正晚点、车次信息,作者还<a href="http://scatwang.com/index.php/2013/07/%E4%BD%99%E7%A5%A8%E5%92%8C%E9%93%81%E8%B7%AF%E5%9C%B0%E5%9B%BE%E7%9A%84%E9%82%A3%E7%82%B9%E4%BA%8B%E5%84%BF/">介绍了网站的实现</a>,我也想自己写一个相似的网站。想了很久都没有动手,几个月前突发奇想去找域名,刚好在 Godaddy 上搜到 cntrains.com 这个域名在拍卖,截止日期刚好是当天,于是我用底价就拍到了,好幸运:-)</p> <p>买到之后闲置了很久,后来写了个抓盛名时刻表 rar 压缩包的东西。</p> <p>网站:<a href="http://cntrains.com/smsk">cntrains.com/smsk</a></p> <p>代码:<a href="http://github.com/greenmoon55/cntrains">github.com/greenmoon55/cntrains</a></p> <p>##获取数据</p> <p>这个,我不好意思叫爬虫。。盛名时刻表官方的下载地址很好搞到,从下载页面正则匹配一下就可以获取到最新的下载地址,直接 GET 就能下载到,下载地址里时包含发布时间的,我暴力了一下,找到的最旧的版本是去年的,可惜没有更早的。现在用 crontab 每小时查询一次。</p> <p>##存储</p> <p>最开始是把压缩包存在本地,然后用数据库记录下来,现在放在七牛云上了,每月免费配额很多,完全用不完,代码也好写。</p> <p>##代码</p> <p>代码是用 Django 写的,你可能想问了,为嘛用 Django 呢,就这么一个破网页,呃,因为公司有用到(<a href="https://github.com/openstack/horizon/">horizon</a>),顺便看看。</p> <p>##服务器</p> <p>最开始用的是Azure免费试用,最近刚刚换成DigitalOcean,DO在墙外要方便一点,感觉Azure还有点小问题,比如内网的DNS。DO看起来挺好用的,一个月5刀可以接受。</p> <p>##Docker</p> <p>嗯,这个是跑在 Docker 上的,有必要么?我只是跑着玩玩嘛。Dockerfile里主要就是 <code class="highlighter-rouge">pip install</code>了,另外还要自己起 cron。</p> <p>玩了一下 Docker Hub 的 webhook,就是每次 push 的时候会 POST 一个 json 到自己指定的 URL,我在服务器上又起了个 python http server,接到这个请求就重新 pull image 再 run image。只要 push 就能自动部署啦,分分中搞定,有木有 Heroku 的感觉!</p> <p>参考:<a href="http://nathanleclaire.com/blog/2014/08/17/automagical-deploys-from-docker-hub/">Automagical Deploys from Docker Hub</a></p> <p>##未来</p> <p>首先要把页面稍微改改,至少声明跟软件作者无关。其他功能嘛,暂时懒得弄,我最终目标时要做个自动更新带历史记录的火车时刻表,最好提供个API,其实和 yupiao.info 之前做的差不多,但是没空研究爬虫、验证码方面的东西,很可能会无限期推迟(但愿不会)。</p> <p>监控和测试目前没有,可以考虑加上。</p> <p>Docker Hub 支持 Automated Build,就是 push 到 GitHub 之后可以自动根据 Dockerfile build image,但是我有些文件不想传到 GitHub 上(密码、access key),不知道该怎么办,其实现在也不错了。</p> <p>有个基于 Ubuntu 修改过的 Docker image <a href="https://registry.hub.docker.com/u/phusion/baseimage/">phusion/baseimage</a>,解决了一些问题,有 cron 和 ssh server,挺热门的。</p> <p>部署方面,要看看 CoreOS,现在 DO 和 UStack 都支持了。现在还有一些 Docker container hosting 的公司,也就是说以后想部署服务,连 VM 都不用搞,直接 push docker image就可以。比如这两家:<a href="http://quay.io">Quay.io</a> 和 <a href="https://www.tutum.co">tutum</a></p> <p>前几天看到陈沙克的<a href="http://weibo.com/1748606363/Bn9xq1zKh">这条微博</a>,感觉确实很多东西可以用 Docker 来搞了,没必要搞那些 VM 了。</p> <p>zhxq 最近做了<a href="http://piao.today">票.今天</a>,能生成每小时余票趋势图,看起来好厉害~</p> 使 Cloud-init 只读取一次 Metadata 2014-07-04T00:00:00+00:00 http://greenmooon55.com/cloud-init-get-metadata-only-once <p>Linux 的 <a href="https://launchpad.net/ubuntu/+source/cloud-init">cloud-init</a> 会在每次启动时运行,默认每次都会读取 metadata,如果 metadata service 挂掉了或者响应缓慢就会使虚拟机重启时花费很多时间,一般 metadata 也不会改变,Windows 上的 <a href="http://www.cloudbase.it/cloud-init-for-windows-instances/">cloudbase-init</a> 貌似就只会读一次。</p> <p>只需配置<code class="highlighter-rouge">manual_cache_clean:True</code>,否则在 init-local 时会自动清除缓存,见<a href="https://github.com/number5/cloud-init/blob/d37e212159ee14ca859714a7473268717db93e88/bin/cloud-init#L233">代码</a>。</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code> <span class="c"># The cache is not instance specific, so it has to be purged</span> <span class="c"># but we want 'start' to benefit from a cache if</span> <span class="c"># a previous start-local populated one...</span> <span class="n">manual_clean</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">get_cfg_option_bool</span><span class="p">(</span><span class="n">init</span><span class="o">.</span><span class="n">cfg</span><span class="p">,</span> <span class="s">'manual_cache_clean'</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span> <span class="k">if</span> <span class="n">manual_clean</span><span class="p">:</span> <span class="n">LOG</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">"Not purging instance link, manual cleaning enabled"</span><span class="p">)</span> <span class="n">init</span><span class="o">.</span><span class="n">purge_cache</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">init</span><span class="o">.</span><span class="n">purge_cache</span><span class="p">()</span> <span class="c"># Delete the non-net file as well</span> <span class="n">util</span><span class="o">.</span><span class="n">del_file</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path_helper</span><span class="o">.</span><span class="n">get_cpath</span><span class="p">(</span><span class="s">"data"</span><span class="p">),</span> <span class="s">"no-net"</span><span class="p">))</span> </code></pre> </div> 毕业设计 2014-06-24T00:00:00+00:00 http://greenmooon55.com/graduation-thesis <p>还是写写毕设的过程吧。从选导师开始,这就是一个很烦的事情。因为热门导师有很多人选,三个志愿其实后两个完全没意义,我就三个都没要我。。还好最后我的导师还不错,很负责的,每一两周都检查我们的进度,提一些建议。</p> <p>不想随便写个无聊的项目,最开始我的选择是Kaggle上的<a href="www.kaggle.com/c/expedia-personalized-sort">Personalize Expedia Hotel Searches - ICDM 2013</a>,导师觉得她也不熟,就换了个短文本聚类的题目,最开始的选题是《并行聚类算法在短小文本上的应用》,寒假的时候在家还在看谱聚类的算法,《A Tutorial on Spectral Clustering》(请Google)写得很详细,数学太烂了,看了点线性代数,还是看不懂,后来导师告诉我不用看证明了= =只实现出来就好。 接下来几个月就慢慢在意写代码的基础上实现了K-means、层次聚类和谱聚类,再通过WordNet获取相似度,最后就搞定了。。放弃了Hadoop相关的东西,所有内容都是导师提供的,创新性为0。这是<a href="https://speakerdeck.com/greenmoon55/ju-lei-fen-xi-zai-duan-xiao-wen-ben-shang-de-ying-yong">答辩PPT</a>。</p> <p>最后一周的时候,导师叫我把传统文本聚类的向量空间模型也做出来,数据集是路透社经过处理后的一些向量,我还请了假写好代码之后发现求出的最佳聚类数目跟正确答案不匹配,又没有和原始数据的对应关系,根本没法debug。那个时候一个人在新租的房子里非常不爽,我的感受就像在做一个需求不清的项目,到了最后一刻又要改需求。反思之后,感觉我的整体毕设就是一直被导师push,像在完成一个作业一样,应该要反过来才对,最开始就应该好好研究一下我原始的选题,早一点要实验数据,早知道是这样的数据的话就应该很早放弃并行,这么点数据搞毛线啊。还有一点很不爽,就是没有去做数据的预处理,起初还想做twitter真实数据呢。。完全不可能。最后的东西只是在很烂的数据上跑了一下而已。</p> <p>通过毕设了解一点学术圈了,就是不停地提出想法、做实验、发论文的样子。数据挖掘这边coding其实不太重要,数学不行寸步难行。最后写论文的时候花了很久,写到将近一万字到一万五千字是最艰难的,都不知道些什么好了,最后凑到两万多字,四十多页。假期在家看谱聚类的时候也很不爽的。。还想去嘉定看看大组答辩都有啥呢,那天没去,在印论文。</p> <p>再列几个有价值的参考资料:</p> <ul> <li> <p><a href="http://blog.pluskid.org/?page_id=78">漫谈 Clustering 系列</a> 对各种聚类算法都有介绍</p> </li> <li> <p>Clustering Indices Bernard Desgraupes University Paris Ouest Lab Modal’X April 2013 介绍各种聚类评价方法</p> </li> <li> <p><a href="http://book.douban.com/subject/3639345/">Web数据挖掘</a> 聚类算法介绍详细,译者还有sjtu的俞勇,不过翻译一般,有些词前后译文都不一样。。</p> </li> <li> <p><a href="http://www.ruanyifeng.com/blog/2013/03/tf-idf.html">TF-IDF与余弦相似性的应用(一):自动提取关键词</a> 阮一峰的文章,我论文里的传统文本聚类就是在凑字数- -</p> </li> <li> <p><a href="http://www.matrix67.com/blog/archives/5044">互联网时代的社会语言学:基于SNS的文本数据挖掘</a> M67很早以前写的,just for fun</p> </li> </ul> <p>写论文的过程中Word仍然经常崩溃或者按了保存其实没存上,非常感谢Dropbox一直在备份,还保留历史记录。这次学到了Word自动加参考文献、给图片和表格加编号,还有用制表符给公式编号,以后估计也用不到了。</p> 死亡诗社 2014-03-31T00:00:00+00:00 http://greenmooon55.com/dead-poets-society <embed src="http://player.youku.com/player.php/sid/XNjYwOTg0OTIw/v.swf" allowfullscreen="true" quality="high" width="480" height="400" align="middle" allowscriptaccess="always" type="application/x-shockwave-flash" /> <p>&lt;/embed&gt;</p> <p>寒假在家看超级碗广告的时候发现的这个 iPad 广告,太赞了,吸引我把<a href="http://movie.douban.com/subject/1291548/">电影</a>也看了,电影有点“痛斥美国教育”的感觉…里面的诗写得真好。</p> <blockquote> <p>We don’t read and write poetry because it’s cute. We read and write poetry because we are <strong>members of the human race</strong>. And the human race is filled with passion. And medicine, law, business, engineering, these are noble pursuits and necessary to sustain life. But poetry, beauty, romance, love, <strong>these are what we stay alive for</strong>.</p> </blockquote> <p>还有这一段</p> <blockquote> <p>To quote from Whitman, “O me! O life!… of the questions of these recurring; of the endless trains of the faithless… of cities filled with the foolish; what good amid these, O me, O life?” Answer. That you are here - that life exists, and identity; that the powerful play goes on and you may contribute a verse. That the powerful play goes on and you may contribute a verse. What will your verse be?</p> </blockquote> <p>电影里印象很深的还有这段,Mr.Keating 带学生看很多年前也在这所学校上学的学生的照片,这些照片就摆在楼下,现在的学生们每天匆匆走过,从不驻足。照片里的学生和现在的学生差不多,充满希望,不过现在他们已经离世了,最后到底有没有有所成就呢?</p> <blockquote> <p>They’re not that different from you, are they? Same haircuts. Full of hormones, just like you. Invincible, just like you feel. The world is their oyster. <strong>They believe they’re destined for great things, just like many of you, their eyes are full of hope, just like you.</strong> Did they wait until it was too late to make from their lives even one iota of what they were capable? Because, you see gentlemen, these boys are now fertilizing daffodils. But if you listen real close, you can hear them whisper their legacy to you. Go on, lean in. Listen, you hear it? - - Carpe - - hear it? - - <strong>Carpe, carpe diem, seize the day boys, make your lives extraordinary.</strong></p> </blockquote> <p>还有电影中评价诗的方法:完美程度为x轴、重要性为y轴,面积越大说明诗越好,真的是笑死了哈哈。于是老师直接让大家把这页书撕掉了。</p> <p><a href="http://www.imdb.com/character/ch0004782/quotes">John Keating (Character) - Quotes - IMDB</a></p> <p><a href="http://movie.douban.com/review/1690137/">残酷青春 (死亡诗社 影评) - 豆瓣</a></p> 烟花三月下扬州 2014-03-26T23:46:00+00:00 http://greenmooon55.com/yangzhou <p>还记得小学时全班一起<strong>唱</strong>《黄鹤楼送孟浩然之广陵》</p> <blockquote> <p>故人西辞黄鹤楼,烟花三月下扬州,孤帆远影碧空尽,唯见长江天际流。</p> </blockquote> <p>古人用的是农历,我们三月去扬州的时候,瘦西湖好多花儿还没开,游客倒是已经有很多了。瘦西湖很大,和西湖相比,瘦西湖确实很瘦,感觉更像一条河,有“二十四桥”(<a href="http://baike.baidu.com/view/160007.htm">寄扬州韩绰判官</a>,<a href="http://baike.baidu.com/view/2753818.htm">扬州慢·淮左名都</a>),有亭台楼阁,有假山瀑布,还有古时的城墙。</p> <p><img src="/images/yangzhou1.jpg" alt="" /></p> <p><img src="/images/yangzhou2.jpg" alt="" /></p> <p><img src="/images/yangzhou3.jpg" alt="" /> 湖心岛上的小径,挺有感觉的</p> <p><img src="/images/yangzhou4.jpg" alt="" /> 从大明寺景区拍瘦西湖,到处都在施工,整个扬州也是,搞得很多尘土。另外看到在修下穿瘦西湖的隧道。</p> <p><img src="/images/yangzhou5.jpg" alt="" /> 个园,有各种竹子,中国四大名园之一。有很多竹子、假山,四季风景。江南的园林还挺有意思。</p> <p><img src="/images/yangzhou6.jpg" alt="" /></p> <p><img src="/images/yangzhou7.jpg" alt="" /> 何园,刚好赶上人家拍婚纱照,这个角度好漂亮。</p> <p><img src="/images/yangzhou8.jpg" alt="" /> 别有洞天</p> <p><img src="/images/yangzhou9.jpg" alt="" /> 复道回廊,走廊连通着各个房子</p> <p><img src="/images/yangzhou10.jpg" alt="" /> 文昌阁</p> <p><img src="/images/yangzhou11.jpg" alt="" /> 东关街上一家很精致的贺卡店,这些贺卡都能折起来的哦</p> <p><img src="/images/yangzhou12.jpg" alt="" /></p> <p><img src="/images/yangzhou13.jpg" alt="" /> 瓜州(泊船瓜州)镇杨汽渡,24小时不断</p> <p>古代的扬州多么繁华,才有了现在这些园林,当年的扬州可能是现在的南京、上海,可惜随着航运的衰落,扬州不再那么重要,感觉城市建设完全比不上苏州。</p> <p>扬州是个很特别的城市,景点和美食都值得一去,淮扬菜比较清淡,我喜欢,还没吃够,希望有机会再来。不过扬州炒饭没觉得很特别。这次和童鞋们玩得很开心:)</p> <p>乘坐 CRH2-075C 和 CRH2-076C 往返,还是比普速快多了,这次发现花钱换时间还是很爽的…比如说打车、乘高铁。</p> 20140215 2014-02-15T01:25:00+00:00 http://greenmooon55.com/140215 <p>时间过得太快啦!转眼间已经过了十五。在家里过了学生时代最后一个寒假,总结一下都干了啥。每次假期最后一天都会很后悔时间利用不够好,当然这次也不例外。</p> <p>1月19号下午到家,2月8号走,约三周。</p> <p>20略,23见同学看了kmeans,24-26搞定100行的代码,这一周时间利用还可以。</p> <p>第二周最不爽了,看线代教材看不下去,上网找视频看,看了一段时间又觉得看不完,又回来看书,浪费了很多时间。。导致过年的时候心情不太好。而且其中很长时间都在玩游戏…以后注意..</p> <p>最后一周没什么,对于我来说过年区别不大,家里聚聚,跟同学吃吃饭,很快就过去了。值得一提的是三个人一起去了趟烟筒山,这种运转的机会不知道以后还有吗= = 另外我找了几个好久没见的高中同学,财富广场真不错,连高中时在北京吃的比格比萨自助都有..为啥我高中、初中、小学都没人组织聚会呢…真神奇= =!吉林这次有了山寨无印良品风格的店,全场十元…</p> <p>越长大越想家哈,以后估计只有十一和春节会回家…大概就是飞机回吉林,Z62+G1 或 T60+G13 回上海吧,火车的话就末班高铁到北京然后换临D(如果有的话),这样会累死吧。</p> 最小树形图 2013-12-22T13:31:00+00:00 http://greenmooon55.com/directed-minimum-spanning-tree <p>参考资料:</p> <ul> <li><a href="http://www.cs.princeton.edu/courses/archive/spring13/cos528/directed-mst-1.pdf">Lecture notes on “Analysis of Algorithms”: Directed Minimum Spanning Trees</a></li> <li><a href="http://www.csie.ntnu.edu.tw/~u91029/SpanningTree.html#5">演算法筆記 - Spanning Tree</a></li> </ul> <p>先翻译一部分上面的 Lecture notes:</p> <p>最小树形图也就是有向最小生成树。如果有向加权图 G 有子图 T,T 中从某个结点 r 可以到达全部其他结点,并且若这些边是无向边,T 就是最小生成树的话,那么 T 就是 G 上以 r 为根的有向生成树。最小有向生成树就是有向生成树中边权值的和最小的那个。(有点晕。。)</p> <p><strong>引理 1.1</strong> 以下条件等价:</p> <ol> <li>T 是 G 的以 r 为根的有向生成树</li> <li>r 在 T 中的入度为 0,其它边在 T 中的入度是 1,且 T 是无环的。</li> <li>r 在 T 中的入度为 0,其它边在 T 中的入度是 1,T 中从 r 到其他任意结点都有有向路径</li> </ol> <p><strong>推论 1.2</strong></p> <p>设 T 是以 r 为根的有向生成树,设 (u,v) 是不在 T 中的一条边,且 v != r,u 不是 T 中 v 的后续结点(后续结点的意思是 T 中从 v 到 u 有路径),设 (u’, v) 是 T 中终点为 v 的(唯一的)边。那么把 T 加上 (u,v) 去掉 (u,v’) 仍然是一个 r 为根的有向生成树。</p> <p><strong>定理 4.1</strong></p> <p>设 G=(V, E, w)是加权有向图,设 r 在 V 中。设 C 是一个不包含 r 的由最小的入边构成的边集。那么,存在一个最小有向生成树,包含 C 中去掉一条边后剩余的边。</p> <p>证明:设 T 是 G 中以 r 为根的最小有向生成树。设 v1 是 C 中的点,且 T 中从 r 到 v1 不经过 C 中其他的边。(一定至少存在一个点,比如 C 中在 T 上最接近 r 的点中的一个。)按照环上的顺序,设 v1, v2, .., vk 是 C 中的点。如果 (v1, v2), (v2, v3), …, (vk - 1, vk) 属于T,我们就搞定了。不过,假设存在某个 i &lt; k,(v1, v2), (v2, v3), …, (vi - 1, vi) 属于 T,而 (vi, vi + 1) 不属于T。设 T 中 vi + 1 的入边为 (u, vi + 1)。vi 在 T 中的上游结点是 T 中从 r 到 v1 这些结点和 C 上的 v1, v2, …, vi - 1,所以 vi 不是 vi + 1 的下游结点(descendents…)。根据推论1.2,T 去掉 (u, vi + 1) 加上 (vi, vi + 1) 仍然是一个以 r 为根的生成树,记为T‘。既然 (vi, vi + 1) 是进入 vi + 1 的最短边,T’ 也是以 r 为根的最小有向生成树。一直这样做,我们就能构造一个以 r 为根,包含 C 中除了 (vk, v1) 之外所有边的最小有向生成树。</p> <p>接下来说说做法,根据上面的定理:</p> <ol> <li>找除了根节点外其他点的最小入边,如果哪个点没有,就说明无解</li> <li>看看现在是否有环,如果无环则找到解了,记录当前选择的所有边的权值和,作为答案。</li> <li>把环缩点,设 u 是不在环上的点,v 是在环上的点,minEdgeDis[v] 表示 v 的最短入边的权值,那么把边 (u, v) 的权值减去 minEdgeDis[v]。把环上所有的点合并为同一个点。回到第一步。</li> </ol> <p>曾经2011年大连网络赛就出了这个算法的模板题 <a href="http://acm.hdu.edu.cn/showproblem.php?pid=4009">HDU 4009 Transfer Water</a>,当时好像是 yds 远程过的…我到现在还没去做这道题。</p> Debian Wheezy 使用 oh-my-zsh 的小问题 2013-12-22T13:26:00+00:00 http://greenmooon55.com/oh-my-zsh-on-debian <p>最近发现默认的 Shell(应该是 bash)有点小问题,果断换成 zsh,装了 oh-my-zsh 之后发现每次开启终端都会显示类似环境变量的东西</p> <p>查了半天问题在哪,发现是在 <code class="highlighter-rouge">~/.oh-my-zsh/oh-my-zsh.sh</code> 里:</p> <div class="language-sh highlighter-rouge"><pre class="highlight"><code><span class="c"># Check for updates on initial load...</span> <span class="k">if</span> <span class="o">[</span> <span class="s2">"</span><span class="nv">$DISABLE_AUTO_UPDATE</span><span class="s2">"</span> !<span class="o">=</span> <span class="s2">"true"</span> <span class="o">]</span>; <span class="k">then</span> /usr/bin/env <span class="nv">ZSH</span><span class="o">=</span><span class="nv">$ZSH</span> <span class="nv">DISABLE_UPDATE_PROMPT</span><span class="o">=</span><span class="nv">$DISABLE_UPDATE_PROMPT</span> zsh <span class="nv">$ZSH</span>/tools/check_for_upgrade.sh <span class="k">fi</span> </code></pre> </div> <p>执行 <code class="highlighter-rouge">/usr/bin/env</code> 的时候会输出这些东西。解决办法就是在第三行重定向输出:</p> <div class="language-sh highlighter-rouge"><pre class="highlight"><code>/usr/bin/env <span class="nv">ZSH</span><span class="o">=</span><span class="nv">$ZSH</span> <span class="nv">DISABLE_UPDATE_PROMPT</span><span class="o">=</span><span class="nv">$DISABLE_UPDATE_PROMPT</span> zsh <span class="nv">$ZSH</span>/tools/check_for_upgrade.sh &amp;&gt;/dev/null </code></pre> </div> Horizon 中 Python 元编程的例子 2013-11-27T00:00:00+00:00 http://greenmooon55.com/python-metaprogramming-example-in-horizon <p>Horizon 中创建虚拟机的 workflows 里面有这样一个类,用来选择虚拟机的一些信息,比如配置。<a href="https://github.com/openstack/horizon/blob/cde81c38027b17014abcd1449a263bdc411503cc/openstack_dashboard/dashboards/project/instances/workflows/create_instance.py#L170">GitHub代码</a></p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">SetInstanceDetailsAction</span><span class="p">(</span><span class="n">workflows</span><span class="o">.</span><span class="n">Action</span><span class="p">):</span> </code></pre> </div> <p>里面有这几个方法</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">def</span> <span class="nf">populate_image_id_choices</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span> <span class="n">images</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_available_images</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span> <span class="n">choices</span> <span class="o">=</span> <span class="p">[(</span><span class="n">image</span><span class="o">.</span><span class="nb">id</span><span class="p">,</span> <span class="n">image</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">for</span> <span class="n">image</span> <span class="ow">in</span> <span class="n">images</span> <span class="k">if</span> <span class="n">image</span><span class="o">.</span><span class="n">properties</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"image_type"</span><span class="p">,</span> <span class="s">''</span><span class="p">)</span> <span class="o">!=</span> <span class="s">"snapshot"</span><span class="p">]</span> <span class="k">if</span> <span class="n">choices</span><span class="p">:</span> <span class="n">choices</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="p">(</span><span class="s">""</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s">"Select Image"</span><span class="p">)))</span> <span class="k">else</span><span class="p">:</span> <span class="n">choices</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="p">(</span><span class="s">""</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s">"No images available."</span><span class="p">)))</span> <span class="k">return</span> <span class="n">choices</span> <span class="k">def</span> <span class="nf">populate_instance_snapshot_id_choices</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="k">def</span> <span class="nf">populate_flavor_choices</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> </code></pre> </div> <p>这几个方法是用来填充一些页面上的选择框的,看来看去不知道是在哪里被调用的。</p> <p>于是找父类 <a href="https://github.com/openstack/horizon/blob/cde81c38027b17014abcd1449a263bdc411503cc/horizon/workflows/base.py#L77">workflows.Action</a></p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="k">def</span> <span class="nf">_populate_choices</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span> <span class="k">for</span> <span class="n">field_name</span><span class="p">,</span> <span class="n">bound_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> <span class="n">meth</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">"populate_</span><span class="si">%</span><span class="s">s_choices"</span> <span class="o">%</span> <span class="n">field_name</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="k">if</span> <span class="n">meth</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="nb">callable</span><span class="p">(</span><span class="n">meth</span><span class="p">):</span> <span class="n">bound_field</span><span class="o">.</span><span class="n">choices</span> <span class="o">=</span> <span class="n">meth</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span> </code></pre> </div> <p>哈,原来在这里,看出来做什么了吧。太具体的我也还没研究= =</p> <p>最后,在 <code class="highlighter-rouge">__init__</code> 中有一行。</p> <div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="bp">self</span><span class="o">.</span><span class="n">_populate_choices</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span> </code></pre> </div> <p>看大牛写的高质量代码也有点意思,我想吐槽貌似 Django 的文档不够全面啊,至少跟 Rails 比。</p> 实习感受131127 2013-11-27T00:00:00+00:00 http://greenmooon55.com/intern-at-ctrip <p>前段时间换了地方实习,现在在携程待了半个多月,我们组主要是做内部的 IAAS,更具体的说貌似只是给其他组分配虚拟机。。最近主要看了 Python, Django 和 Openstack Horizon,Horizon 是基于 Django 的,然后调用 Nova 的 API,Nova 再去用具体的虚拟化技术分配虚拟机,比如 VMware vSphere 和 KVM。刚开始觉得 Python 不太习惯,缩进有点丑,很多地方没有 Ruby 舒服,现在习惯多了。</p> <p>现在还没什么事情做,没写代码也动力不大,感觉我用了不到百分之七八十的精力。其实对组里具体事情还不了解,晨会都没有,希望下个月能逐渐熟悉起来。现在看起来还不错,就是写代码有点少。= =不知道这个方向是不是有点窄?前两天把三方交给学院,发现我是第一个交的。。</p> <p>携程让人很不爽的地方就是办公区域太小了,甚至比寝室的桌子还小,过道也很窄。Outlook 没 Gmail 好用,我自己加了存档和星标凑合用,Outlook 跟 Lync 集成看在线状态不错,不过 Lync 没 QQ 好用。</p> <p>这边同事看起来更 geek 一点,天天玩魔方、用 Vim、看 The Old Reader 等等。这边的电脑也爽多了,下载速度跟寝室差不多,22 寸显示器。还是想吐槽点评几十K的下载速度,下个 Eclipse 都花了好几个小时,网速差都没有加班的动力啊。</p> <p>当时也可以留在点评的,面试真是个神奇的事情,当年面实习生,跟我水平差不多的同学挂了,我比较轻松地进来,都没问什么问题,转正面试也感觉还好啊,结果几乎挂掉= = 转正面的面试官让我意识到产品有多么重要,其实我花时间做得那个小工具并不是真正需要的东西,不过让我做的 PM 也没说什么嘛= = 我自己确实没想太多。</p> <p>很怀念点评那边的氛围,就像大学里的朋友一样~点评的工作环境也很赞,在创意园区里,,我觉得比百度都好,虽然百度每个人的空间大一点。组里希望把每个人培养成 full-stack 工程师。</p> <p>我觉得不好的地方</p> <ol> <li> <p>对这个东西是在没有太大兴趣= = 不想去深入了解 Spring,Struts,当时学校上课就没兴趣</p> </li> <li> <p>做业务很多人用可能很有成就感,很好玩。听过产品的分享,感觉产品的整体思路很棒,但是,我不是 PM,很多细节不够好,我觉得不能接受,不应该上线,就算是 MVP 也要把体验做到比较好才行啊。这里只说消费的一般用户用到的,比如:</p> <ol> <li> <p>刚开始的时候首页的餐厅预定时间选择是无效的,到了商户页还要再选一遍。有种被耍了的感觉</p> </li> <li> <p>6.0 还没出来的时候首页上就有下载的广告,点开会发现还是旧版</p> </li> <li> <p>这个只是我自己想的,可能产品有自己的想法。团购评价和商户评价没合并,很麻烦</p> </li> <li> <p>对了,预定抽奖的抵用券在团购里没显示,得自己输入号码。。还有细节问题,鼠标不能点的信誉值星级还是手形指针。</p> </li> </ol> </li> <li> <p>改 bug 不够快,大概十月份同学提了个bug,后来发现八月论坛上就有报告。我离职两周前提了公司地图位置标错,走的时候也没改= = 而且不够处女座啊,注意点细节好么。</p> </li> <li> <p>比较辛苦啊,我觉得每天工作到八九点,回家哪还有心情去学其他技术了,我还想学点工作上用不到的呢,天天这么写业务,我怕一年后我就开始不想写代码了。。</p> </li> <li> <p>虽然没认真看代码,但是感觉代码质量不会很高的</p> </li> </ol> <p>需求经常变,这是不可避免的= =,我还是想,能不能变得少一点?能不能在 sprint 开始前明确需求?能不能慢一点,体验做得好一点?刚好看到 Fenng 小道消息的<a href="http://zhuanlan.zhihu.com/WebNotes/19624175">《试错》</a></p> <p>最后再写下我找工作的标准吧:</p> <ol> <li> <p><strong>Do cool things that matter</strong> (via <a href="http://www.google.com/about/jobs/lifeatgoogle/do-cool-things-that-matter.html">Google</a>),这个很难定义啊,以后每天大部分时间都在工作,千万不要做得很不爽。</p> </li> <li> <p>不要太辛苦= = 点评的工作时间已经是我的极限了,不要 996 啊= =</p> </li> <li> <p>薪水</p> </li> </ol> <p>虽然没认真想过,之前的目标大概是毕业的时候有一线的互联网企业的水平吧,完全没达到啊。上半年找实习的时候发现有点难,然后就想去点评或者百姓网了。现在在携程感觉也还不错,今年应该不会再有面试了。</p> <p>十一之前注册了携程,定了个旅店,付款后第二天上午告诉我没定成,垃圾邮件总是退订失败。去哪儿有百度支持,也招很多牛人,觉得携程可能没有互联网基因吧,看起来最近转型还不错,以后就算去哪儿发展特别好,携程也能是市场第二吧。</p> <p>我 06 年就注册了点评,阿里的口碑没打过,网易、百度做得类似的东西也不行。点评只是移动端的入口比较弱,挺看好点评的。</p> <p>终于写了这篇,以后看肯定会觉得现在很幼稚。。留此存照= =</p> Skip List 的应用 2013-10-26T00:00:00+00:00 http://greenmooon55.com/skip-list <p>首先这里是 <a href="http://www.redisbook.com/en/latest/internal-datastruct/skiplist.html">skip list</a> 的介绍。</p> <h1 id="redis-sorted-set">Redis Sorted Set</h1> <p>之前用 Redis 的 Sorted Set 存在线用户。</p> <div class="language-ruby highlighter-rouge"><pre class="highlight"><code><span class="c1"># key, score, member</span> <span class="vg">$redis</span><span class="p">.</span><span class="nf">zadd</span><span class="p">(</span><span class="s1">'online-users'</span><span class="p">,</span> <span class="no">Time</span><span class="p">.</span><span class="nf">now</span><span class="p">.</span><span class="nf">to_i</span><span class="p">,</span> <span class="n">current_user</span><span class="p">.</span><span class="nf">id</span><span class="p">)</span> </code></pre> </div> <p>查询某用户最后访问的时间</p> <div class="language-ruby highlighter-rouge"><pre class="highlight"><code><span class="n">last_seen</span> <span class="o">=</span> <span class="vg">$redis</span><span class="p">.</span><span class="nf">zscore</span><span class="p">(</span><span class="s2">"online-users"</span><span class="p">,</span> <span class="nb">self</span><span class="p">.</span><span class="nf">id</span><span class="p">)</span> </code></pre> </div> <p>ZADD 的复杂度是 O(log(N)),ZSCORE 的复杂度是 O(1)。</p> <p>ZSET 是由一个 dict 和 skip list 实现的。dict 是一个根据 member 生成的哈希表,所以 ZSCORE 的复杂度是 O(1),ZADD 还需插入到 skip list 里所以需要 O(log(N))。</p> <p>这个 skip list 是根据 score 排序的,所以根据 score 找 member 的复杂度是 O(log(N)),用于 ZRANK 等命令。</p> <p><a href="http://www.redisbook.com/en/latest/datatype/sorted_set.html">有序集 — Redis 设计与实现</a></p> <h1 id="倒排索引">倒排索引</h1> <p>前几天在公司听了搜索的分享,才知道这个东西。 <a href="http://zh.wikipedia.org/wiki/%E5%80%92%E6%8E%92%E7%B4%A2%E5%BC%95">维基百科</a></p> <p>比如有两个到排索引,一个是“五角场”,另外一个是“火锅”。那在搜索“五角场 火锅”的时候就要合并这两条链,找出两条链上相同的节点。</p> <p>如果是普通的链表的话,效率是 O(M + N)。skip list 在合并时,如果另外那个表跳跃过去的节点仍然小于当前表的节点,中间的节点就不用考虑了。</p> Bash 找出最大的数 2013-10-25T00:00:00+00:00 http://greenmooon55.com/bash-command-in-interview <p>随机生成一些数据</p> <div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="c">#!/bin/bash</span> <span class="k">for </span>i <span class="k">in</span> <span class="o">{</span>1..10<span class="o">}</span> <span class="k">do </span><span class="nb">printf</span> <span class="s2">"%d</span><span class="se">\t</span><span class="s2">%d</span><span class="se">\n</span><span class="s2">"</span> <span class="nv">$i</span> <span class="nv">$RANDOM</span> <span class="k">done</span> </code></pre> </div> <div class="highlighter-rouge"><pre class="highlight"><code>1 2879 2 28792 3 91 4 23059 5 8242 6 13228 7 31060 8 12015 9 25696 10 27964 </code></pre> </div> <p>现在要找出右边第三大的数</p> <div class="highlighter-rouge"><pre class="highlight"><code>sort -k2 -n -r temp.txt | sed -n '3p' </code></pre> </div> <ul> <li>-k2 说明按第二列排序</li> <li>-n 说明当作数字排序</li> <li>-r 说明由大到小</li> </ul> <p>详见 <a href="http://explainshell.com/explain?cmd=sort+-k2+-n+-r+temp.txt+%7C+sed+-n+%273p%27">explainshell.com</a></p> <p><a href="http://coolshell.cn/articles/9104.html">sed 简明教程 酷壳 - CoolShell.cn</a></p> <p>这是百度的面试题和某公司的笔试题</p> 面试问题1 2013-10-07T00:00:00+00:00 http://greenmooon55.com/interview-1 <p>#初面</p> <h2 id="new和malloc的区别">new和malloc的区别</h2> <p>http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free</p> <table> <thead> <tr> <th>New</th> <th>Malloc</th> </tr> </thead> <tbody> <tr> <td>失败时抛出异常</td> <td>返回 NULL</td> </tr> <tr> <td>Free Store</td> <td>Heap</td> </tr> <tr> <td>可重载</td> <td> </td> </tr> </tbody> </table> <h2 id="进程静态区存了什么">进程静态区存了什么</h2> <p>全局变量和静态变量?</p> <p>Data Segment (Data(variables, string literals) + BSS + Heap)<br /> Stack<br /> Code segment</p> <p>http://en.wikipedia.org/wiki/Data_segment</p> <p>text(program code) data bss heap stack</p> <h2 id="进程和线程的区别">进程和线程的区别</h2> <p>没想到被问到这种问题…</p> <p>一个进程可能有多个线程 共享代码段、数据段</p> <p>真不知道说什么…</p> <p>##Web相关,比如(网站)安全、性能优化</p> <p>CDN、缓存、前端优化,这个当时应该多说一些</p> <h2 id="查找和排序的关系">查找和排序的关系</h2> <p>不知所云</p> <h2 id="链表和数组区别">链表和数组区别</h2> <p>略</p> <h2 id="树二叉树的有哪些应用">树、二叉树的有哪些应用?</h2> <p>树:B树 数据库</p> <p>二叉树:平衡二叉树:C++ std::map</p> <h2 id="重载和多态区别">重载和多态区别</h2> <p>override, overload, polymorphism 这几个词要搞清楚…</p> <p>http://stackoverflow.com/questions/12893907/is-polymorphism-overloading-and-overriding-are-same-concepts</p> <p>Polymorphism can be achieved through overriding.<br /> Method Overriding<br /> Method Overloading<br /> Operator Overloading</p> <h2 id="tcp-udp-区别">TCP UDP 区别</h2> <p>TCP 三次握手保证传输<br /> UDP best-effort 不保证一定发过去,开销小<br /> 视频用UDP,HTTP用UDP。</p> <p>TCP: connection oriented<br /> UDP: datagram oriented 不保证传输顺序</p> <h2 id="共30万个url每个占空间256bytesunicode任意给出一个url判断是否在里面">共30万个url,每个占空间256bytes(unicode),任意给出一个url,判断是否在里面</h2> <p>十分钟写思路</p> <p>我的想法:hash然后二分查找…当时应该问问正解怎么做…</p> <h1 id="二面">二面</h1> <h2 id="c和c有什么区别">C++和C有什么区别</h2> <p>去死吧…</p> <p>C++ 面向对象,有类的概念。<br /> C++ 有 STL,有 template,有异常处理</p> <p>然后我就说不出啥了…</p> <p>额,new/delete,运算符重载…</p> <h2 id="既然有gcjava会不会出现内存泄露什么时候会出现gc的原理">既然有GC,Java会不会出现内存泄露,什么时候会出现?GC的原理</h2> <p>不知道…</p> <p>如果一个对象 = null 了就可以被回收了吧。如果在某个循环里生成很多的对象,存到循环外的某个容器里,这样就会内存泄露吧。</p> <p>http://www.ibm.com/developerworks/cn/java/l-JavaMemoryLeak/</p> <h2 id="本专业四年学了什么-大学里最大的收获-最重要的两门专业课是什么">本专业四年学了什么 大学里最大的收获 最重要的两门专业课是什么</h2> <h2 id="有没有自己课外做的东西非实习非课程项目非外包">有没有自己课外做的东西(非实习、非课程项目、非外包)</h2> Const Reference To Pointer 2013-09-21T00:00:00+00:00 http://greenmooon55.com/const-reference-to-pointer <div class="language-cpp highlighter-rouge"><pre class="highlight"><code><span class="k">struct</span> <span class="n">mycomp</span> <span class="p">{</span> <span class="kt">bool</span> <span class="k">operator</span><span class="p">()(</span><span class="n">ListNode</span> <span class="o">*</span> <span class="k">const</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">,</span> <span class="n">ListNode</span> <span class="o">*</span> <span class="k">const</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span><span class="o">-&gt;</span><span class="n">val</span> <span class="o">&gt;</span> <span class="n">b</span><span class="o">-&gt;</span><span class="n">val</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> </code></pre> </div> <p>上学期 euyuil 曾经讲过这方面的东西,现在终于用到了。之前一直写成 <code class="highlighter-rouge">const ListNode *</code>,还不明白为什么不对。</p> <p>这里重载括号也挺有意思,这种东西叫做 functor,我还没想明白为什么不能直接传名为 mycomp 的函数过去。</p> <p><a href="http://www.learncpp.com/cpp-tutorial/99-overloading-the-parenthesis-operator/">9.9 — Overloading the parenthesis operator « Learn C++</a></p> <p>LeetCode 很不错,网页上的代码编辑器做得相当好,和本地的 CodeBlocks 差不多了。写 C++ 也想把左括号放到行尾了,可惜 CodeBlocks 自动缩进有问题。</p> MyBatis 与 MySQL Timestamp 类型的问题 2013-08-23T00:00:00+00:00 http://greenmooon55.com/mybatis-timestamp <p>正常使用时,找不到对应的记录,MyBatis 的日志是这样的。</p> <div class="highlighter-rouge"><pre class="highlight"><code>[DEBUG] 25:52/(BaseJdbcLogger.java:debug:132) ==&gt; Preparing: SELECT COUNT(id) FROM paths WHERE task_id = ? AND time = ? [DEBUG] 25:52/(BaseJdbcLogger.java:debug:132) ==&gt; Parameters: 255103(Integer), 2013-08-22 13:40:02.208(Timestamp) </code></pre> </div> <p>根据 <a href="http://dev.mysql.com/doc/refman/5.5/en/datetime.html">MySQL 的官方文档</a></p> <blockquote> <p>TIMESTAMP has a range of ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC.</p> </blockquote> <p>经过测试,把毫秒设为 0 就能正常查找了,也就是<code class="highlighter-rouge">2013-01-01 02:03:04.000</code>。</p> <p>MyBatis 挺好用的,据说公司里用得挺多的。(我还是不想写 Java = =)</p> 20130822上海地铁服务中断 2013-08-22T00:00:00+00:00 http://greenmooon55.com/130822-shmetro-service-outage <p>八点十几分进入地铁站,准备 2 换 4 到中山公园,海伦路下楼梯的时候听到广播 4 号线延误,果断返回乘坐十号线,坐到了之前那趟 10 号线后面的第二班车。耽误了 10 分钟的时间。走到十号线站台才听到这边的广播说<a href="http://www.shmetro.com/dtyy2010/wdzm_detail.jsp?id=1446">4号线故障</a>。我在换乘通道里刷了一下微博,9 分钟之前就有故障告知,“上海发布”在三分钟之前转发了。看来地铁站反应还没微博快,感觉二号线客流比平时多。</p> <p>晚上回来,南京东路站台电视上提示由于车门故障,国权路到江湾体育场运行延误。没有任何广播通知,估计是当时基本解决了。</p> <p>我有个想法是抓取“上海metro”的微博,如果有“突发运营信息”之类的词语就推送给用户。。其实安卓版上海地铁官方应用是有这个功能的,还可以选择推送的线路,不过我这边永远是空白,不知道 iOS 怎么样。</p> <p>九点多点的时候,中山公园换乘通道已经被完全封锁,凯旋路上有些打车的。中山公园站停着一辆内圈四号线,车内没有照明。</p> <p>BTW,晚上回来遇到一班终点为龙阳路的二号线,这是要收车了吧。</p> 实习感受 2013-08-19T00:00:00+00:00 http://greenmooon55.com/intern-at-dianping <p>实习两周了,虽然面试时选的是前端,入职之后发现其实主要是做 Java EE 的,对前端的要求不高,team 里没有专职的前端,大家都来写。现在仍然处于打杂阶段,都是自己一个人写小项目。</p> <h2 id="好的地方">好的地方</h2> <ul> <li> <p>环境不错,在一个创意园区</p> </li> <li> <p>氛围挺好,一个团队的人挺像学校里的同学。我们是做新业务,也稍微有点 Startup 的感觉</p> </li> <li> <p>用到了一点比较新的东西,比如内部的 GitLab,我们 team 也用到了 MongoDB。下周还有 Hackathon</p> </li> <li> <p>使用 Google Apps</p> </li> </ul> <h2 id="不好的地方">不好的地方</h2> <ul> <li> <p>好辛苦,感觉经常加班的样子,特别是我们 team,我两次在公司吃完饭,发现貌似我们组里走得都很晚,Line Manager 曾经跟我说有时会加班到晚上九十点钟,貌似周末还有人来加班。</p> </li> <li> <p>网速好慢,肯定到不了 100kb/s,这还是互联网公司?</p> </li> <li> <p>木有免费的饮料</p> </li> <li> <p>不想写 Java… 用到各种库、写配置文件,遇到很多莫名其妙的 bug</p> </li> <li> <p>工作时用到 QQ,难道不怕腾讯获取聊天记录?或许这也是公司里 Linuxer 不多的原因?</p> </li> <li> <p>或许因为赶进度代码质量不够好,上周差不多每天线上都有 bug 要去做 hotfix…</p> </li> </ul> 迁移到 Jekyll 2013-08-11T00:00:00+00:00 http://greenmooon55.com/migrating-to-jekyll <p>Wordpress 放在虚拟主机上太慢了,影响写作欲望…</p> <p>迁移进行中,预计会持续很久。。</p> Codeforces Round #191 (Div. 2) 2013-07-13T02:35:58+00:00 http://greenmooon55.com/codeforces-round-191-div-2 <p>比赛是在嘉定做的,只做了A,rating掉到历史新低= =</p> <h1 id="a-flipping-game">A. Flipping Game</h1> <p>n个0或1,从中任选i,j,翻转从i到j的数(变成x-1),求最长连续的1。由于n很小(100),直接模拟即可。</p> <h1 id="b-hungry-sequence">B. Hungry Sequence</h1> <p>输出一个递增的序列,要求任意一项不能被其他项整除。序列长度最大为10^5,每个数字不能超过10^7。</p> <p>如果全部输出质数就是符合条件的,比赛的时候脑残以为质数不够,所以乱搞的,现在看来貌似不太对。。</p> <h1 id="c-magic-five">C. Magic Five</h1> <p>给出一个数字,可以去掉任意个digit,不能全部去掉,可以有leading zero,问能有多少种情况使处理后的数字能被5整除。</p> <p>这个思路很明显,就是找5和0,然后乘以2^i,i为前面的位数。但是实现上有一些问题。</p> <p>(ans = 2^i + 2^{l+i} + 2^{2l+i} + … + 2^{(k-1)l+i})</p> <p>(ans = 2^i * (1 + 2^{l} + 2^{2l} + … + 2^{(k-1)l}))</p> <p>(1 + 2^{l} + 2^{2l} + … + 2^{(k-1)l} = \frac{2^{k*l}-1}{2^l-1} )</p> <p>现在的重点就是求出(\frac{2^{k*l}-1}{2^l-1} )</p> <p>可以根据费马小定理什么的把(\frac{a}{b} \mod{p})转换成(a* b^{p-2} \mod {p}) 但是这里有些问题,我还不太理解证明的过程,比如说费马小定理要求b与p互质,这里没法证吧= =</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXA</span> <span class="o">=</span> <span class="mi">100005</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MOD</span> <span class="o">=</span> <span class="mi">1000000007</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">pow</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">a</span><span class="p">,</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">b</span><span class="p">,</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">mod</span> <span class="o">=</span> <span class="n">MOD</span><span class="p">)</span> <span class="p">{</span> <span class="c1">//cout &lt;&lt; a &lt;&lt; " ^ " &lt;&lt; b &lt;&lt; " = "; </span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">&amp;</span> <span class="mi">1</span><span class="p">)</span> <span class="n">ans</span> <span class="o">=</span> <span class="p">(</span><span class="n">ans</span> <span class="o">*</span> <span class="n">a</span><span class="p">)</span> <span class="o">%</span> <span class="n">mod</span><span class="p">;</span> <span class="n">b</span> <span class="o">&gt;&gt;=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">a</span> <span class="o">=</span> <span class="p">(</span><span class="n">a</span> <span class="o">*</span> <span class="n">a</span><span class="p">)</span> <span class="o">%</span> <span class="n">mod</span><span class="p">;</span> <span class="p">}</span> <span class="c1">//cout &lt;&lt; ans &lt;&lt; endl; </span> <span class="k">return</span> <span class="n">ans</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">string</span> <span class="n">str</span><span class="p">;</span> <span class="kt">int</span> <span class="n">k</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">str</span> <span class="o">&gt;&gt;</span> <span class="n">k</span><span class="p">;</span> <span class="kt">int</span> <span class="n">l</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">length</span><span class="p">();</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">l</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">str</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'5'</span> <span class="o">||</span> <span class="n">str</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'0'</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">=</span> <span class="p">(</span><span class="n">ans</span> <span class="o">+</span> <span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">i</span><span class="p">))</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">ans</span> <span class="o">*</span> <span class="p">(</span><span class="n">pow</span><span class="p">((</span><span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">),</span> <span class="n">l</span><span class="p">)</span> <span class="o">%</span> <span class="n">MOD</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">MOD</span><span class="p">)</span> <span class="o">%</span> <span class="n">MOD</span> <span class="o">*</span> <span class="n">pow</span><span class="p">((</span><span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span> <span class="n">MOD</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <h1 id="d-block-tower">D. Block Tower</h1> <p>对于每个联通块,可以先全部涂成蓝色,然后只保留一个为蓝色(我选的是第一个),其他都换成红色。 刚开始我用了BFS,而且还用了deque、struct Point,结果segmentation fault = =</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">505</span><span class="p">;</span> <span class="kt">char</span> <span class="n">map</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="k">struct</span> <span class="n">Operation</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">;</span> <span class="kt">char</span> <span class="n">operation</span><span class="p">;</span> <span class="p">};</span> <span class="n">queue</span> <span class="n">ansq</span><span class="p">;</span> <span class="kt">void</span> <span class="nf">ans</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">,</span> <span class="kt">char</span> <span class="n">operation</span><span class="p">)</span> <span class="p">{</span> <span class="n">Operation</span> <span class="n">op</span><span class="p">;</span> <span class="n">op</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="p">;</span> <span class="n">op</span><span class="p">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">;</span> <span class="n">op</span><span class="p">.</span><span class="n">operation</span> <span class="o">=</span> <span class="n">operation</span><span class="p">;</span> <span class="n">ansq</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">op</span><span class="p">);</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">firsti</span><span class="p">,</span> <span class="n">firstj</span><span class="p">;</span> <span class="kt">void</span> <span class="nf">search</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span> <span class="n">map</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">]</span> <span class="o">=</span> <span class="sc">'T'</span><span class="p">;</span> <span class="n">ans</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="sc">'B'</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">map</span><span class="p">[</span><span class="n">x</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">y</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'.'</span><span class="p">)</span> <span class="n">search</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">n</span> <span class="o">&amp;&amp;</span> <span class="n">map</span><span class="p">[</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">y</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'.'</span><span class="p">)</span> <span class="n">search</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">y</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">map</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'.'</span><span class="p">)</span> <span class="n">search</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">m</span> <span class="o">&amp;&amp;</span> <span class="n">map</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'.'</span><span class="p">)</span> <span class="n">search</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="n">firsti</span> <span class="o">&amp;&amp;</span> <span class="n">y</span> <span class="o">==</span> <span class="n">firstj</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">ans</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="sc">'D'</span><span class="p">);</span> <span class="n">ans</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="sc">'R'</span><span class="p">);</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">m</span><span class="p">;);</span> <span class="n">gets</span><span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">gets</span><span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">m</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'.'</span><span class="p">)</span> <span class="p">{</span> <span class="n">firsti</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="n">firstj</span> <span class="o">=</span> <span class="n">j</span><span class="p">;</span> <span class="n">search</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ansq</span><span class="p">.</span><span class="n">size</span><span class="p">());</span> <span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="n">ansq</span><span class="p">.</span><span class="n">empty</span><span class="p">())</span> <span class="p">{</span> <span class="n">Operation</span> <span class="n">op</span> <span class="o">=</span> <span class="n">ansq</span><span class="p">.</span><span class="n">front</span><span class="p">();</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%c %d %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">op</span><span class="p">.</span><span class="n">operation</span><span class="p">,</span> <span class="n">op</span><span class="p">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">op</span><span class="p">.</span><span class="n">y</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="n">ansq</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> 130506 2013-05-06T10:55:27+00:00 http://greenmooon55.com/130506 <p>4月28号下午12306查了一下票,居然有29号的1462,果断买下,顺便买下成都到青岛的K206次,徐州换乘。</p> <p>11号线一路站着,在火车站仓促地解决午饭,然后上楼候车,上海站感觉真好,只要一进去就感觉好像没有很多人的样子。只有1462和T99在一站台,要先坐电梯回到一楼,然后再走到站台上。1462只有3节YW,貌似其它都是YZ…车上大部分是男的列车员,连广播都是男的。。卧铺始发人很少,到南京基本上满了。</p> <p>对面铺位是个生意人,她也是前一天的下午才买到票的,我感觉可能1462就是提前一天下午,每十分钟放几张票的…她到山东某地(忘了),大部分乘客都是短途的,南京还上来了一大三童鞋,徐州下车前三个人还讨论了一下大学生活及前途。。随遇而安吧,开心就好。貌似中铺也有个徐州下车的。</p> <p>本来以为神车会一路早点的,没想到是一路小晚点。貌似无锡就看到旁边站台上写的T112到站时间,不过到了点它还没到,我们先走了。然后常州终于看到T112了,DF11好破,机车号都看不清。然后神车又先走了,貌似是在丹阳被踩的。接下来,过了高资站1462正线停车。。当时感觉特别奇怪,后来龙潭站侧近侧出踩了停在正线的T112,估计是机车有故障吧,坐这车得郁闷死了,特快被神车踩。南京停车的时候又看到了,我还以为它追不上来了呢。</p> <p>南京出发晚点二十分钟吧,然后徐州之前好像依旧小晚点,徐州提前了几分钟到站,出站非常方便,下车两分钟就出站了,有点旧吉林站的感觉。</p> <p>徐州作为一个中转站是很方便的,站前有麦当劳、德克士以及豆浆店。我在车上吃过15块的盒饭,感觉比518三十块的盒饭好多了。到麦当劳买杯巧克力看书。。</p> <p>神车还是很爽的,就是这25G好破啊,速度快的时候晃得很厉害。坐神车,不挂科!话说现在应该求实习对吗?</p> <p>呆了一个多小时实在受不了了,餐厅里有点热,十点十五左右进站候车,徐州站看起来有点旧,候车室挺多的,车也好多啊,一个候车室里平均就得十分钟一趟吧,不过基本都是过路车,每趟车上车的人不多。K206大约晚点20分钟进站,我等得很急。。济局的车,车上好像都没听到四川话,上车待一会就准备睡觉,这一觉可没睡好,盛名时刻写的兖州到泰山均速43km/h,实际在泰山站停了50分钟左右。我停车的时候睡不着。。开车了才睡着。。到济南又醒了,过了淄博、潍坊车上的人很少很少,两个列车员睡在下铺…最后也早点了二十分钟左右到青岛。其中一位还吐槽说“现在的车越开越慢了”= =</p> <p>回程选择的是K878烟台到南京+G7041到安亭北。感觉胶州道路有些破旧,车站规模不比吉林西大多少。第一次坐残疾人专用车,定员57,厕所还有坐便和卫生纸,好高端。只有一个问题,就是门关不上…这车比1462平稳多了。胶济客专跟秦沈的感觉差不多,在K207上有种坐车回家的感觉…普速和动车共线,但是在胶州又分开了,普速进胶州站,动车进胶州北,貌似以前丹东到青岛的车也进过胶州北。</p> <p>上车之后基本就在睡,在淄博待避了三趟动车,晚上上下行动车都多,还看到了G243合肥到青岛,这货要在淄博待避G191北京到青岛,真可怜。。然后又睡到了济南,然后从兖州睡到滁州北,时刻表上显示蚌埠要停站半小时,不过我一点感觉都没有。。睡得很好。</p> <p>南京同站台停着K784鹰潭到上海,这车有人坐全程吗?比走沪昆多三百多公里。。出站之后走到玄武湖边,买报纸,进站吃绝味鸭血粉丝,南京站换乘挺舒服的,还有KFC和永和豆浆,在一个很偏僻的地方发5100。。</p> <p>G7041南京发车人不多,最高速度29x,一般27x,可能因为离前车太近(前车停镇江),到丹阳之前减速几次,都到了80km/h。后来人就越来越多了,在无锡追上了K784,在苏州追上了早点10分钟的T109,T109居然是SS8牵引,好吧我是看的涂装颜色。过了昆山南车上稍微超员,安亭北有几个上车的,下车的人好多啊,感觉有五十到一百人,安亭北没有人工检票口,我拿着红纸票只好跟在别人后面…</p> <p>安亭7路换安亭5路回学校,感觉不错,全程四十分钟左右。</p> <p>好吧,其实其他的没什么想写的。那几天其实很想回上海,这就是人生吧。。总是不满足,总是不珍惜现在。</p> <p>其实这些应该写在日记里的。。图片现在懒得传…</p> <p>使用 <a href="http://writings.io">writings.io</a> 编辑,强烈推荐!</p> The 10th Tongji University Collegiate Programming Contest 2013-04-05T02:23:21+00:00 http://greenmooon55.com/the-10th-tongji-university-collegiate-programming-contest <p><img src="http://greenmoon55.com/wp-content/uploads/2013/04/2013-03-31-18.07.31.jpg" alt="提交记录" /></p> <p>囧,居然不是按时间排序的。</p> <p>比赛前两天做了一些腾讯的初赛题,热热身,发现我还是很适合这种比较水的题的,写起来觉得挺好玩。</p> <p>比赛的位置非常不好。。在窗边正对着阳光,而且还没有窗帘。刚开始的时候看了A题,也就是复制粘贴那个,觉得很眼熟不过暂时没思路,然后就发现有人已经过了B,接下来就是跟着 board 做题了,没什么可说的。比赛过程中还遇到了一些问题,比如说 CodeBlocks 一直不能调试、memcpy 参数那个在前那个在后、如何从字符数组选择一些转成 String、怎么写 gcd…当然其实不用 gcd 的,J题队友推了公式,貌似思路错了。我一直没看 J 题。另外 H 题模拟也很坑。</p> <p>封榜之后过了 E(似乎应该TLE的?),最后半小时左右开始写D,貌似就是个搜索吧?最后几分钟大脑空白状态,和队友一起写完,没测样例就交了,结果TLE。</p> <p>最后应该是二等奖第一名吧,还不错,本来我想不比去年差太多就好的。我们两个第一次一起比赛,配合还不错,代码几乎都是我敲的,队友想到了A、J算法,还帮我出测试数据。</p> <p><a href="http://greenmoon55.com/tongji-university-programming-contest-2011/">两年前是这样的</a>,不知道明年还会不会过来~</p> 江城 2013-04-01T06:28:51+00:00 http://greenmooon55.com/river-town <p>刚上大学的时候发现一本新书《寻路中国》,在季风书园看了一部分觉得挺有趣的。后来发现了他写的另外一本书《江城》。前段时间看过国家地理杂志的<a href="http://ngm.nationalgeographic.com/2013/03/fuling-china/hessler-text">Fuling, China  Return to River Town</a>,过了一段时间才发现,其实这篇文章的作者就是《江城》的作者“何伟”,我这才去买了这本书。</p> <p><a href="http://book.douban.com/subject/7060185/"><img src="http://greenmoon55.com/wp-content/uploads/2013/04/rivertown.jpg" alt="江城" /></a></p> <p>以前以为,只有吉林和武汉才算<a href="http://zh.wikipedia.org/wiki/%E6%B1%9F%E5%9F%8E">江城</a>,其实很多城市都是沿江而建的,它们都可以叫做江城吧。这本书写的是1996年到1998年Peter Hessler在涪陵做志愿者教英语的一些经历,从一个外国人的视角看当时的中国。</p> <p>大部分中国人都应该听说过涪陵这个名字吧,乌江榨菜可是大家天天都吃的,话说“涪”我还读错了很久。那时的涪陵是一座很有特色的小城,三峡工程尚未完工,出行的主要方式是轮船。</p> <p>作者来到中国的那段时间正好说我上小学之前的几年,刚刚开始记事,当时的中国还没有很多外国人,我也记得邓小平去世、香港回归。</p> <blockquote> <p>我发现,我读不懂文学评论了,因为其中晦涩的学术性与作品的优美性相去甚远。对于绝大多数文学评论,我都不太明白,它们好似一团乱麻,无可救药地充塞着令人费解的字眼:解构主义、后现代主义、新历史主义。这一切统统无法得到简单而明了的解释——就像我在涪陵的这些学生,当被问到什么是历史唯物主义,什么是中国特色的社会主义的时候,只能张口结舌。</p> </blockquote> <blockquote> <p>克莱姆,出于其在华尔街工作的精明的数学头脑,记下了驾驶员摁响喇叭的总次数。在十五分钟的行车过程中,那位驾驶员一共摁响喇叭566次。每分钟37次。</p> </blockquote> <blockquote> <p>这就是中国的方式。成功乃是预期之中的,而失败则遭到批评。你要么对要么不对,这里没有中间地带。</p> </blockquote> <blockquote> <p>不过,这样的集体主义仅限于一些小团体、家庭、朋友和单位之内,这些紧密的社交圈也起到了一定的边界作用:它们既包容,又排他。对于自己熟知的圈子外的人,一般的涪陵人几乎很少表现出认同感。</p> </blockquote> <blockquote> <p>尽管对这一段历史略知一二,我还是觉得人们对三峡大坝工程缺乏兴趣和漠不关心达到了不一般的程度。现在的人们受教育的程度远胜于以往那个年代,在一定程度上,人们应该记住所经历的历史教训,防止因盲目而重蹈覆辙。然而,十分明显的是,普通民众对于三峡大坝和下游城市移民的命运完全漠不关心。</p> </blockquote> <blockquote> <p>在这本宪法里,我最喜欢的是第一章第三十二条: 中华人民共和国对于因为政治原因要求避难的外国人,可以给予受庇护的权利。</p> </blockquote> <blockquote> <p>因为我知道,中国人总是对其他地方的人抱着一种强烈的偏见。</p> </blockquote> <blockquote> <p>据说政府有一条规定,如果气温上升到三十七度,所有人都不用上班。因此官方报告的气温总是三十五度。</p> </blockquote> <blockquote> <p>外国人总是说,中国人很难搞懂,这样的断言一般来说是正确的,但在很多情况下,人们的思想高度同一,可以预测。你只要按下这几个按钮——希特勒、犹太人、日本人、鸦片战争、西藏、台湾,十有八九都可以精准地预测会有怎样的反应,甚至可以精准地预测人们会使用哪些具体的词汇。就中国的具体情况而言——大家基本上属于同一种族、闭关锁国好几百年,目前的教育制度高度单一化和政治化,这是自然而然的事情。</p> </blockquote> <blockquote> <p>“中共中央总书记、国家主席江泽民推荐观看《泰坦尼克号》,富通珠宝金行独家赞助!”</p> </blockquote> <p>这本书很有趣,翻译也很不错,有空的时候可以翻翻。。平平淡淡的生活其实并不平凡,所以世界上才会有各种纪录片、纪实文学。</p> <p>十五年过去了,中国发生了很大的变化,涪陵据说已经成为有很多高楼大厦的典型中国城市。当时何伟班上的学生(大部分来自农村)现在从事着各种各样的工作,基本都过上了“优越的生活”。(<a href="http://ngmchina.com.cn/web/?action-viewnews-itemid-199904">《华夏地理》重返江城</a>)</p> <p>觉得当时那些由志愿者上课的学生好幸福,或许学到的英美文学的比我们现在多,为什么我们不能多学点英语经典呢。突然想到高中时候的外教,已经忘了他说过什么,只记得大部分人都在下面自习…</p> <p>不知道再过十年,我会在做什么,观念会改变多少。</p> 2013腾讯编程马拉松初赛第〇场 2013-03-30T06:04:23+00:00 http://greenmooon55.com/2013%e8%85%be%e8%ae%af%e7%bc%96%e7%a8%8b%e9%a9%ac%e6%8b%89%e6%9d%be%e5%88%9d%e8%b5%9b%e7%ac%ac%e3%80%87%e5%9c%ba <p>很遗憾,连复赛都没进。。cin 超时,一直没想到改成 scanf…</p> <h1 id="小q系列故事屌丝的逆袭">小Q系列故事——屌丝的逆袭</h1> <p>略</p> <h1 id="小明系列故事买年货">小明系列故事——买年货</h1> <p>dp[i][j][l][x] 表示前i个商品,剩余金额j,剩余积分l,剩余免费商品数x。 dp[i][j][l][x] = max(dp[i-1][j][l][x], dp[i-1][j][l][x+1] + val), dp[i-1][j][l+b][x] + val, dp[i-1][j+a][l][x] + val);</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="mi">110</span><span class="p">][</span><span class="mi">110</span><span class="p">][</span><span class="mi">110</span><span class="p">][</span><span class="mi">6</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">v1</span><span class="p">,</span> <span class="n">v2</span><span class="p">,</span> <span class="n">k</span><span class="p">;</span> <span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">val</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">v1</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">v2</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">k</span><span class="p">;)</span><span class="o">!=</span> <span class="n">EOF</span><span class="p">)</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">val</span><span class="p">;);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">v1</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">l</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">l</span> <span class="o">&lt;=</span> <span class="n">v2</span><span class="p">;</span> <span class="n">l</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">x</span> <span class="o">&lt;=</span> <span class="n">k</span><span class="p">;</span> <span class="n">x</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&lt;</span> <span class="n">k</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">val</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">+</span> <span class="n">b</span> <span class="o">&lt;=</span> <span class="n">v2</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="o">+</span><span class="n">b</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">+</span> <span class="n">val</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">+</span> <span class="n">a</span> <span class="o">&lt;=</span> <span class="n">v1</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="o">+</span><span class="n">a</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">+</span> <span class="n">val</span><span class="p">);</span> <span class="c1">//printf("%d %d %d %d %d\n", i,j,l,x,dp[i][j][l][x]); </span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">dp</span><span class="p">[</span><span class="n">n</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <h1 id="吉哥系列故事临时工计划">吉哥系列故事——临时工计划</h1> <p>dp[i] 表示工作不超过第i天时获得的最多工资。先把工作按结束时间排序一下,然后你懂的。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="mi">110</span><span class="p">][</span><span class="mi">110</span><span class="p">][</span><span class="mi">110</span><span class="p">][</span><span class="mi">6</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">v1</span><span class="p">,</span> <span class="n">v2</span><span class="p">,</span> <span class="n">k</span><span class="p">;</span> <span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">val</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">v1</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">v2</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">k</span><span class="p">;)</span><span class="o">!=</span> <span class="n">EOF</span><span class="p">)</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">val</span><span class="p">;);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">v1</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">l</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">l</span> <span class="o">&lt;=</span> <span class="n">v2</span><span class="p">;</span> <span class="n">l</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">x</span> <span class="o">&lt;=</span> <span class="n">k</span><span class="p">;</span> <span class="n">x</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&lt;</span> <span class="n">k</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">val</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">+</span> <span class="n">b</span> <span class="o">&lt;=</span> <span class="n">v2</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="o">+</span><span class="n">b</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">+</span> <span class="n">val</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">+</span> <span class="n">a</span> <span class="o">&lt;=</span> <span class="n">v1</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="o">+</span><span class="n">a</span><span class="p">][</span><span class="n">l</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">+</span> <span class="n">val</span><span class="p">);</span> <span class="c1">//printf("%d %d %d %d %d\n", i,j,l,x,dp[i][j][l][x]); </span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">dp</span><span class="p">[</span><span class="n">n</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <h1 id="湫湫系列故事植树节">湫湫系列故事——植树节</h1> <p>这道题很有趣!!搜了题解才会,其实这道题是在求单色三角形的个数。详见<a href="http://noclyt.com/blog/?p=185">这里</a>吧。。懒得画图说明了。</p> <h1 id="威威猫系列故事篮球梦">威威猫系列故事——篮球梦</h1> <p>每次攻击的时候 dp[i] = dp[i-1] + dp[i-2] + dp[i-3],存的就是有多少种。。好多细节,略恶心,还有long long,如果数据不到一局且已经赢了,种数还是1。。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">t</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">&gt;&gt;</span> <span class="n">b</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">)</span> <span class="p">{</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">defence</span> <span class="o">=</span> <span class="n">t</span> <span class="o">/</span> <span class="mi">30</span><span class="p">;</span> <span class="kt">int</span> <span class="n">attack</span> <span class="o">=</span> <span class="n">t</span> <span class="o">/</span> <span class="mi">30</span> <span class="o">+</span> <span class="n">t</span> <span class="o">%</span> <span class="mi">30</span> <span class="o">/</span> <span class="mi">15</span><span class="p">;</span> <span class="n">b</span> <span class="o">+=</span> <span class="n">defence</span><span class="p">;</span> <span class="kt">int</span> <span class="n">gap</span> <span class="o">=</span> <span class="n">b</span> <span class="o">-</span> <span class="n">a</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">attack</span> <span class="o">*</span> <span class="mi">3</span> <span class="o">&lt;=</span> <span class="n">gap</span><span class="p">)</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">attack</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">attack</span> <span class="o">&lt;=</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">dp</span><span class="p">[</span><span class="mi">210</span><span class="p">];</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">attack</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">200</span><span class="p">;</span> <span class="n">j</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">-</span> <span class="mi">3</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">3</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">2</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">gap</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">200</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">sum</span> <span class="o">+=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">attack</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">sum</span> <span class="o">=</span> <span class="n">sum</span> <span class="o">*</span> <span class="mi">3</span><span class="p">;</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%I64d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">sum</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> USACO Video Game Troubles 2013-03-30T05:18:34+00:00 http://greenmooon55.com/usaco-video-game-troubles <p>BNUOJ 4140</p> <p>有很多游戏平台,每个平台上有很多游戏,每个游戏有花费和价值。</p> <p>这道题很有趣,要背包两次,先考虑要不要买某个游戏,再考虑要不要买某个游戏平台。</p> <p>看了这个题解才明白的:http://blog.sina.com.cn/s/blog_4a0c4e5d010147j3.html</p> <p>循环过每个平台所有游戏之后,再考虑购买游戏平台的花费,这时候转换为另一个背包问题,要不要买这个平台。</p> <p>dp[j][k] 表示第i个(当前平台)考虑了j个游戏,预算为k时的最大价值。dp[0][k] 就是前i个平台,当前预算为k的最优值。。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">55</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXG</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="kt">int</span> <span class="n">p</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">g</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">gp</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXG</span><span class="p">],</span> <span class="n">pv</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXG</span><span class="p">];</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="mi">100010</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">v</span><span class="p">;)</span><span class="o">!=</span><span class="n">EOF</span><span class="p">)</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">p</span><span class="p">;[</span><span class="n">i</span><span class="p">]);</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">g</span><span class="p">;[</span><span class="n">i</span><span class="p">]);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">gp</span><span class="p">;[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">],</span> <span class="o">&amp;</span><span class="n">pv</span><span class="p">;[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]);</span> <span class="p">}</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="n">v</span><span class="p">;</span> <span class="n">k</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">k</span> <span class="o">&gt;=</span> <span class="n">gp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">k</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">k</span><span class="o">-</span><span class="n">gp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]]</span> <span class="o">+</span> <span class="n">pv</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]);</span> <span class="k">else</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">k</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="n">v</span><span class="p">;</span> <span class="n">k</span> <span class="o">&gt;=</span> <span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">k</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="n">k</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">g</span><span class="p">[</span><span class="n">i</span><span class="p">]][</span><span class="n">k</span><span class="o">-</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">]]);</span> <span class="p">}</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">dp</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="n">v</span><span class="p">]);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> 130312 2013-03-12T12:33:03+00:00 http://greenmooon55.com/130312 <p>还以为标题可以是 120312 呢,那样更好玩一点。</p> <p>其实呢,这个世界挺神奇的,有的时候感觉很公平,有的时候又好像也不是。</p> <p>还记得高中班主任问什么是幸福?我说活着就是幸福。真的啊。。活到十八、二十是很幸运的事情啊,并不是必然事件嘛。。</p> <p>有的人学X很轻松,有人花了很长时间也学不会X,而如果学Y就可能完全不一样。</p> <p>其实上一句话不是重点。。所以,顺其自然吧~享受生活,享受这个世界。</p> 为什么 Kosaraju 算法一定要把所有边反向 2013-03-12T12:13:59+00:00 http://greenmooon55.com/why-reversing-all-edges-is-necessary-in-kosarajus-algorithm <p><a href="https://www.coursera.org/course/algo">算法课</a>上介绍说,<a href="http://en.wikipedia.org/wiki/Kosaraju's_algorithm">Kosaraju 算法</a>的大概步骤是</p> <ol> <li> <p>DFS反向图,记录每个点的出栈顺序</p> </li> <li> <p>逆序(出栈顺序)遍历,找到所有的强连通分量</p> </li> </ol> <p>正确性证明略…见课程 PDF…</p> <p>很自然地想到一个问题…为什么不能在原图 DFS,然后顺序遍历呢?</p> <p>想想证明里的那个例子,从强连通分量 SCC1 到 SCC2 有一条边,反向之后DFS,能保证最后出栈的点一定在 SCC2 中,因此第二次 DFS 时能找到正确的结果。如果在原图 DFS,则不能保证第一个出栈的点在哪里…呃,很简单嘛…</p> <p>比如说 A-&gt;B,B-&gt;A, A-&gt;C 这个图,如果出栈顺序是BAC就杯具了= =</p> <p>其实这个例子是网上搜到的…</p> 快排笔记 2013-02-27T11:20:18+00:00 http://greenmooon55.com/%e5%bf%ab%e6%8e%92%e7%ac%94%e8%ae%b0 <p>另一种快排的实现方法,<a href="https://www.coursera.org/course/algo">算法课</a>讲的。感觉这个挺容易懂的,都忘了以前怎么写的了= =。</p> <p>首先选择数组中第一个元素为 pivot,先不管它,引入 i, j 两个指针。从左向右扫描数组,j 分隔开扫描过的元素和未知的元素,从第二个元素到 i 之前都比 pivot 小,从 i 到 j 都比 pivot 大,当然等于 pivot 的元素放在哪边都可以啦。扫描时如果遇到比 pivot 小的元素,与 i 后面那个元素交换即可,然后再右移 i。最后交换第一个元素和 i 左边的数。</p> <p>实际代码中 a[i] 表示数组中大于 pivot 的第一个数(当然也可能还没扫到),最后和 a[i-1] 交换。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="kt">int</span> <span class="nf">partition</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">p</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="n">l</span><span class="p">];</span> <span class="c1">// pivot </span> <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">l</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="n">l</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">r</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">p</span><span class="p">)</span> <span class="p">{</span> <span class="n">swap</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span> <span class="n">i</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="n">swap</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">l</span><span class="p">],</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]);</span> <span class="k">return</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="nf">quicksort</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">p</span> <span class="o">=</span> <span class="n">partition</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">&lt;</span> <span class="n">p</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="n">quicksort</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">p</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">p</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">r</span><span class="p">)</span> <span class="n">quicksort</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">p</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="p">}</span> </code></pre> </div> <p>如果选取其他 pivot 怎么办呢?先交换第一个数和新的 pivot 就可以了。</p> TEDxFDUSalon:因为这是真的 2012-12-27T16:09:32+00:00 http://greenmooon55.com/tedxfdusalon%ef%bc%9a%e5%9b%a0%e4%b8%ba%e8%bf%99%e6%98%af%e7%9c%9f%e7%9a%84 <p>上个月去看了 TEDxFDU,拖到现在还没写点什么呢,把记下来的东西随便写写好了。</p> <p>暖场演讲有个很惊人的题目 <a href="http://v.youku.com/v_show/id_XNDkzNjc3NTA4.html">Ted is SHIT</a>,让大家思考参加活动的目的。联想自己、不要吐槽、生活中多折腾。</p> <p>一、兰启昌 http://www.lanqc.com 事实与真相,事实不等于真相,我忘了他这里怎么讲的了,感觉确实是这样。。有些记者和读者都有一些偏见 “中国逻辑”比如说:A:这酒真难喝 B:… 新闻专业主义,还没找到英文怎么说,要独立于权贵、资本、<strong>大多数人或特立独行</strong>。 他提到了两个他喜欢的人:李海鹏和吴晓波</p> <p>二、沈奕斐 社会性别:文化的力量 女主内,男主外?</p> <p>她介绍了社会性别的概念,有的时候社会对个体的“期望”和他/她自身的喜好是不一样的。</p> <p>三、Junxian Lee 授人以鱼不如授人以渔,所以他们帮助印度洋海啸受灾某地区如何晒盐,而不是直接捐款。 三个观点: follow your heart try be do have 而不是 do have be</p> <p>四、刘雨阳 又是一个物理转到软件工程的学生 他说在做校园活动海报上的二维码什么的…想到了我们的软工项目。 虚拟世界与真实世界: 指令:能量 数据:物质 《Matrix》 虚拟世界不能实现:细节(Retina也要舍弃)、随机(伪随机)</p> <p>五、贝敏 貌似是大学以后就一直靠家教养活自己,去旅行,在黄山差点丧命= =</p> <p>六、丁琦亮 PhD:拓展人类知识,想到了<a href="http://matt.might.net/articles/phd-school-in-pictures/">这个图</a>。 研究人类学,好强大,还有这门学科。</p> <p>七、龚金平 顺势而为的人生。我初中开始就想,每天开开心心的就好了嘛。</p> <p>=== 大概就记得这些有点意义。。 好像学生都是直奔主题,教师/教授都会先说:“很高兴今天来到这里…” 最后回想一下,到底有什么收获呢?好像没什么哎…只是了解了一些别人的想法,不同的工作、人生。 我感觉自己话都不会说了= =</p> <p>嘉定那个 <a href="http://v.youku.com/v_show/id_XNDkwODMxODQ4.html">ACE Project</a> 可以弄成类似的形式吧。。我还一次都没去过。想要变得更 social 一点,不能太宅了= = 英语社我也没帮上多少忙,不知道下学期怎么样呢。</p> Ruby on Rails 环境配置 2012-11-06T12:11:15+00:00 http://greenmooon55.com/install-ruby-on-rails-on-ubuntu-12-10 <p><strong>Updated @ 2013/3/21 21:21</strong></p> <ol> <li>下载 RVM (Ruby Version Manager)</li> </ol> <p>在终端里执行</p> <blockquote> <table> <tbody> <tr> <td>curl -L https://get.rvm.io</td> <td>bash -s stable</td> </tr> </tbody> </table> </blockquote> <p>可能需要先安装 curl</p> <ol> <li> <p>关闭当前打开的终端窗口,重新打开</p> </li> <li>执行</li> <li></li> </ol> <blockquote> <p>sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config</p> </blockquote> <ol> <li>安装 Ruby 2.0.0</li> </ol> <p>输入</p> <blockquote> <p>rvm install 2.0.0</p> </blockquote> <p>请耐心等待,可以出去转一圈或者睡一觉了…</p> <ol> <li> <p>点击终端的“编辑”菜单,选择“配置文件首选项”,切换到“标题和命令”选项卡,选择“以登录Shell方式运行命令”,然后重启终端</p> </li> <li> <p>输入</p> </li> </ol> <blockquote> <p>rvm 2.0.0 –default</p> </blockquote> <p>如果没有任何显示,应该是正常的。 此时输入</p> <blockquote> <p>ruby -v</p> </blockquote> <p>应该会显示 ruby 的版本号。</p> <ol> <li>由于某些原因,我们使用淘宝的 <a href="http://ruby.taobao.org/">Rubygems 镜像</a></li> </ol> <blockquote> <p>gem source -r http://rubygems.org/ gem source -a http://ruby.taobao.org</p> </blockquote> <ol> <li>安装rails</li> </ol> <blockquote> <p>gem install rails</p> </blockquote> <p>至少需要半小时吧…</p> TopCoder SRM 558 DIV 2 2012-10-22T08:33:20+00:00 http://greenmooon55.com/topcoder-srm-558-div-2 <p>先吐个嘈。。这次题目太难了吧。。第二题整个房间没人交,第三题只有两个人交,而且都 Failed System Test…</p> <p>250 略</p> <p>600 读题很重要!读题很重要!又一次写好了才发现错误。。 dp[i][j]表示盖章到第i个字符,颜色为j,最少盖几次。细节很容易错= =</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAX</span> <span class="o">=</span> <span class="mh">0x7f7f7f7f</span><span class="p">;</span> <span class="kr">inline</span> <span class="kt">int</span> <span class="n">min</span><span class="p">(</span><span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">c</span><span class="p">;)</span> <span class="p">{</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"min"</span> <span class="o">&lt;&lt;</span> <span class="n">min</span><span class="p">(</span><span class="n">min</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">),</span> <span class="n">c</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="k">return</span> <span class="n">min</span><span class="p">(</span><span class="n">min</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">),</span> <span class="n">c</span><span class="p">);</span> <span class="p">}</span> <span class="k">class</span> <span class="nc">Stamp</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="n">string</span> <span class="n">str</span><span class="p">;</span> <span class="kt">int</span> <span class="n">length</span><span class="p">;</span> <span class="kt">bool</span> <span class="n">used</span><span class="p">[</span><span class="mi">300</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">getMinimumCost</span><span class="p">(</span><span class="n">string</span> <span class="n">desiredColor</span><span class="p">,</span> <span class="kt">int</span> <span class="n">stampCost</span><span class="p">,</span> <span class="kt">int</span> <span class="n">pushCost</span><span class="p">)</span> <span class="p">{</span> <span class="n">str</span> <span class="o">=</span> <span class="n">desiredColor</span><span class="p">;</span> <span class="n">length</span> <span class="o">=</span> <span class="n">desiredColor</span><span class="p">.</span><span class="n">size</span><span class="p">();</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="mi">55</span><span class="p">][</span><span class="mi">4</span><span class="p">];</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">MAX</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">l</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">l</span> <span class="o">&lt;=</span> <span class="n">length</span><span class="p">;</span> <span class="n">l</span><span class="o">++</span><span class="p">)</span> <span class="c1">// 章的长度为l </span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">used</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">used</span><span class="p">));</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mh">0x7f</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">l</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">used</span><span class="p">[</span><span class="n">str</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">sum</span> <span class="o">=</span> <span class="n">used</span><span class="p">[</span><span class="sc">'R'</span><span class="p">]</span> <span class="o">+</span> <span class="n">used</span><span class="p">[</span><span class="sc">'G'</span><span class="p">]</span> <span class="o">+</span> <span class="n">used</span><span class="p">[</span><span class="sc">'B'</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="n">sum</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">sum</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">l</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">l</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">l</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">used</span><span class="p">[</span><span class="sc">'R'</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">l</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">used</span><span class="p">[</span><span class="sc">'G'</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">l</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">used</span><span class="p">[</span><span class="sc">'B'</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">l</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">l</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="c1">// 当前盖章到第i个字符(含) </span> <span class="p">{</span> <span class="c1">// 0???????????? </span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">l</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">i</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="c1">// 上一个章到第j个字符(含) </span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">MAX</span> <span class="o">&amp;&amp;</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="n">MAX</span> <span class="o">&amp;&amp;</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">==</span> <span class="n">MAX</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="n">memset</span><span class="p">(</span><span class="n">used</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">used</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">l</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="n">i</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">used</span><span class="p">[</span><span class="n">str</span><span class="p">[</span><span class="n">k</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">sum</span> <span class="o">=</span> <span class="n">used</span><span class="p">[</span><span class="sc">'R'</span><span class="p">]</span> <span class="o">+</span> <span class="n">used</span><span class="p">[</span><span class="sc">'G'</span><span class="p">]</span> <span class="o">+</span> <span class="n">used</span><span class="p">[</span><span class="sc">'B'</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="n">sum</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">==</span> <span class="n">i</span> <span class="o">-</span> <span class="n">l</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">2</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">3</span><span class="p">])</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">sum</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">used</span><span class="p">[</span><span class="sc">'R'</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">used</span><span class="p">[</span><span class="sc">'G'</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span> <span class="k">else</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span><span class="c1">//used['B'] </span> <span class="p">}</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">sum</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">3</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">used</span><span class="p">[</span><span class="sc">'R'</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">used</span><span class="p">[</span><span class="sc">'G'</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="k">else</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">3</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">2</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">3</span><span class="p">])</span> <span class="o">==</span> <span class="n">MAX</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="kt">int</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">stampCost</span> <span class="o">*</span> <span class="n">l</span> <span class="o">+</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">2</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">3</span><span class="p">])</span> <span class="o">*</span> <span class="n">pushCost</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">temp</span> <span class="o">&lt;</span> <span class="n">ans</span><span class="p">)</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">ans</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> </code></pre> </div> <p>900 通过<a href="http://apps.topcoder.com/forums/?module=Thread&amp;threadID=765870&amp;start=0">搜索</a>,才知道这是一个经典博弈题,叫做 <a href="http://community.topcoder.com/tc?module=Static&amp;d1=tutorials&amp;d2=algorithmGames">The Game of Nim</a>,异或一下就可以了,好神奇。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">class</span> <span class="nc">CatAndRabbit</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="n">string</span> <span class="n">getWinner</span><span class="p">(</span><span class="n">string</span> <span class="n">tiles</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">length</span> <span class="o">=</span> <span class="n">tiles</span><span class="p">.</span><span class="n">size</span><span class="p">();</span> <span class="kt">bool</span> <span class="n">hasBlack</span> <span class="o">=</span> <span class="nb">false</span><span class="p">,</span> <span class="n">hasWhite</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">tiles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'#'</span><span class="p">)</span> <span class="n">hasBlack</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">tiles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'.'</span><span class="p">)</span> <span class="n">hasWhite</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">hasBlack</span> <span class="o">||</span> <span class="o">!</span><span class="n">hasWhite</span><span class="p">)</span> <span class="k">return</span> <span class="s">"Rabbit"</span><span class="p">;</span> <span class="kt">int</span> <span class="n">s</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">tiles</span><span class="p">[</span><span class="n">s</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'#'</span><span class="p">)</span> <span class="o">++</span><span class="n">s</span><span class="p">;</span> <span class="kt">int</span> <span class="n">l</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">a</span><span class="p">[</span><span class="mi">100</span><span class="p">],</span> <span class="n">p</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">s</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">tiles</span><span class="p">[</span><span class="n">s</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'#'</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">tiles</span><span class="p">[</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'.'</span><span class="p">)</span> <span class="p">{</span> <span class="n">a</span><span class="p">[</span><span class="n">p</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">l</span><span class="p">;</span> <span class="n">l</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="o">++</span><span class="n">s</span><span class="p">;</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">else</span> <span class="o">++</span><span class="n">l</span><span class="p">;</span> <span class="o">++</span><span class="n">s</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">tiles</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="sc">'.'</span><span class="p">)</span> <span class="p">{</span> <span class="n">a</span><span class="p">[</span><span class="n">p</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">l</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">p</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">ans</span> <span class="o">^</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="n">ans</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="s">"Rabbit"</span><span class="p">;</span> <span class="k">else</span> <span class="k">return</span> <span class="s">"Cat"</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> </code></pre> </div> 苏宁退货记 2012-10-16T06:42:00+00:00 http://greenmooon55.com/%e8%8b%8f%e5%ae%81%e9%80%80%e8%b4%a7%e8%ae%b0 <p>10月1日于上海苏宁徐家汇旗舰店(肇嘉浜路店)购入三星NP530U3C-A01CN一台。当时发现屏幕亮度不均,苏宁员工认为正常。</p> <p>10月X日(似乎是五号),去徐家汇太平洋电脑一期20楼三星售后服务站,工程师认为正常。值得一提的是当时只有我一个人去检修,却等了半小时左右,理由是工程师在修昨天的机器。</p> <p>10月7日,再去三星售后,简单的说问题是电脑休眠唤醒时会进入Windows错误恢复。售后查不出问题,电脑留下来检修。</p> <p>10月9日中午,工程师来电话说他们仍然找不到问题,需要返厂检测。我说要退货,万一返厂直接修好了怎么办。。</p> <p>10月12日早9点多,工程师说他没有返厂,可以出具检测单。 下午我就去开了检测单,注明日期是7号。然后<strong>去苏宁要求换货</strong>,苏宁说当时没货但可以帮我调货,当晚可以到,让我第二天来换,卖电脑的人还给我留了电话,好像说了有货会通知我。</p> <p>10月13日早上,我没有接到电话,于是主动打电话问,<strong>苏宁说还没货,我只好去退</strong>,办了一部分手续。有些东西被我妈拿回家了,没有办法拿过来,苏宁说要我再交50元左右买个包就可以,耳机就不算了。然后说中午有些手续办不了,叫我下午再去,于是我回了趟本部。下午再去,他们复印了发票、身份证、检测单等等,然后说需要联系南京什么的,要周一晚上之后来办退款,给我写了个收条。我问了退款是退到银行卡里还是退现金,貌似她说她不清楚。</p> <p>10月16日,一直打不通留的电话,我还是去了,售货员说是她手机有问题,难道你就不能给我打个电话?最后说要付款的银行卡,已经被我妈拿走了啊。。问过两次售货员退货需要什么,都没说需要这个,其实她也不清楚。总之,<strong>没有任何人说过需要付款的银行卡</strong>。火车票退票就不用银行卡的嘛。</p> <p>于是上了地铁往回走,手机上网查了一下,打苏宁客服4008-365-365,某客服在询问后告诉我可以退货,于是我在上海体育馆下车坐回去。苏宁收银人员仍然坚持不能退且态度非常不好,于是我又打了一遍电话,电话里的客服让我把手机给收银员,然后<strong>收银员吼了客服一顿。。。最后客服和我说确实不行。。</strong>这个好好笑哦。。还管我要前一位客服的工号,我根本就没记嘛。。</p> <p>于是现在手里一张发票一张退货单,等待快递银行卡中。。已经过了退货时间,不知道还会不会有麻烦。反正我都复印/拍照了,实在不行只好找消协什么的了~</p> <p>To be continued… @十一号线马陆站 Sent from my Android…….</p> <p>在实体店买东西就是为了退换货、维修的时候方便点嘛。。</p> <p><strong>苏宁的问题</strong></p> <ol> <li> <p>应该加强对员工的培训。。说明退货需要什么,比如说身份证、付款的银行卡。</p> </li> <li> <p>不能400和门店员工说法不一样,看他们吵真好笑。。</p> </li> <li> <p>收银和某个新来的员工态度较差,后者不停地问我电脑是什么故障,很不相信我。。话说电脑要是没问题服务站会给我出检测报告吗?你当服务站是傻子啊。。</p> </li> <li> <p>整个退货流程时间太长,我12号说要换货,13号知道没货了,决定退货。就算带了银行卡当天也拿不到钱。。</p> </li> <li> <p>不能出尔反尔吧,要回赠品可以理解,但是不能先说了不要,今天又说要吧。。</p> </li> </ol> <p>本来对苏宁的印象要略好于国美的,现在。。以后肯定不会优先考虑苏宁了。</p> <p>为了退换货现在已经往返三次了,单程1个半小时,地铁要7块钱。等到银行卡到了再去,难道银行卡挂失了或者销户了就不能退款了?</p> Trainspotter badge, Level 8! 2012-09-29T15:03:50+00:00 http://greenmooon55.com/trainspotter-badge-level-8 <p><a href="https://foursquare.com/greenmoon55/badge/5039f055e4b0124e4430cd5d"><img src="http://greenmoon55.com/wp-content/uploads/2012/09/20120929224442000.png" alt="" /></a></p> <p>8月26号从昆山南坐动车回来拿到的 badge,收到邮件之后感觉很好玩,查了一下这些单词:</p> <p>MTA 是<a href="http://zh.wikipedia.org/wiki/%E7%B4%90%E7%B4%84%E5%A4%A7%E9%83%BD%E6%9C%83%E9%81%8B%E8%BC%B8%E7%BD%B2">纽约大都会运输署</a>。 Tube 是<a href="http://zh.wikipedia.org/wiki/%E4%BC%A6%E6%95%A6%E5%9C%B0%E9%93%81">伦敦地铁</a>哦,以前只知道伦敦地铁叫 Underground…伦敦可是地铁的发源地哦。 Shinkansen 当时是新干线啦,地球人都知道。 Trans-Siberian 是<a href="http://zh.wikipedia.org/wiki/%E8%A5%BF%E4%BC%AF%E5%88%A9%E4%BA%9E%E9%90%B5%E8%B7%AF">西伯利亚大铁路</a>,北京到莫斯科的国际列车就经过这里吧。</p> <p>最后很好玩,”backpackers who haven’t showered in a week.”</p> <p>原来火车迷也可以叫做 Trainspotter,其实以前一直以为邮件里这个单词是 Transporter…</p> <p>明天或后天准备体验一下22号线。</p> 120922 2012-09-22T14:56:31+00:00 http://greenmooon55.com/120922 <p>今年第一场网络赛,还好不是完全没想法,一道简单搜索写了很久,还TLE一次,另外的概率dp,xw的想法有点问题,后来有点思路我也不愿意细想了,没到五点就撤了,很不爽。。</p> 120921 2012-09-21T15:30:33+00:00 http://greenmooon55.com/120921 <p>和老妈中午昆山101+沪宁高铁去了趟苏州,再高铁回然后镇坪路换北安线回来。</p> <p>沪宁城际太火了,赶到安亭北的时候最后一张票正好被前面的人买走= = 昆山南到苏州的坑爹G上座率也是几乎100%,(全程上海到常州的,在昆山南还要待避后面的G…),苏州买票时前两班也基本没票了,去虹桥的D居然还有二等座。</p> <p>20:40左右,感觉人多果断没有坐北安跨,等到的北安线有很多空座,一小时回来,感觉不错。昆山101有点慢= =</p> <p>这次去观前街、苏州博物馆、平江路看了一圈。平江路那边有种古镇的感觉,很多小房子,其实再稍微多点店铺就好了,但是不要太多…听到苏州公交的本地报站了,苏州地铁还没坐过。</p> <p>沪宁城际现在这样都该加车了吧…人真多。</p> 120920 2012-09-20T15:41:10+00:00 http://greenmooon55.com/120920 <p>前段时间很想写blog的,后来又不想了,今天没收到OhLife的邮件,又登陆不上。在这里写写吧= =</p> <p>医院水很深,家里有这方面的人会好很多。 注意身体多休息 这学期选课真纠结,课表今天才定下来。有些课有点无聊 没去交换有点遗憾,大四去的话更影响实习吧… GC分组学习感觉怪怪的。。不过挺好的,希望能坚持到最后,自己也多学点东西 笔记本仍然纠结中。。本来看好了一个,结果京东突然涨价了,不想再挑了,要疯了。。 开始做题吧,现在完全没有信心,十一看看能不能提升点。 还有…有没有可能呢。。</p> <p>写这个好无聊。 早睡吧,多休息,都要困死了!</p> Codeforces Round #133 (Div. 2) 2012-08-15T15:43:08+00:00 http://greenmooon55.com/codeforces-round-133-div-2 <p>这次比赛很爽啊,四题,room leader。Rating 终于过 1500 了。Div. 2 专场的题目貌似要简单一些。 <strong>A. Tiling with Hexagons</strong> 分成几个平行四边形,再减去相邻平行四边形重合的面积。(a<em>b+b</em>c+c*a-(a+b+c)+1)</p> <p><strong>B. Forming Teams</strong> 这道题有个很强烈的限制条件,每个学生最多有两个敌人。于是分为三种情况,环、链和孤立的点,如果环上的结点数为奇数,这个环上一定有个点要被取下。有偶数个节点的链也可以平分为两份,有奇数个节点的链分完之后只能是某一份多一个,奇数个有奇数个节点的链的结果还是某一份多一个,再考虑孤立的点,所以其实只要考虑环,然后剩下的节点数如果是奇数就去得掉一个。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="n">map</span><span class="p">[</span><span class="mi">105</span><span class="p">][</span><span class="mi">105</span><span class="p">];</span> <span class="kt">void</span> <span class="nf">addedge</span><span class="p">(</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="n">map</span><span class="p">[</span><span class="n">a</span><span class="p">][</span><span class="o">++</span><span class="n">map</span><span class="p">[</span><span class="n">a</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">ans</span><span class="p">;</span> <span class="kt">int</span> <span class="n">visited</span><span class="p">[</span><span class="mi">105</span><span class="p">];</span> <span class="kt">void</span> <span class="nf">search</span><span class="p">(</span><span class="kt">int</span> <span class="n">node</span><span class="p">,</span> <span class="kt">int</span> <span class="n">depth</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">visited</span><span class="p">[</span><span class="n">node</span><span class="p">])</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">depth</span> <span class="o">-</span> <span class="n">visited</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">((</span><span class="n">depth</span> <span class="o">-</span> <span class="n">visited</span><span class="p">[</span><span class="n">node</span><span class="p">])</span> <span class="o">%</span> <span class="mi">2</span><span class="p">)</span> <span class="o">++</span><span class="n">ans</span><span class="p">;</span> <span class="k">return</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="k">return</span><span class="p">;</span> <span class="p">}</span> <span class="n">visited</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">depth</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">map</span><span class="p">[</span><span class="n">node</span><span class="p">][</span><span class="mi">0</span><span class="p">];</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">search</span><span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">node</span><span class="p">][</span><span class="n">i</span><span class="p">],</span> <span class="n">depth</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span> <span class="o">&gt;&gt;</span> <span class="n">m</span><span class="p">;</span> <span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">m</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">&gt;&gt;</span> <span class="n">b</span><span class="p">;</span> <span class="n">addedge</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">);</span> <span class="n">addedge</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">);</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">visited</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="n">search</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="k">if</span> <span class="p">((</span><span class="n">n</span> <span class="o">-</span> <span class="n">ans</span><span class="p">)</span> <span class="o">%</span> <span class="mi">2</span><span class="p">)</span> <span class="o">++</span><span class="n">ans</span><span class="p">;</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p><strong>C. Hiring Staff</strong> 这道题也有非常强的限制条件,(m\leq n) 且 (n \neq 1)。 写写算算就出来答案了。 第一天需要 k 个人,第 n 天需要一个,第 n+1 天需要 k-1 个,若 (m \leq n-1),到此结束。若 (m=n),第 2n 天还需要一个,到此结束。其实如果 m &gt; n,应该是第 i<em>n 天一个,第 i</em>n+1 天 k-1 个吧,(k \geq2),直到 n+m+1 天为止。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="n">a</span><span class="p">[</span><span class="mi">1000000</span><span class="p">];</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">k</span><span class="p">;</span> <span class="kt">void</span> <span class="nf">work</span><span class="p">()</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">k</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="n">a</span><span class="p">[</span><span class="n">count</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="n">i</span> <span class="o">+=</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">k</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">a</span><span class="p">[</span><span class="n">count</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">a</span><span class="p">[</span><span class="n">count</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">a</span><span class="p">[</span><span class="n">count</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">&lt;=</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">==</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="n">a</span><span class="p">[</span><span class="n">count</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">n</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span> <span class="k">return</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">&gt;</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">n</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="nb">true</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="n">m</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&gt;=</span> <span class="n">i</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span> <span class="n">a</span><span class="p">[</span><span class="n">count</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="n">m</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&gt;=</span> <span class="n">i</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">;</span> <span class="n">a</span><span class="p">[</span><span class="n">count</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="n">i</span> <span class="o">+=</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span> <span class="o">&gt;&gt;</span> <span class="n">m</span> <span class="o">&gt;&gt;</span> <span class="n">k</span><span class="p">;</span> <span class="n">work</span><span class="p">();</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">count</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">" "</span> <span class="o">&lt;&lt;</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p><strong>D. Spider’s Web</strong> 这题最难的地方在于读题。对于每个 sector 处理一下就可以了。 每个 sector 可能有 (10^5) 个 bridge,bridge 的总数不超过 (10^5),开数组存不下,只好用 vector 啦。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">1010</span><span class="p">;</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">vector</span> <span class="n">num</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">void</span> <span class="nf">work</span><span class="p">(</span><span class="kt">int</span> <span class="n">cur</span><span class="p">,</span> <span class="kt">int</span> <span class="n">prev</span><span class="p">,</span> <span class="kt">int</span> <span class="n">next</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">pcur</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">pp</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">pn</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">num</span><span class="p">[</span><span class="n">prev</span><span class="p">][</span><span class="n">pp</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">num</span><span class="p">[</span><span class="n">cur</span><span class="p">][</span><span class="n">pcur</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="n">pp</span> <span class="o">&lt;</span> <span class="n">num</span><span class="p">[</span><span class="n">prev</span><span class="p">].</span><span class="n">size</span><span class="p">())</span> <span class="o">++</span><span class="n">pp</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">num</span><span class="p">[</span><span class="n">next</span><span class="p">][</span><span class="n">pn</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">num</span><span class="p">[</span><span class="n">cur</span><span class="p">][</span><span class="n">pcur</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="n">pn</span> <span class="o">&lt;</span> <span class="n">num</span><span class="p">[</span><span class="n">next</span><span class="p">].</span><span class="n">size</span><span class="p">())</span> <span class="o">++</span><span class="n">pn</span><span class="p">;</span> <span class="o">++</span><span class="n">pcur</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">pcur</span> <span class="o">&lt;</span> <span class="n">num</span><span class="p">[</span><span class="n">cur</span><span class="p">].</span><span class="n">size</span><span class="p">())</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">oldpp</span> <span class="o">=</span> <span class="n">pp</span><span class="p">,</span> <span class="n">oldpn</span> <span class="o">=</span> <span class="n">pn</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">num</span><span class="p">[</span><span class="n">prev</span><span class="p">][</span><span class="n">pp</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">num</span><span class="p">[</span><span class="n">cur</span><span class="p">][</span><span class="n">pcur</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="n">pp</span> <span class="o">&lt;</span> <span class="n">num</span><span class="p">[</span><span class="n">prev</span><span class="p">].</span><span class="n">size</span><span class="p">())</span> <span class="o">++</span><span class="n">pp</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">num</span><span class="p">[</span><span class="n">next</span><span class="p">][</span><span class="n">pn</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">num</span><span class="p">[</span><span class="n">cur</span><span class="p">][</span><span class="n">pcur</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="n">pn</span> <span class="o">&lt;</span> <span class="n">num</span><span class="p">[</span><span class="n">next</span><span class="p">].</span><span class="n">size</span><span class="p">())</span> <span class="o">++</span><span class="n">pn</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">pp</span> <span class="o">-</span> <span class="n">oldpp</span> <span class="o">!=</span> <span class="n">pn</span> <span class="o">-</span> <span class="n">oldpn</span><span class="p">)</span> <span class="o">++</span><span class="n">ans</span><span class="p">;</span> <span class="o">++</span><span class="n">pcur</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">temp</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">k</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">k</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">temp</span><span class="p">;</span> <span class="n">num</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">push_back</span><span class="p">(</span><span class="n">temp</span><span class="p">);</span> <span class="p">}</span> <span class="n">sort</span><span class="p">(</span><span class="n">num</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">begin</span><span class="p">(),</span> <span class="n">num</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">end</span><span class="p">());</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">work</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="n">work</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span> <span class="n">work</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> Codeforces Round #132 (Div. 2) 2012-08-14T02:34:36+00:00 http://greenmooon55.com/codeforces-round-132-div-2 <p><strong>A. Bicycle Chain</strong> 略</p> <p><strong>B. Olympic Medal</strong> 可以很容易地推出公式,有些变量直接用double了,用int相乘会溢出。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">5010</span><span class="p">;</span> <span class="kt">double</span> <span class="n">x</span><span class="p">[</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">y</span><span class="p">[</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">z</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">k</span><span class="p">;</span> <span class="kt">double</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">m</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">m</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">k</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">k</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">z</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">&gt;&gt;</span> <span class="n">b</span><span class="p">;</span> <span class="n">sort</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="n">n</span><span class="p">);</span> <span class="n">sort</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">z</span> <span class="o">+</span> <span class="n">k</span><span class="p">);</span> <span class="kt">double</span> <span class="n">r1</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">];</span> <span class="kt">double</span> <span class="n">p2</span> <span class="o">=</span> <span class="n">z</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="kt">double</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">m</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">double</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">b</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">r1</span> <span class="o">*</span> <span class="n">r1</span> <span class="o">/</span> <span class="p">(</span><span class="n">a</span> <span class="o">*</span> <span class="n">p2</span> <span class="o">+</span> <span class="n">b</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]));</span> <span class="k">if</span> <span class="p">(</span><span class="n">temp</span> <span class="o">&gt;=</span> <span class="n">r1</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="n">temp</span><span class="p">);</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%.10lf</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ans</span><span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p><strong>C. Crosses</strong> 这题折腾死我了。。Virtual paticipation的时候我猜是个DP,完全没思路。看了题解才知道,是枚举包含Cross的最小矩形,然后算一下四角需要去掉多大面积,枚举某方向的边长,到sqrt(area)即可,全部枚举会超时。如果是矩形,a,b和c,d可以交换位置,所以算两种情况。这里要注意一下。。我有点晕,WA好多次。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">s</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span> <span class="o">&gt;&gt;</span> <span class="n">m</span> <span class="o">&gt;&gt;</span> <span class="n">s</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span> <span class="o">+=</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">m</span><span class="p">;</span> <span class="n">j</span> <span class="o">+=</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">s</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">v</span> <span class="o">=</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">m</span> <span class="o">-</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="n">j</span> <span class="o">==</span> <span class="n">s</span><span class="p">)</span> <span class="p">{</span> <span class="n">v</span> <span class="o">*=</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">i</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">j</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">area</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">j</span> <span class="o">-</span> <span class="n">s</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">area</span> <span class="o">%</span> <span class="mi">4</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="n">area</span> <span class="o">/=</span> <span class="mi">4</span><span class="p">;</span> <span class="kt">int</span> <span class="n">temp</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">length</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">area</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="n">length</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">area</span> <span class="o">%</span> <span class="n">k</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">&lt;</span> <span class="n">i</span> <span class="o">&amp;&amp;</span> <span class="n">area</span> <span class="o">/</span> <span class="n">k</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">&lt;</span> <span class="n">j</span><span class="p">)</span> <span class="n">temp</span><span class="o">++</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">&lt;</span> <span class="n">j</span> <span class="o">&amp;&amp;</span> <span class="n">area</span> <span class="o">/</span> <span class="n">k</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">&lt;</span> <span class="n">i</span><span class="p">)</span> <span class="n">temp</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="n">length</span> <span class="o">==</span> <span class="n">area</span><span class="p">)</span> <span class="k">if</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">&lt;</span> <span class="n">i</span> <span class="o">&amp;&amp;</span> <span class="n">length</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">&lt;</span> <span class="n">j</span><span class="p">)</span> <span class="n">temp</span><span class="o">--</span><span class="p">;</span> <span class="n">v</span> <span class="o">*=</span> <span class="n">temp</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span> <span class="p">}</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">v</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p><strong>D. Hot Days</strong> 这题的题目描述好复杂,当时看了好久才懂。第一反应是三分法,后来一想,只有两种可能嘛,一种是有足够多的车,没有“罚款”,另外一种是只有一辆车,可能有罚款。如果车的数量不够多,罚的钱和只有一辆车是一样的。注意一定会罚款的情况,因为这个WA一次= =</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">t</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">cost</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span> <span class="o">&gt;&gt;</span> <span class="n">m</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">n</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span> <span class="o">&gt;&gt;</span> <span class="n">T</span> <span class="o">&gt;&gt;</span> <span class="n">x</span> <span class="o">&gt;&gt;</span> <span class="n">cost</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">T</span> <span class="o">&lt;=</span> <span class="n">t</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">cost</span> <span class="o">+</span> <span class="n">m</span> <span class="o">*</span> <span class="n">x</span><span class="p">;</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">count</span> <span class="o">=</span> <span class="n">ceil</span><span class="p">(</span><span class="n">m</span> <span class="o">/</span> <span class="p">(</span><span class="kt">double</span><span class="p">)(</span><span class="n">T</span> <span class="o">-</span> <span class="n">t</span><span class="p">));</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">t1</span> <span class="o">=</span> <span class="n">count</span> <span class="o">*</span> <span class="n">cost</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">t2</span> <span class="o">=</span> <span class="n">cost</span> <span class="o">+</span> <span class="n">m</span> <span class="o">*</span> <span class="n">x</span><span class="p">;</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">min</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span> <span class="n">t2</span><span class="p">);</span> <span class="p">}</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> 2012 Multi-University Training Contest 6 2012-08-13T11:00:58+00:00 http://greenmooon55.com/2012-multi-university-training-contest-6 <p>官方题解:<a href="http://page.renren.com/601081183/note/865145486">2012 Multi-University Training Contest 6 Solution</a> 1001 <strong>Card</strong> int length = r - l + 1; 循环节的长度一定是LCM(r, length) / length,然后就好做了吧…代码里把lcm写成lcd了。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kr">inline</span> <span class="kt">int</span> <span class="nf">gcd</span><span class="p">(</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">b</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">t</span> <span class="o">=</span> <span class="n">a</span> <span class="o">%</span> <span class="n">b</span><span class="p">;</span> <span class="n">a</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="n">t</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">a</span><span class="p">;</span> <span class="p">}</span> <span class="kr">inline</span> <span class="kt">long</span> <span class="kt">long</span> <span class="nf">lcd</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">a</span><span class="p">,</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="n">b</span> <span class="o">/</span> <span class="n">gcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">;</span> <span class="kt">int</span> <span class="n">card</span><span class="p">[</span><span class="mi">55</span><span class="p">];</span> <span class="kt">int</span> <span class="n">N</span> <span class="o">=</span> <span class="mi">52</span><span class="p">;</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">t</span><span class="p">;);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">caseno</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">caseno</span> <span class="o">&lt;=</span> <span class="n">t</span><span class="p">;</span> <span class="n">caseno</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">N</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">card</span><span class="p">;[</span><span class="n">i</span><span class="p">]);</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d%d"</span><span class="p">,</span><span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">l</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">r</span><span class="p">;);</span> <span class="kt">int</span> <span class="n">length</span> <span class="o">=</span> <span class="n">r</span> <span class="o">-</span> <span class="n">l</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="kt">int</span> <span class="n">loop</span> <span class="o">=</span> <span class="n">lcd</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">length</span><span class="p">)</span> <span class="o">/</span> <span class="n">length</span><span class="p">;</span> <span class="c1">// 循环节长度 </span> <span class="kt">int</span> <span class="n">shift</span> <span class="o">=</span> <span class="n">n</span> <span class="o">%</span> <span class="n">loop</span> <span class="o">*</span> <span class="n">length</span> <span class="o">%</span> <span class="n">r</span><span class="p">;</span> <span class="n">shift</span> <span class="o">=</span> <span class="n">r</span> <span class="o">-</span> <span class="n">shift</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case #%d:"</span><span class="p">,</span> <span class="n">caseno</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">shift</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">r</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">printf</span><span class="p">(</span><span class="s">" %d"</span><span class="p">,</span> <span class="n">card</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">shift</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">printf</span><span class="p">(</span><span class="s">" %d"</span><span class="p">,</span> <span class="n">card</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">r</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">N</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">printf</span><span class="p">(</span><span class="s">" %d"</span><span class="p">,</span> <span class="n">card</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1006 <strong>Party All the Time</strong> 对于每个精灵,unhappiness都是与选定的聚会地相关的<a href="http://zh.wikipedia.org/zh-cn/%E5%87%B8%E5%87%BD%E6%95%B0">凹函数</a>,而多个凹函数相加一定还是凹函数(貌似根据定义就可证),所以可以使用三分法。似乎这道题会有<a href="http://blog.ac521.org/?p=526">精度问题</a>。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">50010</span><span class="p">;</span> <span class="k">const</span> <span class="kt">double</span> <span class="n">eps</span> <span class="o">=</span> <span class="mf">1e-6</span><span class="p">;</span> <span class="kt">double</span> <span class="n">x</span><span class="p">[</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">w</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">n</span><span class="p">;</span> <span class="kt">double</span> <span class="nf">calc</span><span class="p">(</span><span class="kt">double</span> <span class="n">pos</span><span class="p">)</span> <span class="p">{</span> <span class="kt">double</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">w</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">pow</span><span class="p">(</span><span class="n">fabs</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">pos</span><span class="p">),</span> <span class="mi">3</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="n">ans</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">;</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">t</span><span class="p">;);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">caseno</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">caseno</span> <span class="o">&lt;=</span> <span class="n">t</span><span class="p">;</span> <span class="n">caseno</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%lf%lf"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">;[</span><span class="n">i</span><span class="p">],</span> <span class="o">&amp;</span><span class="n">w</span><span class="p">;[</span><span class="n">i</span><span class="p">]);</span> <span class="kt">double</span> <span class="n">left</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">right</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">];</span> <span class="kt">double</span> <span class="n">leftmid</span><span class="p">,</span> <span class="n">rightmid</span><span class="p">;</span> <span class="kt">double</span> <span class="n">leftvalue</span><span class="p">,</span> <span class="n">rightvalue</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">eps</span> <span class="o">&lt;</span> <span class="n">right</span><span class="p">)</span> <span class="p">{</span> <span class="n">leftmid</span> <span class="o">=</span> <span class="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="n">rightmid</span> <span class="o">=</span> <span class="p">(</span><span class="n">leftmid</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="n">leftvalue</span> <span class="o">=</span> <span class="n">calc</span><span class="p">(</span><span class="n">leftmid</span><span class="p">);</span> <span class="n">rightvalue</span> <span class="o">=</span> <span class="n">calc</span><span class="p">(</span><span class="n">rightmid</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">leftvalue</span> <span class="o">&lt;</span> <span class="n">rightvalue</span><span class="p">)</span> <span class="n">right</span> <span class="o">=</span> <span class="n">rightmid</span><span class="p">;</span> <span class="k">else</span> <span class="n">left</span> <span class="o">=</span> <span class="n">leftmid</span><span class="p">;</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case #%d: %.0lf</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">caseno</span><span class="p">,</span> <span class="n">calc</span><span class="p">(</span><span class="n">left</span><span class="p">));</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1008 <strong>String change</strong> 看官方题解吧。。这思路太神奇了。</p> <p>1010 <strong>Easy Tree DP?</strong> 题中条件的意思是,左右子树中最大的元素一定在右子树。 搜到一篇题解,遗憾的是现在找不到了。。dp[i][j]表示用了i个节点,高度<strong>小于等于</strong>j的方案数。 dp[i][j] = i * (dp[i-1][j-1] * 2 + c[i-2][k] * dp[k][j-1] * dp[i-k-1][j-1]),k为左子树的节点数。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">365</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MOD</span> <span class="o">=</span> <span class="mi">1000000007</span><span class="p">;</span> <span class="kt">int</span> <span class="n">c</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">dp</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">freopen</span><span class="p">(</span><span class="s">"test.in"</span><span class="p">,</span> <span class="s">"r"</span><span class="p">,</span> <span class="n">stdin</span><span class="p">);</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">d</span><span class="p">;</span> <span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">MAXN</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">i</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="n">j</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">c</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">360</span><span class="p">;</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">%=</span> <span class="n">MOD</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">2</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">*</span> <span class="n">dp</span><span class="p">[</span><span class="n">k</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">%</span> <span class="n">MOD</span> <span class="o">*</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="o">-</span><span class="n">k</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">%=</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">*=</span> <span class="n">i</span><span class="p">;</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">%=</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">t</span><span class="p">;);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">caseno</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">caseno</span> <span class="o">&lt;=</span> <span class="n">t</span><span class="p">;</span> <span class="n">caseno</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">d</span><span class="p">;);</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case #%d: %lld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">caseno</span><span class="p">,</span> <span class="p">((</span><span class="n">dp</span><span class="p">[</span><span class="n">n</span><span class="p">][</span><span class="n">d</span><span class="p">]</span> <span class="o">-</span> <span class="n">dp</span><span class="p">[</span><span class="n">n</span><span class="p">][</span><span class="n">d</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span> <span class="o">+</span> <span class="n">MOD</span><span class="p">)</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> 2012 Multi-University Training Contest 5 2012-08-13T10:19:23+00:00 http://greenmooon55.com/2012-multi-university-training-contest-5 <p>官方题解:<a href="http://page.renren.com/601081183/note/864816900">2012 Multi-University Training Contest 5 Solution</a></p> <p>1003 <strong>Gold miner</strong> 分组背包,详见《<a href="http://cuitianyi.com/blog/%E3%80%8A%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E4%B9%9D%E8%AE%B2%E3%80%8B2-0-alpha1/">背包问题九讲</a>》。 用点积判断是否在一条直线上。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">210</span><span class="p">;</span> <span class="k">struct</span> <span class="n">gold</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">v</span><span class="p">;</span> <span class="p">}</span><span class="n">g</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="mi">40010</span><span class="p">];</span> <span class="kt">bool</span> <span class="nf">sameGroup</span><span class="p">(</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">g</span><span class="p">[</span><span class="n">a</span><span class="p">].</span><span class="n">x</span> <span class="o">*</span> <span class="n">g</span><span class="p">[</span><span class="n">b</span><span class="p">].</span><span class="n">y</span> <span class="o">==</span> <span class="n">g</span><span class="p">[</span><span class="n">b</span><span class="p">].</span><span class="n">x</span> <span class="o">*</span> <span class="n">g</span><span class="p">[</span><span class="n">a</span><span class="p">].</span><span class="n">y</span><span class="p">;</span> <span class="p">}</span> <span class="kt">bool</span> <span class="n">comp</span><span class="p">(</span><span class="k">const</span> <span class="n">gold</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="n">gold</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">y</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">.</span><span class="n">y</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">t</span><span class="p">;</span> <span class="kt">int</span> <span class="n">group</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">caseno</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">t</span><span class="p">;)</span><span class="o">!=</span> <span class="n">EOF</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">groupCount</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">memset</span><span class="p">(</span><span class="n">group</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">group</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">g</span><span class="p">;[</span><span class="n">i</span><span class="p">].</span><span class="n">x</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">g</span><span class="p">;[</span><span class="n">i</span><span class="p">].</span><span class="n">y</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">g</span><span class="p">;[</span><span class="n">i</span><span class="p">].</span><span class="n">t</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">g</span><span class="p">;[</span><span class="n">i</span><span class="p">].</span><span class="n">v</span><span class="p">);</span> <span class="n">sort</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">g</span> <span class="o">+</span> <span class="n">n</span><span class="p">,</span> <span class="n">comp</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">bool</span> <span class="n">found</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">groupCount</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">sameGroup</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">group</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">1</span><span class="p">]))</span> <span class="p">{</span> <span class="n">gold</span><span class="o">*</span> <span class="n">prev</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">g</span><span class="p">;[</span><span class="n">group</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">group</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">0</span><span class="p">]]];</span> <span class="o">++</span><span class="n">group</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">0</span><span class="p">];</span> <span class="n">group</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">group</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="n">g</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">t</span> <span class="o">+=</span> <span class="n">prev</span><span class="o">-&gt;</span><span class="n">t</span><span class="p">;</span> <span class="n">g</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">v</span> <span class="o">+=</span> <span class="n">prev</span><span class="o">-&gt;</span><span class="n">v</span><span class="p">;</span> <span class="n">found</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">found</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="o">++</span><span class="n">groupCount</span><span class="p">;</span> <span class="o">++</span><span class="n">group</span><span class="p">[</span><span class="n">groupCount</span><span class="p">][</span><span class="mi">0</span><span class="p">];</span> <span class="n">group</span><span class="p">[</span><span class="n">groupCount</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="p">}</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">groupCount</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">time</span> <span class="o">=</span> <span class="n">t</span><span class="p">;</span> <span class="n">time</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">time</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">group</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">];</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="c1">//if (time - g[group[i][j]].t &lt; 0) break; </span> <span class="k">if</span> <span class="p">(</span><span class="n">time</span> <span class="o">-</span> <span class="n">g</span><span class="p">[</span><span class="n">group</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]].</span><span class="n">t</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">time</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">time</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">time</span> <span class="o">-</span> <span class="n">g</span><span class="p">[</span><span class="n">group</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]].</span><span class="n">t</span><span class="p">]</span> <span class="o">+</span> <span class="n">g</span><span class="p">[</span><span class="n">group</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]].</span><span class="n">v</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case %d: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="o">++</span><span class="n">caseno</span><span class="p">,</span> <span class="n">dp</span><span class="p">[</span><span class="n">t</span><span class="p">]);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1004 <strong>History repeat itself</strong> 不停地求sqrt(N),得到比N小的新平方数的个数,然后加上这个数,直到找不到新的平方数为止。此时正好有N个非平方数(新加的数都是平方数)。然后可以很方便地推出公式求和。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">t</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;);</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">m</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">add</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">m</span><span class="p">);</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">add</span><span class="p">)</span> <span class="p">{</span> <span class="n">m</span> <span class="o">+=</span> <span class="n">add</span><span class="p">;</span> <span class="n">count</span> <span class="o">+=</span> <span class="n">add</span><span class="p">;</span> <span class="n">add</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="n">count</span><span class="p">;</span> <span class="p">}</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">m</span> <span class="o">&lt;&lt;</span> <span class="s">" "</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">x</span> <span class="o">=</span> <span class="n">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">3</span><span class="o">+</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="p">(</span><span class="n">m</span> <span class="o">-</span> <span class="n">count</span><span class="o">*</span><span class="n">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">count</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1007 <strong>Permutation</strong> 很容易想到这道题其实就是求,一些数相加和为N,它们的最小公倍数有多少种情况。遗憾的是没往下想,直接想DP没思路。 官方题解:由于1不影响最小公倍数,问题转化为相加小于等于N的若干正整数的最小公倍数的可能数。 再考虑,如果两个数不互质,最小公倍数与少一些因子变得互质的情况相同。所以只要考虑这些正整数是质数即可,注意每个数可能出现多次。 dp[i][j]表示前i种质数,构成和为j有多少种情况。 dp[i][j] = dp[i-1][j] + dp[i-1][j-k*prime[i]]</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">dp</span><span class="p">[</span><span class="mi">1010</span><span class="p">][</span><span class="mi">1010</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">primeCount</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">p</span><span class="p">[</span><span class="mi">1000</span><span class="p">];</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">1010</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">s</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">i</span><span class="p">);</span> <span class="kt">bool</span> <span class="n">prime</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">s</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="n">j</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">prime</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">prime</span><span class="p">)</span> <span class="n">p</span><span class="p">[</span><span class="o">++</span><span class="n">primeCount</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">temp</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;)</span> <span class="o">!=</span> <span class="n">EOF</span><span class="p">)</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">primeCount</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">primeCount</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">];</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span> <span class="n">j</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">];</span> <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="n">temp</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span> <span class="n">j</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">-</span> <span class="n">temp</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="n">j</span> <span class="o">-</span> <span class="n">temp</span><span class="p">];</span> <span class="p">}</span> <span class="n">temp</span> <span class="o">*=</span> <span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1011 <strong>Xiao Ming’s Hope</strong> 找规律,这题我用cin居然超时,不知道为什么。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;)</span> <span class="o">!=</span> <span class="n">EOF</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">do</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="mi">1</span> <span class="o">&amp;</span> <span class="n">n</span><span class="p">)</span> <span class="o">++</span><span class="n">count</span><span class="p">;</span> <span class="n">n</span> <span class="o">&gt;&gt;=</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">while</span><span class="p">(</span><span class="n">n</span><span class="p">);</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="mi">1</span> <span class="o">&lt;&lt;</span> <span class="n">count</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> TopCoder SRM 551 DIV 2 2012-08-08T06:06:05+00:00 http://greenmooon55.com/topcoder-srm-551-div-2 <p>250 略</p> <p>500 枚举中心,然后两边往中间移,当时想错了= =</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">class</span> <span class="nc">ColorfulChocolates</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="kt">int</span> <span class="n">maximumSpread</span><span class="p">(</span><span class="n">string</span> <span class="n">chocolates</span><span class="p">,</span> <span class="kt">int</span> <span class="n">maxSwaps</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">chocolates</span><span class="p">.</span><span class="n">length</span><span class="p">();</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">left</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="kt">int</span> <span class="n">leftCount</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">rightCount</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">right</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="kt">char</span> <span class="n">color</span> <span class="o">=</span> <span class="n">chocolates</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="kt">int</span> <span class="n">curSwaps</span> <span class="o">=</span> <span class="n">maxSwaps</span><span class="p">;</span> <span class="kt">int</span> <span class="n">dis</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">left</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">right</span> <span class="o">&lt;</span> <span class="n">chocolates</span><span class="p">.</span><span class="n">length</span><span class="p">())</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">left</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">chocolates</span><span class="p">[</span><span class="n">left</span><span class="p">]</span> <span class="o">==</span> <span class="n">color</span><span class="p">)</span> <span class="p">{</span> <span class="n">dis</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">leftCount</span> <span class="o">-</span> <span class="n">left</span><span class="p">;</span> <span class="n">leftCount</span><span class="o">++</span><span class="p">;</span> <span class="n">curSwaps</span> <span class="o">-=</span> <span class="n">dis</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">curSwaps</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="n">leftCount</span> <span class="o">+</span> <span class="n">rightCount</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">right</span> <span class="o">&lt;</span> <span class="n">chocolates</span><span class="p">.</span><span class="n">length</span><span class="p">()</span> <span class="o">&amp;&amp;</span> <span class="n">chocolates</span><span class="p">[</span><span class="n">right</span><span class="p">]</span> <span class="o">==</span> <span class="n">color</span><span class="p">)</span> <span class="p">{</span> <span class="n">dis</span> <span class="o">=</span> <span class="n">right</span> <span class="o">-</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">rightCount</span><span class="p">);</span> <span class="n">rightCount</span><span class="o">++</span><span class="p">;</span> <span class="n">curSwaps</span> <span class="o">-=</span> <span class="n">dis</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">curSwaps</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="n">leftCount</span> <span class="o">+</span> <span class="n">rightCount</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="o">--</span><span class="n">left</span><span class="p">;</span> <span class="o">++</span><span class="n">right</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="n">ans</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> </code></pre> </div> <p>950 DP 三种纸杯蛋糕,相邻的朋友得到的蛋糕不一样。 很容易想出dp[i][j][k][l]表示前i个朋友,发了j个A纸杯蛋糕,k个B纸杯蛋糕,第i个朋友的蛋糕种类为l。问题是怎么处理最后一个人和第一个不能相同呢,其实只要枚举第一个蛋糕是哪种,DP三次,然后处理最后一个人获得的蛋糕即可。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MOD</span> <span class="o">=</span> <span class="mi">1000000007</span><span class="p">;</span> <span class="k">class</span> <span class="nc">ColorfulCupcakesDivTwo</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="mi">55</span><span class="p">][</span><span class="mi">55</span><span class="p">][</span><span class="mi">55</span><span class="p">][</span><span class="mi">3</span><span class="p">];</span> <span class="kt">int</span> <span class="n">length</span><span class="p">,</span> <span class="n">count</span><span class="p">[</span><span class="mi">3</span><span class="p">];</span> <span class="k">public</span><span class="o">:</span> <span class="kt">void</span> <span class="n">work</span><span class="p">()</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">i</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="c1">// 用A的数量 </span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">j</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="c1">// 用B的数量 </span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">count</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">k</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">count</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="n">j</span> <span class="o">-</span> <span class="n">k</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">count</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">j</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">count</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="n">j</span> <span class="o">-</span> <span class="n">k</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">2</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">2</span><span class="p">])</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">count</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">j</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">count</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">k</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">countArrangements</span><span class="p">(</span><span class="n">string</span> <span class="n">cupcakes</span><span class="p">)</span> <span class="p">{</span> <span class="n">length</span> <span class="o">=</span> <span class="n">cupcakes</span><span class="p">.</span><span class="n">length</span><span class="p">();</span> <span class="n">memset</span><span class="p">(</span><span class="n">count</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">count</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="o">++</span><span class="n">count</span><span class="p">[</span><span class="n">cupcakes</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="sc">'A'</span><span class="p">];</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">count</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">work</span><span class="p">();</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">length</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="n">length</span> <span class="o">-</span> <span class="n">j</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">=</span> <span class="p">(</span><span class="n">ans</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="n">ans</span> <span class="o">=</span> <span class="p">(</span><span class="n">ans</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">2</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">count</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">work</span><span class="p">();</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">length</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="n">length</span> <span class="o">-</span> <span class="n">j</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">=</span> <span class="p">(</span><span class="n">ans</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="n">ans</span> <span class="o">=</span> <span class="p">(</span><span class="n">ans</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">2</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">count</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">work</span><span class="p">();</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">length</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="n">length</span> <span class="o">-</span> <span class="n">j</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">=</span> <span class="p">(</span><span class="n">ans</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="n">ans</span> <span class="o">=</span> <span class="p">(</span><span class="n">ans</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">length</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span> <span class="o">%</span> <span class="n">MOD</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="n">ans</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> </code></pre> </div> 2012 Multi-University Training Contest 4 2012-08-08T05:41:54+00:00 http://greenmooon55.com/2012-multi-university-training-contest-4 <p>官方题解:<a href="http://page.renren.com/601081183/note/864084778">http://page.renren.com/601081183/note/864084778 </a>1001 <strong>Image Recognition</strong> 从01矩阵中找四条边都是1的正方形。 按照官方题解做的。 可以预处理出每一点上下左右四个方向上全部是1的长度,用l, r, u, d表示。接下来的想法是对于每条对角线(左上到右下),判断各点(i, j)到能到达的最远点(i + min(r[i][j], d[i][j], j + min(r[i][j], d[i][j]),共min(r[i][j], d[i][j]个点里,哪些点往左上可以延伸到原来的(i, j)点,也就是(x, y)的坐标减去min(l[x][y], u[x][y])。这样是三次方的算法,会超时的。 在扫描对角线的时候记录各点能往左上延伸到哪里,相当于记录下坐标。若点(i, j)为1,定义一个区间为从点(i, j)开始到最远点(i + min(r[i][j], d[i][j], j + min(r[i][j], d[i][j])。当扫描到区间始点时,减去左上延伸到始点(i, j)以外的点的个数,扫描到区间终点时,加上左上延伸到始点以外的点的个数,这样就能算出区间内符合条件的点的个数。用线段树来维护每个点往左上能延伸到哪里,线段树的每个区间存区间内点的个数即可。复杂度O(n^2*logn)。写得好累啊。。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">1010</span><span class="p">;</span> <span class="kt">int</span> <span class="n">map</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">u</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">d</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">l</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">r</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">a</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">visit</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="k">struct</span> <span class="n">node</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">count</span><span class="p">;</span> <span class="p">}</span><span class="n">tree</span><span class="p">[</span><span class="n">MAXN</span> <span class="o">*</span> <span class="mi">4</span><span class="p">];</span> <span class="kt">void</span> <span class="nf">build</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">=</span> <span class="n">l</span><span class="p">;</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="p">;</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">!=</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">l</span> <span class="o">+</span> <span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="n">build</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">mid</span><span class="p">);</span> <span class="n">build</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">void</span> <span class="nf">insert</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="kt">int</span> <span class="n">pos</span><span class="p">,</span> <span class="kt">int</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">count</span> <span class="o">+=</span> <span class="n">value</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">==</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">+</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">pos</span> <span class="o">&lt;=</span> <span class="n">mid</span><span class="p">)</span> <span class="n">insert</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">pos</span><span class="p">,</span> <span class="n">value</span><span class="p">);</span> <span class="k">else</span> <span class="n">insert</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">pos</span><span class="p">,</span> <span class="n">value</span><span class="p">);</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">query</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">==</span> <span class="n">l</span> <span class="o">&amp;&amp;</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span> <span class="o">==</span> <span class="n">r</span><span class="p">)</span> <span class="k">return</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">count</span><span class="p">;</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">+</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">r</span> <span class="o">&lt;=</span> <span class="n">mid</span><span class="p">)</span> <span class="k">return</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">&gt;</span> <span class="n">mid</span><span class="p">)</span> <span class="k">return</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="k">return</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">mid</span><span class="p">)</span> <span class="o">+</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">work</span><span class="p">(</span><span class="kt">int</span> <span class="n">xx</span><span class="p">,</span> <span class="kt">int</span> <span class="n">yy</span><span class="p">,</span> <span class="kt">int</span> <span class="n">count</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">build</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">count</span><span class="p">);</span> <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="n">xx</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">yy</span><span class="p">;</span> <span class="c1">//memset(visit, 0, sizeof(visit)); 会超时 </span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">visit</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">min</span><span class="p">(</span><span class="n">u</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">],</span> <span class="n">l</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">])</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="n">ans</span> <span class="o">-=</span> <span class="n">query</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span> <span class="n">insert</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="mi">1</span><span class="p">);</span> <span class="kt">int</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="n">min</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">],</span> <span class="n">r</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">])</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="n">visit</span><span class="p">[</span><span class="n">temp</span><span class="p">][</span><span class="o">++</span><span class="n">visit</span><span class="p">[</span><span class="n">temp</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">visit</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">visit</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">];</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">query</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">visit</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]);</span> <span class="p">}</span> <span class="o">++</span><span class="n">x</span><span class="p">;</span> <span class="o">++</span><span class="n">y</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">ans</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">casenum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">n</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="k">while</span><span class="p">(</span><span class="n">t</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">map</span><span class="p">;[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]);</span> <span class="k">if</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="n">u</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">u</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">u</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="n">memset</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">d</span><span class="p">));</span> <span class="n">memset</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">r</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span><span class="o">--</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span> <span class="n">j</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="n">d</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">d</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">work</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">n</span> <span class="o">-</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">work</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span> <span class="o">-</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case %d: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="o">++</span><span class="n">casenum</span><span class="p">,</span> <span class="n">ans</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1004 <strong>Trouble</strong> 给出五组数,每组各取一个,问能否使和为一。 分成两组、两组、一组。前两组求所有情况,存在sum1里,中间两组存在sum2里,排序sum1、sum2,然后对于最后一组的每个数,找sum1、sum2里有没有两个数的和为这个数的相反数。复杂度O(n^3)。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">205</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">sum1</span><span class="p">[</span><span class="n">MAXN</span><span class="o">*</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">sum2</span><span class="p">[</span><span class="n">MAXN</span><span class="o">*</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">num</span><span class="p">[</span><span class="mi">6</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">t</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">5</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%I64d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">num</span><span class="p">;[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]);</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">sum1</span><span class="p">[</span><span class="n">count</span><span class="p">]</span> <span class="o">=</span> <span class="n">num</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">num</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">];</span> <span class="n">sum2</span><span class="p">[</span><span class="n">count</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">num</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">num</span><span class="p">[</span><span class="mi">3</span><span class="p">][</span><span class="n">j</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="n">sort</span><span class="p">(</span><span class="n">sum1</span><span class="p">,</span> <span class="n">sum1</span> <span class="o">+</span> <span class="n">count</span><span class="p">);</span> <span class="n">sort</span><span class="p">(</span><span class="n">sum2</span><span class="p">,</span> <span class="n">sum2</span> <span class="o">+</span> <span class="n">count</span><span class="p">,</span> <span class="n">greater</span><span class="p">());</span> <span class="kt">bool</span> <span class="n">found</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">goal</span> <span class="o">=</span> <span class="o">-</span><span class="n">num</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">];</span> <span class="kt">int</span> <span class="n">p1</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">p2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">p1</span> <span class="o">&lt;</span> <span class="n">count</span> <span class="o">&amp;&amp;</span> <span class="n">p2</span> <span class="o">&lt;</span> <span class="n">count</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">sum1</span><span class="p">[</span><span class="n">p1</span><span class="p">]</span> <span class="o">+</span> <span class="n">sum2</span><span class="p">[</span><span class="n">p2</span><span class="p">]</span> <span class="o">==</span> <span class="n">goal</span><span class="p">)</span> <span class="p">{</span> <span class="n">found</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">sum1</span><span class="p">[</span><span class="n">p1</span><span class="p">]</span> <span class="o">+</span> <span class="n">sum2</span><span class="p">[</span><span class="n">p2</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">goal</span><span class="p">)</span> <span class="n">p1</span><span class="o">++</span><span class="p">;</span> <span class="k">else</span> <span class="n">p2</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">found</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">found</span><span class="p">)</span> <span class="n">puts</span><span class="p">(</span><span class="s">"Yes"</span><span class="p">);</span> <span class="k">else</span> <span class="n">puts</span><span class="p">(</span><span class="s">"No"</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1009 <strong>Query</strong> 给出两个字符串,随时可能修改某个字符,找出从任意字符开始,最长字符相同的长度。 这题如果想到怎么建线段树就很好做了,可惜当时想不到。记录区间里第一个不同的字符的位置。找所求位置到最后第一个不同字符的位置即可。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXL</span> <span class="o">=</span> <span class="mi">1000010</span><span class="p">;</span> <span class="kt">char</span> <span class="n">s1</span><span class="p">[</span><span class="n">MAXL</span><span class="p">],</span> <span class="n">s2</span><span class="p">[</span><span class="n">MAXL</span><span class="p">];</span> <span class="k">struct</span> <span class="n">node</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">pos</span><span class="p">;</span> <span class="p">}</span><span class="n">tree</span><span class="p">[</span><span class="n">MAXL</span> <span class="o">*</span> <span class="mi">4</span><span class="p">];</span> <span class="kt">int</span> <span class="n">length</span><span class="p">;</span> <span class="kr">inline</span> <span class="kt">int</span> <span class="n">getleft</span><span class="p">(</span><span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">return</span> <span class="n">b</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">return</span> <span class="n">a</span><span class="p">;</span> <span class="k">return</span> <span class="n">min</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">);</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">build</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">=</span> <span class="n">l</span><span class="p">;</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">&lt;</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">l</span> <span class="o">+</span> <span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="n">build</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">mid</span><span class="p">);</span> <span class="n">build</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">pos</span> <span class="o">=</span> <span class="n">getleft</span><span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="o">*</span><span class="mi">2</span><span class="p">].</span><span class="n">pos</span><span class="p">,</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="o">*</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">].</span><span class="n">pos</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">s1</span><span class="p">[</span><span class="n">l</span><span class="p">]</span> <span class="o">==</span> <span class="n">s2</span><span class="p">[</span><span class="n">l</span><span class="p">])</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">pos</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">pos</span> <span class="o">=</span> <span class="n">l</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">query</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">==</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">&amp;&amp;</span> <span class="n">r</span> <span class="o">==</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="k">return</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">pos</span><span class="p">;</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">+</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">r</span> <span class="o">&lt;=</span> <span class="n">mid</span><span class="p">)</span> <span class="k">return</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">&gt;</span> <span class="n">mid</span><span class="p">)</span> <span class="k">return</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">l</span> <span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="k">return</span> <span class="n">getleft</span><span class="p">(</span><span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">mid</span><span class="p">),</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">));</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">modify</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="kt">int</span> <span class="n">pos</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">==</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">s1</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">==</span> <span class="n">s2</span><span class="p">[</span><span class="n">pos</span><span class="p">])</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">pos</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">pos</span> <span class="o">=</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">+</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">pos</span> <span class="o">&lt;=</span> <span class="n">mid</span><span class="p">)</span> <span class="n">modify</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">pos</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">pos</span> <span class="o">&gt;</span> <span class="n">mid</span><span class="p">)</span> <span class="n">modify</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">pos</span><span class="p">);</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">pos</span> <span class="o">=</span> <span class="n">getleft</span><span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="o">*</span><span class="mi">2</span><span class="p">].</span><span class="n">pos</span><span class="p">,</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="o">*</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">].</span><span class="n">pos</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">caseno</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">pos</span><span class="p">;</span> <span class="kt">char</span> <span class="n">ch</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="kt">char</span> <span class="n">useless</span><span class="p">[</span><span class="mi">10</span><span class="p">];</span> <span class="k">while</span> <span class="p">(</span><span class="n">t</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case %d:</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="o">++</span><span class="n">caseno</span><span class="p">);</span> <span class="n">gets</span><span class="p">(</span><span class="n">useless</span><span class="p">);</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%s"</span><span class="p">,</span> <span class="n">s1</span><span class="p">);</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%s"</span><span class="p">,</span> <span class="n">s2</span><span class="p">);</span> <span class="n">length</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">s1</span><span class="p">),</span> <span class="n">strlen</span><span class="p">(</span><span class="n">s2</span><span class="p">));</span> <span class="c1">//cout &lt;&lt; length &lt;&lt; endl; </span> <span class="n">build</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">q</span><span class="p">;);</span> <span class="k">while</span> <span class="p">(</span><span class="n">q</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">action</span><span class="p">;);</span> <span class="k">if</span> <span class="p">(</span><span class="n">action</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d %d %c"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">pos</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">ch</span><span class="p">;);</span> <span class="k">if</span> <span class="p">(</span><span class="n">pos</span> <span class="o">&gt;=</span> <span class="n">length</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">s1</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">==</span> <span class="n">ch</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">s1</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">!=</span> <span class="n">s2</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="n">ch</span> <span class="o">!=</span> <span class="n">s2</span><span class="p">[</span><span class="n">pos</span><span class="p">])</span> <span class="p">{</span> <span class="n">s1</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">=</span> <span class="n">ch</span><span class="p">;</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="n">s1</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">=</span> <span class="n">ch</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">s2</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">==</span> <span class="n">ch</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">s1</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">!=</span> <span class="n">s2</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="n">ch</span> <span class="o">!=</span> <span class="n">s1</span><span class="p">[</span><span class="n">pos</span><span class="p">])</span> <span class="p">{</span> <span class="n">s2</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">=</span> <span class="n">ch</span><span class="p">;</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="n">s2</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">=</span> <span class="n">ch</span><span class="p">;</span> <span class="p">}</span> <span class="n">modify</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">pos</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pos</span><span class="p">;);</span> <span class="kt">int</span> <span class="n">result</span> <span class="o">=</span> <span class="n">query</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">pos</span><span class="p">,</span> <span class="n">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">result</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">result</span> <span class="o">-</span> <span class="n">pos</span><span class="p">);</span> <span class="k">else</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">length</span> <span class="o">-</span> <span class="n">pos</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> 2012 Multi-University Training Contest 3 2012-08-08T04:09:17+00:00 http://greenmooon55.com/2012-multi-university-training-contest-3 <p>官方题解:<a href="http://blog.renren.com/blog/601081183/863771603">http://blog.renren.com/blog/601081183/863771603</a></p> <p>1001 <strong>Arcane Numbers 1</strong> 问任意一个A进制有限小数,能否用B进制有限小数表示。 A进制小数可以转换为X*A^(-n),n为小数部分位数,这个数除以任意次B^(-i),i从1递增,直到得到一个整数,也就是说,如果A的质因子在B中都包含即可。注意A的某个质因子可能大于sqrt(A),这道题就因为这个WA到最后= =</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">1000010</span><span class="p">;</span> <span class="kt">int</span> <span class="n">prime</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">bool</span> <span class="n">notprime</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">primecount</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">void</span> <span class="nf">getprime</span><span class="p">()</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">notprime</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">notprime</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">MAXN</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">notprime</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="p">{</span> <span class="n">prime</span><span class="p">[</span><span class="n">primecount</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span> <span class="o">*</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">MAXN</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">notprime</span><span class="p">[</span><span class="n">i</span> <span class="o">*</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="n">getprime</span><span class="p">();</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">tt</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">tt</span> <span class="o">&lt;=</span> <span class="n">t</span><span class="p">;</span> <span class="n">tt</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case #%d: "</span><span class="p">,</span> <span class="n">tt</span><span class="p">);</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">&gt;&gt;</span> <span class="n">b</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">b</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">puts</span><span class="p">(</span><span class="s">"NO"</span><span class="p">);</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span> <span class="o">&amp;&amp;</span> <span class="n">a</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">puts</span><span class="p">(</span><span class="s">"YES"</span><span class="p">);</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">&lt;=</span> <span class="mi">1</span> <span class="o">||</span> <span class="n">b</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="n">puts</span><span class="p">(</span><span class="s">"NO"</span><span class="p">);</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">end</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">);</span> <span class="kt">bool</span> <span class="n">ok</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">primecount</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">prime</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">end</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">%</span> <span class="n">prime</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">%</span> <span class="n">prime</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">ok</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="n">a</span> <span class="o">%</span> <span class="n">prime</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">a</span> <span class="o">/=</span> <span class="n">prime</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">%</span> <span class="n">a</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="n">ok</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">ok</span><span class="p">)</span> <span class="n">puts</span><span class="p">(</span><span class="s">"YES"</span><span class="p">);</span> <span class="k">else</span> <span class="n">puts</span><span class="p">(</span><span class="s">"NO"</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1004 <strong>Magic Number</strong> 求字符串间的<a href="http://zh.wikipedia.org/zh-cn/%E7%B7%A8%E8%BC%AF%E8%B7%9D%E9%9B%A2">编辑距离</a> dp[i][j]可以表示为把第一个字符串前i个字符转换为第二个字符串前j个字符需要的操作数。 dp[i][j] = min(dp[i-1][j]+1(删除原串第i个字符), dp[i][j-1]+1(插入新串第j个字符), dp[i-1][j-1]+(oldstr[i]==newstr[j])?0:1(修改))</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">char</span> <span class="n">magic</span><span class="p">[</span><span class="mi">1510</span><span class="p">][</span><span class="mi">15</span><span class="p">];</span> <span class="kt">char</span> <span class="n">query</span><span class="p">[</span><span class="mi">15</span><span class="p">];</span> <span class="kr">inline</span> <span class="kt">int</span> <span class="n">min</span><span class="p">(</span><span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">c</span><span class="p">;)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">)</span> <span class="k">return</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">c</span> <span class="o">?</span> <span class="n">a</span> <span class="o">:</span> <span class="n">c</span><span class="p">;</span> <span class="k">else</span> <span class="k">return</span> <span class="n">b</span> <span class="o">&lt;</span> <span class="n">c</span> <span class="o">?</span> <span class="n">b</span> <span class="o">:</span> <span class="n">c</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="kt">int</span> <span class="n">f</span><span class="p">[</span><span class="mi">15</span><span class="p">][</span><span class="mi">15</span><span class="p">];</span> <span class="kt">int</span> <span class="n">limit</span><span class="p">;</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">t</span><span class="p">;);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">tt</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">tt</span> <span class="o">&lt;=</span> <span class="n">t</span><span class="p">;</span> <span class="n">tt</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">m</span><span class="p">;);</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case #%d:</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">tt</span><span class="p">);</span> <span class="n">getchar</span><span class="p">();</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">gets</span><span class="p">(</span><span class="n">magic</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">ii</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">ii</span> <span class="o">&lt;</span> <span class="n">m</span><span class="p">;</span> <span class="n">ii</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%s%d"</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">limit</span><span class="p">;);</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">jj</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">jj</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">jj</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="n">strlen</span><span class="p">(</span><span class="n">query</span><span class="p">);</span> <span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="n">strlen</span><span class="p">(</span><span class="n">magic</span><span class="p">[</span><span class="n">jj</span><span class="p">]);</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">x</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">y</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">j</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">x</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">y</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">((</span><span class="n">query</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">magic</span><span class="p">[</span><span class="n">jj</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span><span class="o">?</span> <span class="mi">0</span><span class="o">:</span><span class="mi">1</span><span class="p">));</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">limit</span><span class="p">)</span> <span class="o">++</span><span class="n">ans</span><span class="p">;</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ans</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1005 <strong>Triangle LOVE</strong> 给出一个<a href="http://en.wikipedia.org/wiki/Tournament_(graph_theory)">竞赛图</a>(由无向完全图给每条边加方向构成),问有没有长度为3的环。 方法一,从任意一点出发遍历整个图,搜的时候记录深度,找环。<a href="http://euyuil.com/2687/codeforces-117c-cycle/">http://euyuil.com/2687/codeforces-117c-cycle/</a> 官方题解里的增量算法有点晕。。 竞赛图上如果有环,则一定存在长度为3的环。因为若有长度为n的环,一定有长度为n-1的环,画一下就懂了。所以只要DFS判断有没有环即可。 最简单的想法:若存在两个点出度相同则存在长度为3的环。先找出两个点A和B,若A到B有边,此时A出度为1,B出度为0。对于其他任意一点C,有以下四种情况:AB出度都加一,AB出度不变、A出度加一,B出度不变、A出度不变,B出度加一。要想使AB出度相同,必须有B的出度加一、C的出度不变的情况,B到C、C到A有边才可以。这样ABC就形成了一个环。 神奇的图论啊!</p> <p>1006 <strong>Flowers</strong> 花在一段时间内开放,问某一时刻最多开多少花。 很明显,离散化区间更新的线段树,没想到这道题过的人这么多。。大家都好强啊。 把每个区间离散为三个点,如[1,4]分为[1,1],[2,3]和[4,4]即可。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">100010</span><span class="p">;</span> <span class="k">struct</span> <span class="n">flower</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">s</span><span class="p">,</span> <span class="n">t</span><span class="p">;</span> <span class="p">}</span><span class="n">f</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="k">struct</span> <span class="n">Node</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">;</span> <span class="kt">int</span> <span class="n">sum</span><span class="p">,</span> <span class="n">inc</span><span class="p">;</span> <span class="p">}</span><span class="n">tree</span><span class="p">[</span><span class="n">MAXN</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">*</span> <span class="mi">4</span><span class="p">];</span> <span class="kt">int</span> <span class="n">mycount</span><span class="p">;</span> <span class="kt">int</span> <span class="n">a</span><span class="p">[</span><span class="n">MAXN</span> <span class="o">*</span> <span class="mi">2</span><span class="p">];</span> <span class="kt">void</span> <span class="nf">build</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">=</span> <span class="n">l</span><span class="p">;</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="p">;</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">l</span> <span class="o">+</span> <span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">&lt;</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="n">build</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">mid</span><span class="p">);</span> <span class="n">build</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">convert</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">left</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">right</span> <span class="o">=</span> <span class="n">mycount</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="kt">int</span> <span class="n">mid</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">left</span> <span class="o">&lt;</span> <span class="n">right</span><span class="p">)</span> <span class="p">{</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="n">a</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span> <span class="n">left</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="n">right</span> <span class="o">=</span> <span class="n">mid</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">left</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">getsegment</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">s</span><span class="p">;,</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">t</span><span class="p">;)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">left</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">right</span> <span class="o">=</span> <span class="n">mycount</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="kt">int</span> <span class="n">mid</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">left</span> <span class="o">&lt;=</span> <span class="n">right</span><span class="p">)</span> <span class="p">{</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="n">a</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span> <span class="p">{</span> <span class="n">s</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span> <span class="n">t</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span> <span class="k">return</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="n">a</span><span class="p">[</span><span class="n">mid</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">a</span><span class="p">[</span><span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span> <span class="p">{</span> <span class="n">s</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">t</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">return</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="n">a</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span> <span class="p">{</span> <span class="n">left</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">right</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">add</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">,</span> <span class="kt">int</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">==</span> <span class="n">l</span> <span class="o">&amp;&amp;</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span> <span class="o">==</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">inc</span> <span class="o">+=</span> <span class="n">value</span><span class="p">;</span> <span class="k">return</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">+</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">&gt;</span> <span class="n">mid</span><span class="p">)</span> <span class="n">add</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">value</span><span class="p">);</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">r</span> <span class="o">&lt;=</span> <span class="n">mid</span><span class="p">)</span> <span class="n">add</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">value</span><span class="p">);</span> <span class="k">else</span> <span class="p">{</span> <span class="n">add</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span> <span class="p">,</span> <span class="n">mid</span><span class="p">,</span> <span class="n">value</span><span class="p">);</span> <span class="n">add</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">value</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">query</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">==</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">&amp;&amp;</span> <span class="n">r</span> <span class="o">==</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">sum</span> <span class="o">+</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">inc</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span> <span class="o">+</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">inc</span><span class="p">)</span> <span class="p">{</span> <span class="n">add</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">l</span><span class="p">,</span> <span class="n">mid</span><span class="p">,</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">inc</span><span class="p">);</span> <span class="n">add</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">r</span><span class="p">,</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">inc</span><span class="p">);</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">sum</span> <span class="o">+=</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">inc</span><span class="p">;</span> <span class="n">tree</span><span class="p">[</span><span class="n">num</span><span class="p">].</span><span class="n">inc</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">l</span> <span class="o">&gt;</span> <span class="n">mid</span><span class="p">)</span> <span class="k">return</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">r</span> <span class="o">&lt;=</span> <span class="n">mid</span><span class="p">)</span> <span class="k">return</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span> <span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">mid</span><span class="p">)</span> <span class="o">+</span> <span class="n">query</span><span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">;</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">tt</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">tt</span> <span class="o">&lt;=</span> <span class="n">t</span><span class="p">;</span> <span class="n">tt</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">tree</span><span class="p">));</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case #%d:</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">tt</span><span class="p">);</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span> <span class="o">&gt;&gt;</span> <span class="n">m</span><span class="p">;</span> <span class="kt">int</span> <span class="n">temp</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">;[</span><span class="n">i</span><span class="p">].</span><span class="n">s</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">;[</span><span class="n">i</span><span class="p">].</span><span class="n">t</span><span class="p">);</span> <span class="n">a</span><span class="p">[</span><span class="n">temp</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">s</span><span class="p">;</span> <span class="n">a</span><span class="p">[</span><span class="n">temp</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">t</span><span class="p">;</span> <span class="p">}</span> <span class="n">sort</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">temp</span><span class="p">);</span> <span class="n">mycount</span> <span class="o">=</span> <span class="n">unique</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">temp</span><span class="p">)</span> <span class="o">-</span> <span class="n">a</span><span class="p">;</span> <span class="n">build</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">(</span><span class="n">mycount</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">convert</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">s</span><span class="p">),</span> <span class="n">convert</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">t</span><span class="p">),</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">pos</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">m</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pos</span><span class="p">;);</span> <span class="k">if</span> <span class="p">(</span><span class="n">pos</span> <span class="o">&lt;</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">||</span> <span class="n">pos</span> <span class="o">&gt;</span> <span class="n">a</span><span class="p">[</span><span class="n">mycount</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">"0</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">s</span><span class="p">,</span> <span class="n">t</span><span class="p">;</span> <span class="n">getsegment</span><span class="p">(</span><span class="n">pos</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">t</span><span class="p">);</span> <span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">query</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">t</span><span class="p">));</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1009 <strong>Cut the cake</strong> 对于颜色相间的<strong>正方形</strong>,用dp[i][j]表示右下角为(i,j)的最大矩形边长,实时更新答案,首先判断(i,j),(i-1,j),(i,j-1)这三个点,若颜色不合法则记为1。若dp[i][j-1]等于dp[i-1][j],要判断左上角那个点的颜色是否符合。若dp[i][j-1]不等于dp[i-1][j],dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + 1。 对于颜色相同的矩形,其实就是一个找最大子矩形的问题,推荐王知昆的论文《浅谈用极大化思想解决最大子矩形问题》,用到里面第二种方法。 定义悬线为从矩形上边,上端点覆盖了一个障碍点或达到整个矩形上端的有效竖线(不覆盖任何障碍点),把悬线尽量左右移动,就可以形成一个矩形。考虑每个悬线(最多N<em>M个)最多能移动多少就能求出最大子矩形。需要left[i][j],right[i][j],height[i][j]三个数组,分别记录,往左、往右、往上能移动到哪里。从上往下扫,然后每行从左往右、从右往左各扫一遍,leftmost记录最左非障碍点位置,left[i][j] = max(left[i-1][j], leftmost),右边同理。遇到障碍点就把left[i][j]置为1,right[i][j]置为m,height[i][j]置为0。O(N</em>M)</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">1010</span><span class="p">;</span> <span class="kt">char</span> <span class="n">map</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">height</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">l</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">r</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">work</span><span class="p">(</span><span class="kt">char</span> <span class="n">ch</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">m</span><span class="p">;)</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">height</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">height</span><span class="p">));</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">leftmost</span> <span class="p">,</span> <span class="n">rightmost</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">m</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">m</span><span class="p">;</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">leftmost</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">rightmost</span> <span class="o">=</span> <span class="n">m</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">m</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">!=</span> <span class="n">ch</span><span class="p">)</span> <span class="p">{</span> <span class="n">leftmost</span> <span class="o">=</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">height</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="c1">//不影响下一行的min 和 max </span> <span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">m</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">height</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">height</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">],</span> <span class="n">leftmost</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="n">m</span><span class="p">;</span> <span class="n">j</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="n">ch</span><span class="p">)</span> <span class="p">{</span> <span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">],</span> <span class="n">rightmost</span><span class="p">);</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">-</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">height</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">])</span> <span class="o">*</span> <span class="mi">2</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">rightmost</span> <span class="o">=</span> <span class="n">j</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="n">ans</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">char</span> <span class="n">s</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">tt</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">tt</span> <span class="o">&lt;=</span> <span class="n">t</span><span class="p">;</span> <span class="n">tt</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">n</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">m</span><span class="p">;);</span> <span class="n">gets</span><span class="p">(</span><span class="n">s</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">gets</span><span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">m</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">j</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">||</span> <span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">j</span><span class="p">])</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="n">count</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="n">count</span><span class="p">]</span> <span class="o">==</span> <span class="n">map</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">])</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">count</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">else</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">ans</span><span class="p">)</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="n">ans</span> <span class="o">*=</span> <span class="mi">4</span><span class="p">;</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="n">work</span><span class="p">(</span><span class="sc">'B'</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">));</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="n">work</span><span class="p">(</span><span class="sc">'R'</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">));</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case #%d: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">tt</span><span class="p">,</span> <span class="n">ans</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>1010 <strong>MAP</strong> 我擦啊,这么简单的题意,说得这么恶心。用 map 和 set 写起来挺爽的,貌似数据都是按顺序的,用不到 map… C++ 还有 istringstream 这么神奇的东西啊。貌似这题数据是\r\n,所以。。如果gets读换行符就会被坑了= =</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span><span class="p">;</span> <span class="n">string</span> <span class="n">url</span><span class="p">;</span> <span class="n">map</span><span class="o">&lt;</span><span class="n">string</span><span class="p">,</span> <span class="kt">int</span><span class="o">&gt;</span> <span class="n">querymap</span><span class="p">;</span> <span class="n">set</span> <span class="n">urlset</span><span class="p">[</span><span class="mi">105</span><span class="p">];</span> <span class="kt">int</span> <span class="n">c</span><span class="p">[</span><span class="mi">105</span><span class="p">];</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">tt</span> <span class="o">=</span> <span class="n">t</span><span class="p">;</span> <span class="n">tt</span> <span class="o">&lt;=</span> <span class="n">t</span><span class="p">;</span> <span class="n">tt</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">string</span> <span class="n">name</span><span class="p">;</span> <span class="n">string</span> <span class="n">line</span><span class="p">;</span> <span class="n">querymap</span><span class="p">.</span><span class="n">clear</span><span class="p">();</span> <span class="n">memset</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">c</span><span class="p">));</span> <span class="kt">double</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">name</span><span class="p">;</span> <span class="n">querymap</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">pair</span><span class="o">&lt;</span><span class="n">string</span><span class="p">,</span> <span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">i</span><span class="p">));</span> <span class="n">urlset</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">clear</span><span class="p">();</span> <span class="n">getline</span><span class="p">(</span><span class="n">cin</span><span class="p">,</span> <span class="n">line</span><span class="p">);</span> <span class="n">istringstream</span> <span class="n">iss</span><span class="p">(</span><span class="n">line</span><span class="p">);</span> <span class="k">while</span> <span class="p">(</span><span class="n">iss</span> <span class="o">&gt;&gt;</span> <span class="n">url</span><span class="p">)</span> <span class="p">{</span> <span class="c1">//cout &lt;&lt; url &lt;&lt; endl; </span> <span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">++</span><span class="p">;</span> <span class="n">urlset</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">insert</span><span class="p">(</span><span class="n">url</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">name</span><span class="p">;</span> <span class="kt">int</span> <span class="n">index</span> <span class="o">=</span> <span class="n">querymap</span><span class="p">[</span><span class="n">name</span><span class="p">];</span> <span class="n">getline</span><span class="p">(</span><span class="n">cin</span><span class="p">,</span> <span class="n">line</span><span class="p">);</span> <span class="n">istringstream</span> <span class="n">iss</span><span class="p">(</span><span class="n">line</span><span class="p">);</span> <span class="kt">int</span> <span class="n">r</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">double</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">iss</span> <span class="o">&gt;&gt;</span> <span class="n">url</span><span class="p">)</span> <span class="p">{</span> <span class="o">++</span><span class="n">r</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">urlset</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">count</span><span class="p">(</span><span class="n">url</span><span class="p">))</span> <span class="p">{</span> <span class="o">++</span><span class="n">count</span><span class="p">;</span> <span class="n">sum</span> <span class="o">+=</span> <span class="n">count</span> <span class="o">/</span> <span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">r</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">sum</span> <span class="o">/</span> <span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="n">printf</span><span class="p">(</span><span class="s">"Case #%d: %.6lf</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">tt</span><span class="p">,</span> <span class="n">ans</span> <span class="o">/</span> <span class="n">n</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> Codeforces 78C Beaver Game 2012-08-06T16:08:12+00:00 http://greenmooon55.com/codeforces-78c-beaver-game <p><a href="http://codeforces.com/problemset/problem/78/C">http://codeforces.com/problemset/problem/78/C</a></p> <p>n块木头,每块长度为m,两人对战(呃,其实是beaver),每人选一块木头并把它分为<strong>长度相同</strong>的多块,每块长度不得小于k,不能继续分者输。</p> <p>貌似yds讲过类似的题,第一想法就是如果是偶数块木头,先手输,因为此时后手可以完全复制先手的策略,先手必输。如果有奇数块木头,那么先手应该选一块,把它直接分成不可分割的各小块,这样问题转化成前一种情况(此时先手相当于新情况的后手)。如果不可分割先手直接输,否则胜。</p> <p>于是从2到sqrt(m)枚举<strong>分成多少块</strong>即可,注意特殊情况:k=1时一定可分割。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">k</span><span class="p">;</span> <span class="kt">bool</span> <span class="n">split</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span> <span class="o">&gt;&gt;</span> <span class="n">m</span> <span class="o">&gt;&gt;</span> <span class="n">k</span><span class="p">;</span> <span class="kt">int</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">m</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">temp</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">%</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">m</span> <span class="o">/</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="n">k</span><span class="p">)</span> <span class="p">{</span> <span class="n">split</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">k</span> <span class="o">==</span> <span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">m</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="n">split</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">split</span> <span class="o">&amp;&amp;</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span><span class="p">)</span> <span class="n">puts</span><span class="p">(</span><span class="s">"Timur"</span><span class="p">);</span> <span class="k">else</span> <span class="n">puts</span><span class="p">(</span><span class="s">"Marsel"</span><span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>现在应该去做比赛的…刚才犹豫了一下没去做,于是睡觉去吧= =</p> ZOJ Monthly, July 2012 2012-08-04T03:53:21+00:00 http://greenmooon55.com/zoj-monthly-july-2012 <p><a href="http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=339">118 - ZOJ Monthly, July 2012</a> A <strong>Magic Number</strong> 找规律</p> <p>B <strong>Battle Ships</strong> 敌人生命值为l,我们有n个船,每个船可以造无限个,造船时间为t[i],攻击力为attack[i]。 dp[i][j]表示攻击敌人i个生命值,当前攻击力为j时的时间最小花费。 dp[i + j * t[k]][j + attack[k]] = min(dp[i + j * t[k]][j + attack[k]], dp[i][j] + t[k]) 比赛时想出来这个挺爽的,不过接下来就没出题目…</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">INF</span> <span class="o">=</span> <span class="mh">0x7f7f7f7f</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">POWERMAX</span> <span class="o">=</span> <span class="mi">330</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">l</span><span class="p">;</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="mi">350</span><span class="p">][</span><span class="mi">350</span><span class="p">];</span> <span class="kt">int</span> <span class="n">t</span><span class="p">[</span><span class="mi">50</span><span class="p">],</span> <span class="n">attack</span><span class="p">[</span><span class="mi">50</span><span class="p">];</span> <span class="k">while</span> <span class="p">(</span><span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span> <span class="o">&gt;&gt;</span> <span class="n">l</span><span class="p">)</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">127</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">t</span><span class="p">;[</span><span class="n">i</span><span class="p">],</span> <span class="o">&amp;</span><span class="n">attack</span><span class="p">;[</span><span class="n">i</span><span class="p">]);</span> <span class="p">}</span> <span class="n">dp</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">INF</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">l</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">POWERMAX</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="n">INF</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">+</span> <span class="n">attack</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">POWERMAX</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span> <span class="o">*</span> <span class="n">t</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">l</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span> <span class="o">*</span> <span class="n">t</span><span class="p">[</span><span class="n">k</span><span class="p">]][</span><span class="n">j</span> <span class="o">+</span> <span class="n">attack</span><span class="p">[</span><span class="n">k</span><span class="p">]]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span> <span class="o">*</span><span class="n">t</span><span class="p">[</span><span class="n">k</span><span class="p">]][</span><span class="n">j</span> <span class="o">+</span> <span class="n">attack</span><span class="p">[</span><span class="n">k</span><span class="p">]],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="n">t</span><span class="p">[</span><span class="n">k</span><span class="p">]);</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span> <span class="o">&gt;=</span> <span class="n">l</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="n">j</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="n">j</span><span class="p">][</span><span class="n">j</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>E <strong>Treasure Hunt I</strong> 一棵树,某人从一点出发,在限定的时间内返回,求最大收益。 简单的树形DP,这种题目很少写,非常不熟练。dp[node][j]表示<strong>刚好</strong>用j个单位时间遍历node获得的最大值,具体思路见代码,j要递减,保证dp[node][j-l-map[node][i]*2]表示从node出发,走0到i-1节点后获得的最大值,</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">105</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">INF</span> <span class="o">=</span> <span class="mh">0x7f7f7f7f</span><span class="p">;</span> <span class="kt">int</span> <span class="n">v</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">map</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="n">MAXN</span><span class="p">][</span><span class="mi">205</span><span class="p">];</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="kt">void</span> <span class="nf">dfs</span><span class="p">(</span><span class="kt">int</span> <span class="n">node</span><span class="p">)</span> <span class="p">{</span> <span class="n">dp</span><span class="p">[</span><span class="n">node</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span><span class="p">[</span><span class="n">node</span><span class="p">];</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">node</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="n">INF</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="n">dfs</span><span class="p">(</span><span class="n">i</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="n">m</span><span class="p">;</span> <span class="n">j</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">l</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">l</span> <span class="o">+</span> <span class="n">map</span><span class="p">[</span><span class="n">node</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">&lt;=</span> <span class="n">j</span><span class="p">;</span> <span class="n">l</span><span class="o">++</span><span class="p">)</span> <span class="n">dp</span><span class="p">[</span><span class="n">node</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">node</span><span class="p">][</span><span class="n">j</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">l</span><span class="p">]</span> <span class="o">+</span> <span class="n">dp</span><span class="p">[</span><span class="n">node</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="n">l</span><span class="o">-</span><span class="n">map</span><span class="p">[</span><span class="n">node</span><span class="p">][</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="mi">2</span><span class="p">]);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="k">while</span> <span class="p">(</span><span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">v</span><span class="p">;[</span><span class="n">i</span><span class="p">]);</span> <span class="kt">int</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">;</span> <span class="n">memset</span><span class="p">(</span><span class="n">map</span><span class="p">,</span> <span class="mi">127</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">map</span><span class="p">));</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">u</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">v</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">w</span><span class="p">;);</span> <span class="n">map</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="n">map</span><span class="p">[</span><span class="n">v</span><span class="p">][</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="n">w</span><span class="p">;</span> <span class="p">}</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">k</span> <span class="o">&gt;&gt;</span> <span class="n">m</span><span class="p">;</span> <span class="n">dfs</span><span class="p">(</span><span class="n">k</span><span class="p">);</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">m</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="n">dp</span><span class="p">[</span><span class="n">k</span><span class="p">][</span><span class="n">i</span><span class="p">]);</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>H <strong>Treasure Hunt IV</strong> 找规律,刚开始没发现和平方有关,交上去TLE,囧。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">bool</span> <span class="nf">work</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">x</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">sum</span> <span class="o">+=</span> <span class="n">x</span> <span class="o">/</span> <span class="n">i</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">sum</span> <span class="o">%</span> <span class="mi">2</span><span class="p">)</span> <span class="k">return</span> <span class="nb">false</span><span class="p">;</span> <span class="k">return</span> <span class="nb">true</span><span class="p">;</span> <span class="p">}</span> <span class="kt">long</span> <span class="kt">long</span> <span class="nf">getans</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">a</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="n">a</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">x</span> <span class="o">-</span> <span class="n">a</span> <span class="o">*</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">else</span> <span class="k">return</span> <span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">a</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">&gt;&gt;</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">getans</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="k">else</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">getans</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="o">-</span> <span class="n">getans</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>J <strong>Watashi’s BG</strong> 原来BG是<a href="http://zhidao.baidu.com/question/40254871">请客的意思</a>啊。物品数为30,容量为10000000的背包。 原来…搜索就可以过。两个减枝,一是要排序,二是如果后面那些都取还没有当前答案大,那就不用搜索了。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="kt">int</span> <span class="n">cost</span><span class="p">[</span><span class="mi">35</span><span class="p">];</span> <span class="kt">int</span> <span class="n">sum</span><span class="p">[</span><span class="mi">35</span><span class="p">];</span> <span class="kt">int</span> <span class="n">ans</span><span class="p">;</span> <span class="kt">void</span> <span class="nf">search</span><span class="p">(</span><span class="kt">int</span> <span class="n">p</span><span class="p">,</span> <span class="kt">int</span> <span class="n">money</span><span class="p">)</span> <span class="p">{</span> <span class="c1">//cout &lt;&lt; p &lt;&lt; " " &lt;&lt; money &lt;&lt; endl; </span> <span class="k">if</span> <span class="p">(</span><span class="n">ans</span> <span class="o">==</span> <span class="n">m</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">money</span> <span class="o">&gt;</span> <span class="n">ans</span><span class="p">)</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">money</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">p</span> <span class="o">==</span> <span class="n">n</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">sum</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="o">+</span> <span class="n">money</span> <span class="o">&lt;=</span> <span class="n">ans</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">search</span><span class="p">(</span><span class="n">p</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">money</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">money</span> <span class="o">+</span> <span class="n">cost</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">m</span><span class="p">)</span> <span class="n">search</span><span class="p">(</span><span class="n">p</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">money</span> <span class="o">+</span> <span class="n">cost</span><span class="p">[</span><span class="n">p</span><span class="p">]);</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="k">while</span> <span class="p">(</span><span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span> <span class="o">&gt;&gt;</span> <span class="n">m</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">cost</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">sort</span><span class="p">(</span><span class="n">cost</span><span class="p">,</span> <span class="n">cost</span> <span class="o">+</span> <span class="n">n</span><span class="p">,</span> <span class="n">greater</span><span class="p">());</span> <span class="n">memset</span><span class="p">(</span><span class="n">sum</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">sum</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">sum</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">sum</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">cost</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">search</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>另外一个想法,把物品分成两份,各15个,于是两侧各2^15种情况,对于每种情况在另外那边二分找最适合的,需要先排序。曾经把这个二分写错。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">55</span><span class="p">;</span> <span class="kt">int</span> <span class="n">a</span><span class="p">[</span><span class="mi">32768</span><span class="p">],</span> <span class="n">b</span><span class="p">[</span><span class="mi">32768</span><span class="p">];</span> <span class="kt">int</span> <span class="n">cost</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">calculate</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">ar</span><span class="p">,</span> <span class="kt">int</span> <span class="n">left</span><span class="p">,</span> <span class="kt">int</span> <span class="n">right</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">end</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="n">right</span><span class="o">-</span><span class="n">left</span><span class="p">)</span> <span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">ar</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">i</span><span class="p">;</span> <span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">&amp;</span> <span class="n">j</span><span class="p">)</span> <span class="n">ar</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="n">cost</span><span class="p">[</span><span class="n">left</span> <span class="o">+</span> <span class="n">count</span><span class="p">];</span> <span class="n">j</span> <span class="o">=</span> <span class="n">j</span> <span class="o">&lt;&lt;</span> <span class="mi">1</span><span class="p">;</span> <span class="o">++</span><span class="n">count</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="n">end</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">search</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">ar</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">,</span> <span class="kt">int</span> <span class="n">m</span><span class="p">)</span> <span class="p">{</span> <span class="c1">//cout &lt;&lt; l &lt;&lt; " " &lt;&lt; r &lt;&lt; " " &lt;&lt; m &lt;&lt; endl; </span> <span class="kt">int</span> <span class="n">left</span> <span class="o">=</span> <span class="n">l</span><span class="p">;</span> <span class="kt">int</span> <span class="n">right</span> <span class="o">=</span> <span class="n">r</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">left</span> <span class="o">&lt;</span> <span class="n">right</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">ar</span><span class="p">[</span><span class="n">mid</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">m</span> <span class="o">&amp;&amp;</span> <span class="n">ar</span><span class="p">[</span><span class="n">mid</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">m</span><span class="p">)</span> <span class="k">return</span> <span class="n">ar</span><span class="p">[</span><span class="n">mid</span><span class="p">];</span> <span class="k">if</span> <span class="p">(</span><span class="n">ar</span><span class="p">[</span><span class="n">mid</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">m</span><span class="p">)</span> <span class="p">{</span> <span class="n">left</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">right</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="n">ar</span><span class="p">[</span><span class="n">left</span><span class="p">];</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span> <span class="o">&gt;&gt;</span> <span class="n">m</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">cost</span><span class="p">;[</span><span class="n">i</span><span class="p">]);</span> <span class="p">}</span> <span class="n">memset</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">127</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">a</span><span class="p">));</span> <span class="n">memset</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="mi">127</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">b</span><span class="p">));</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">b</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">15</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">end</span> <span class="o">=</span> <span class="n">calculate</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span> <span class="n">sort</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">end</span><span class="p">);</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">search</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">end</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">m</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">aend</span> <span class="o">=</span> <span class="n">calculate</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">15</span><span class="p">);</span> <span class="kt">int</span> <span class="n">bend</span> <span class="o">=</span> <span class="n">calculate</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span> <span class="n">sort</span><span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">b</span> <span class="o">+</span> <span class="n">bend</span><span class="p">);</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">aend</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">m</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="n">search</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">bend</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">m</span> <span class="o">-</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">+</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span> <span class="p">}</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>也可以两边都排序,然后扫一遍就行,这种会更好写。</p> <p>K <strong>Watermelon Full of Water</strong> 买西瓜的时候一定选西瓜延续到当天并且花费最少的策略。(西瓜放这么多天不会坏吗…) 贪心、堆。用一个结构体wm存延续到哪一天last,和总花费cost,根据cost构造最小堆。堆初始化为0 0,如果延续不到“今天”就pop,然后根据堆顶元素创建新的wm,再push进去就好了。xenocide 写的代码好短。。好强大。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">50010</span><span class="p">;</span> <span class="k">struct</span> <span class="n">wm</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">last</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">cost</span><span class="p">;</span> <span class="p">};</span> <span class="kt">bool</span> <span class="k">operator</span> <span class="o">&lt;</span> <span class="p">(</span><span class="k">const</span> <span class="n">wm</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="n">wm</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">cost</span> <span class="o">&gt;</span> <span class="n">b</span><span class="p">.</span><span class="n">cost</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">;</span> <span class="kt">int</span> <span class="n">last</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="c1">// last[i] 表示第 i 天必须买瓜(从0开始) </span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">price</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="n">priority_queue</span> <span class="n">q</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">price</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">last</span><span class="p">;[</span><span class="n">i</span><span class="p">]);</span> <span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="n">q</span><span class="p">.</span><span class="n">empty</span><span class="p">())</span> <span class="n">q</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span> <span class="n">wm</span> <span class="n">temp</span><span class="p">;</span> <span class="n">temp</span><span class="p">.</span><span class="n">cost</span> <span class="o">=</span> <span class="n">temp</span><span class="p">.</span><span class="n">last</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">q</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">temp</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">while</span> <span class="p">(</span><span class="n">q</span><span class="p">.</span><span class="n">top</span><span class="p">().</span><span class="n">last</span> <span class="o">&lt;</span> <span class="n">i</span><span class="p">)</span> <span class="n">q</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">q</span><span class="p">.</span><span class="n">top</span><span class="p">();</span> <span class="n">temp</span><span class="p">.</span><span class="n">cost</span> <span class="o">+=</span> <span class="n">price</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">temp</span><span class="p">.</span><span class="n">last</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="n">last</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">q</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">temp</span><span class="p">);</span> <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="n">q</span><span class="p">.</span><span class="n">top</span><span class="p">().</span><span class="n">last</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">)</span> <span class="n">q</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">q</span><span class="p">.</span><span class="n">top</span><span class="p">().</span><span class="n">cost</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>To do: 最后一题的线段树解法是什么意思 F题应该可以做,估计会很麻烦 C题xw给我讲了比较神奇的思路,应该也可做</p> 2012 Multi-University Training Contest 2 2012-07-28T13:20:29+00:00 http://greenmooon55.com/2012-multi-university-training-contest-2 <p>官方题解:<a href="http://blog.renren.com/blog/601081183/862977450">http://blog.renren.com/blog/601081183/862977450</a></p> <p><strong>1001 Hero</strong> <a href="http://acm.hdu.edu.cn/showproblem.php?pid=4310">HDU4310</a> 每个英雄都有HP和DPS,玩家有无穷HP,DPS为1。每回合玩家选择一个英雄攻击,该英雄HP减一,同时玩家HP减去当前活着的英雄的DPS和。问如何攻击对手,损失HP最少。 官方题解上写的是状态压缩DP,其实当时看那么多人都过了,果断贪心,按DPS/HP递减排序攻击就行,我感觉这个贪心是对的吧。。</p> <blockquote> <p>用dp[mask]表示杀死mask集合的敌人时,这些敌人造成的最小hp消耗。有转移方程dp[mask] = min{dp[mask - {i}] + hp_sum[mask] * dps[i], for all i in mask}</p> </blockquote> <p>额,相当于先攻击后那个{i}。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span> <span class="o">&lt;&lt;</span> <span class="mi">20</span><span class="p">];</span> <span class="kt">int</span> <span class="nf">getdps</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">dps</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="kt">int</span> <span class="n">p</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">while</span> <span class="p">(</span><span class="n">i</span> <span class="o">&lt;=</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&amp;</span> <span class="n">i</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">dps</span><span class="p">[</span><span class="n">p</span><span class="p">];</span> <span class="p">}</span> <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">&lt;&lt;</span> <span class="mi">1</span><span class="p">;</span> <span class="o">++</span><span class="n">p</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">ans</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">n</span><span class="p">;</span> <span class="kt">int</span> <span class="n">dps</span><span class="p">[</span><span class="mi">25</span><span class="p">],</span> <span class="n">hp</span><span class="p">[</span><span class="mi">25</span><span class="p">];</span> <span class="k">while</span> <span class="p">(</span><span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="n">memset</span><span class="p">(</span><span class="n">dp</span><span class="p">,</span> <span class="mi">127</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">dp</span><span class="p">));</span> <span class="n">dp</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">dps</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;&gt;</span> <span class="n">hp</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1</span> <span class="o">&lt;&lt;</span> <span class="n">n</span><span class="p">);</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="kt">int</span> <span class="n">p</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="kt">int</span> <span class="n">dpssum</span> <span class="o">=</span> <span class="n">getdps</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">dps</span><span class="p">);</span> <span class="k">while</span> <span class="p">(</span><span class="n">j</span> <span class="o">&lt;=</span> <span class="n">i</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">j</span> <span class="o">^</span> <span class="n">i</span><span class="p">;</span> <span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="n">temp</span><span class="p">]</span> <span class="o">+</span> <span class="n">dpssum</span> <span class="o">*</span> <span class="n">hp</span><span class="p">[</span><span class="n">p</span><span class="p">]);</span> <span class="n">j</span> <span class="o">=</span> <span class="n">j</span> <span class="o">&lt;&lt;</span> <span class="mi">1</span><span class="p">;</span> <span class="o">++</span><span class="n">p</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">dp</span><span class="p">[(</span><span class="mi">1</span> <span class="o">&lt;&lt;</span> <span class="n">n</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p><strong>1002 Meeting point-1</strong> <a href="http://acm.hdu.edu.cn/showproblem.php?pid=4311">HDU4311</a> 用坐标表示每个人的住处,只能上下左右走。选择一个住处聚会,使所有人到达这里的距离和最小。 刚开始也是没思路,后来xw说,可以分别算x和y方向的距离,先按x排序,算出其他点到第一个点x方向的距离和(sumx[0]),然后递推算出每一个sumx,再按y排序,算出每个sumy。index记录到底是哪个点。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">100010</span><span class="p">;</span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">struct</span> <span class="n">point</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">index</span><span class="p">;</span> <span class="p">}</span><span class="n">px</span><span class="p">[</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">py</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">sumx</span><span class="p">[</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">sumy</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">ans</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">bool</span> <span class="n">compx</span><span class="p">(</span><span class="k">const</span> <span class="n">point</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="n">point</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">.</span><span class="n">x</span><span class="p">;</span> <span class="p">}</span> <span class="kt">bool</span> <span class="n">compy</span><span class="p">(</span><span class="k">const</span> <span class="n">point</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="n">point</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">y</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">.</span><span class="n">y</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="k">while</span><span class="p">(</span><span class="n">t</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">memset</span><span class="p">(</span><span class="n">sumx</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">sumx</span><span class="p">));</span> <span class="n">memset</span><span class="p">(</span><span class="n">sumy</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">sumy</span><span class="p">));</span> <span class="n">memset</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">ans</span><span class="p">));</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">px</span><span class="p">;[</span><span class="n">i</span><span class="p">].</span><span class="n">x</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">px</span><span class="p">;[</span><span class="n">i</span><span class="p">].</span><span class="n">y</span><span class="p">);</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">index</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="n">sort</span><span class="p">(</span><span class="n">px</span><span class="p">,</span> <span class="n">px</span><span class="o">+</span><span class="n">n</span><span class="p">,</span> <span class="n">compx</span><span class="p">);</span> <span class="n">sort</span><span class="p">(</span><span class="n">py</span><span class="p">,</span> <span class="n">py</span><span class="o">+</span><span class="n">n</span><span class="p">,</span> <span class="n">compy</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">sumx</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">x</span> <span class="o">-</span> <span class="n">px</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">x</span><span class="p">;</span> <span class="n">sumy</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">y</span> <span class="o">-</span> <span class="n">py</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">y</span><span class="p">;</span> <span class="p">}</span> <span class="n">ans</span><span class="p">[</span><span class="n">px</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sumx</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="n">ans</span><span class="p">[</span><span class="n">py</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sumy</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">sumx</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">sumx</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">x</span> <span class="o">-</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">].</span><span class="n">x</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)(</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span> <span class="o">-</span> <span class="n">n</span><span class="p">);</span> <span class="n">sumy</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">sumy</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">y</span> <span class="o">-</span> <span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">].</span><span class="n">y</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)(</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span> <span class="o">-</span> <span class="n">n</span><span class="p">);</span> <span class="c1">//sumx[i] = sumx[i-1] - (px[i].x - px[i-1].x) * (n - i) + ; </span> <span class="c1">//sumy[i] = sumy[i-1] - (py[i].y - py[i-1].y) * (n - i); </span> <span class="n">ans</span><span class="p">[</span><span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sumx</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">ans</span><span class="p">[</span><span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sumy</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="cm">/* cout &lt;&lt;"sumx" &lt;&lt; endl; for (int i = 0; i &lt; n; i++) cout &lt;&lt; sumx[i] &lt;&lt; endl; cout &lt;&lt; "sumy" &lt;&lt; endl; for (int i = 0; i &lt; n; i++) cout &lt;&lt; sumy[i] &lt;&lt; endl; */</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">bestans</span> <span class="o">=</span> <span class="mi">999999999999999LL</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">ans</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">bestans</span><span class="p">)</span> <span class="n">bestans</span> <span class="o">=</span> <span class="n">ans</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">bestans</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p><strong>Meeting point-2</strong> <a href="http://acm.hdu.edu.cn/showproblem.php?pid=4312">HDU4312</a> 和上一道题的意思基本一样,这次可以斜着走,斜着走距离仍为1而不是根号二。 看了题解才知道,原来还有<a href="http://zh.wikipedia.org/wiki/%E5%88%87%E6%AF%94%E9%9B%AA%E5%A4%AB%E8%B7%9D%E7%A6%BB">切比雪夫距离</a>、<a href="http://zh.wikipedia.org/zh-cn/%E6%9B%BC%E5%93%88%E9%A0%93%E8%B7%9D%E9%9B%A2">曼哈顿距离</a>。</p> <blockquote> <p>对于原坐标系中两点间的 Chebyshev 距离,是将坐标轴顺时针旋转45度并将所有点的坐标值放大sqrt(2)倍所得到的新坐标系中的Manhattan距离的二分之一。</p> </blockquote> <p>这个挺神奇的,现在曼哈顿距离为2的两个点在原坐标系里切比雪夫距离为1。新坐标为 (x-y, x+y),怎么算的呢?转换成极坐标然后就能算了,刚才我都忘了= =然后还用上道题的代码就可以了。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">100010</span><span class="p">;</span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">struct</span> <span class="n">point</span> <span class="p">{</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">;</span> <span class="kt">int</span> <span class="n">index</span><span class="p">;</span> <span class="p">}</span><span class="n">px</span><span class="p">[</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">py</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">sumx</span><span class="p">[</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">sumy</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">ans</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">bool</span> <span class="n">compx</span><span class="p">(</span><span class="k">const</span> <span class="n">point</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="n">point</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">.</span><span class="n">x</span><span class="p">;</span> <span class="p">}</span> <span class="kt">bool</span> <span class="n">compy</span><span class="p">(</span><span class="k">const</span> <span class="n">point</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="n">point</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">y</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">.</span><span class="n">y</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span><span class="p">;</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">t</span><span class="p">;</span> <span class="k">while</span><span class="p">(</span><span class="n">t</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">memset</span><span class="p">(</span><span class="n">sumx</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">sumx</span><span class="p">));</span> <span class="n">memset</span><span class="p">(</span><span class="n">sumy</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">sumy</span><span class="p">));</span> <span class="n">memset</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">ans</span><span class="p">));</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">tempx</span><span class="p">,</span> <span class="n">tempy</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="c1">//cin &gt;&gt; tempx &gt;&gt; tempy; </span> <span class="n">scanf</span><span class="p">(</span><span class="s">"%I64d%I64d"</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">tempx</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">tempy</span><span class="p">;);</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">index</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">x</span> <span class="o">=</span> <span class="n">tempx</span> <span class="o">-</span> <span class="n">tempy</span><span class="p">;</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">y</span> <span class="o">=</span> <span class="n">tempx</span> <span class="o">+</span> <span class="n">tempy</span><span class="p">;</span> <span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="n">sort</span><span class="p">(</span><span class="n">px</span><span class="p">,</span> <span class="n">px</span><span class="o">+</span><span class="n">n</span><span class="p">,</span> <span class="n">compx</span><span class="p">);</span> <span class="n">sort</span><span class="p">(</span><span class="n">py</span><span class="p">,</span> <span class="n">py</span><span class="o">+</span><span class="n">n</span><span class="p">,</span> <span class="n">compy</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">sumx</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">x</span> <span class="o">-</span> <span class="n">px</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">x</span><span class="p">;</span> <span class="n">sumy</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">y</span> <span class="o">-</span> <span class="n">py</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">y</span><span class="p">;</span> <span class="p">}</span> <span class="n">ans</span><span class="p">[</span><span class="n">px</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sumx</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="n">ans</span><span class="p">[</span><span class="n">py</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sumy</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">sumx</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">sumx</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">x</span> <span class="o">-</span> <span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">].</span><span class="n">x</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)(</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span> <span class="o">-</span> <span class="n">n</span><span class="p">);</span> <span class="n">sumy</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">sumy</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">y</span> <span class="o">-</span> <span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">].</span><span class="n">y</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)(</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span> <span class="o">-</span> <span class="n">n</span><span class="p">);</span> <span class="n">ans</span><span class="p">[</span><span class="n">px</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sumx</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">ans</span><span class="p">[</span><span class="n">py</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sumy</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">bestans</span> <span class="o">=</span> <span class="mi">999999999999999LL</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">ans</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">bestans</span><span class="p">)</span> <span class="n">bestans</span> <span class="o">=</span> <span class="n">ans</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="p">}</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">bestans</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p><strong>1004 Matrix</strong> <a href="http://acm.hdu.edu.cn/showproblem.php?pid=4313">HDU4313</a> 一棵树上有K个机器,要使这K个机器互不相连,使去掉的边的权值和最小。 类似于Kruskal求最小生成树,边由大到小排序,如果某条边使两个机器连通就放弃这条边。因为其它的边一定比这条边长。</p> <div class="language-cpp highlighter-rouge"><pre class="highlight"><code> <span class="cp">#include #include #include #include </span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">MAXN</span> <span class="o">=</span> <span class="mi">100010</span><span class="p">;</span> <span class="k">struct</span> <span class="n">e</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">;</span> <span class="p">}</span><span class="n">edge</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">p</span><span class="p">[</span><span class="n">MAXN</span><span class="p">],</span> <span class="n">rank</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">bool</span> <span class="n">machine</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span> <span class="kt">int</span> <span class="n">n</span><span class="p">;</span> <span class="kt">bool</span> <span class="n">comp</span><span class="p">(</span><span class="k">const</span> <span class="n">e</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;,</span> <span class="k">const</span> <span class="n">e</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">;)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">w</span> <span class="o">&gt;</span> <span class="n">b</span><span class="p">.</span><span class="n">w</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">make_set</span><span class="p">()</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="c1">// ? </span> <span class="p">{</span> <span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span> <span class="n">rank</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">find_set</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">!=</span> <span class="n">p</span><span class="p">[</span><span class="n">x</span><span class="p">])</span> <span class="n">p</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">find_set</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="n">x</span><span class="p">]);</span> <span class="k">return</span> <span class="n">p</span><span class="p">[</span><span class="n">x</span><span class="p">];</span> <span class="p">}</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">kruskal</span><span class="p">(</span><span class="kt">int</span> <span class="n">edgecount</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">;</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">make_set</span><span class="p">();</span> <span class="n">sort</span><span class="p">(</span><span class="n">edge</span><span class="p">,</span> <span class="n">edge</span> <span class="o">+</span> <span class="n">edgecount</span><span class="p">,</span> <span class="n">comp</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">edgecount</span> <span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">x</span> <span class="o">=</span> <span class="n">find_set</span><span class="p">(</span><span class="n">edge</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">u</span><span class="p">);</span> <span class="n">y</span> <span class="o">=</span> <span class="n">find_set</span><span class="p">(</span><span class="n">edge</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">v</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">machine</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="n">machine</span><span class="p">[</span><span class="n">y</span><span class="p">])</span> <span class="k">continue</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">machine</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">||</span> <span class="n">machine</span><span class="p">[</span><span class="n">y</span><span class="p">])</span> <span class="n">machine</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">machine</span><span class="p">[</span><span class="n">y</span><span class="p">]</span> <span class="o">=</span> <spa