Files
PAT/source/html/a1067.html
T

18 lines
3.8 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Sort with Swap(0, i)
<div class="ques-view"><p>Given any permutation of the numbers {0, 1, 2,..., <span class="katex"><span class="katex-mathml"><math><mrow><mi>N</mi><mo>−</mo><mn>1</mn></mrow>N-1</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">N</span><span class="mbin">−</span><span class="mord mathrm">1</span></span></span></span>}, it is easy to sort them in increasing order. But what if <code>Swap(0, *)</code> is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:</p>
<pre><code>Swap(0, 1) =&gt; {4, 1, 2, 0, 3}
Swap(0, 3) =&gt; {4, 1, 2, 3, 0}
Swap(0, 4) =&gt; {0, 1, 2, 3, 4}
</code></pre><p>Now you are asked to find the minimum number of swaps need to sort the given permutation of the first <span class="katex"><span class="katex-mathml"><math><mrow><mi>N</mi></mrow>N</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.68333em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">N</span></span></span></span> nonnegative integers.</p>
<h3 id="input-specification-">Input Specification:</h3>
<p>Each input file contains one test case, which gives a positive <span class="katex"><span class="katex-mathml"><math><mrow><mi>N</mi></mrow>N</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.68333em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">N</span></span></span></span> (<span class="katex"><span class="katex-mathml"><math><mrow><mo>≤</mo><mn>1</mn><msup><mn>0</mn><mn>5</mn></msup></mrow>\le 10^5</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.8141079999999999em;"></span><span class="strut bottom" style="height:0.950078em;vertical-align:-0.13597em;"></span><span class="base textstyle uncramped"><span class="mrel">≤</span><span class="mord mathrm">1</span><span class="mord"><span class="mord mathrm">0</span><span class="msupsub"><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord mathrm mtight">5</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span>​</span></span></span></span></span></span></span>) followed by a permutation sequence of {0, 1, ..., <span class="katex"><span class="katex-mathml"><math><mrow><mi>N</mi><mo>−</mo><mn>1</mn></mrow>N-1</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">N</span><span class="mbin">−</span><span class="mord mathrm">1</span></span></span></span>}. All the numbers in a line are separated by a space.</p>
<h3 id="output-specification-">Output Specification:</h3>
<p>For each case, simply print in a line the minimum number of swaps need to sort the given permutation.</p>
<h3 id="sample-input-">Sample Input:</h3>
<pre><code class="lang-in">10
3 5 7 2 6 4 9 0 8 1
</code></pre>
<h3 id="sample-output-">Sample Output:</h3>
<pre><code class="lang-out">9
</code></pre>
</div>