{"id":175,"date":"2008-03-08T18:26:02","date_gmt":"2008-03-08T10:26:02","guid":{"rendered":"http:\/\/sinya.yo2.cn\/624099.html"},"modified":"2008-03-08T18:26:02","modified_gmt":"2008-03-08T10:26:02","slug":"usaco-sort3","status":"publish","type":"post","link":"https:\/\/sinyalee.com\/blog\/?p=175","title":{"rendered":"[USACO\u89e3\u9898\u62a5\u544a]Sorting a Three-Valued Sequence (sort3)"},"content":{"rendered":"<h4>\u539f\u9898<\/h4>\n<p><font color=\"#008000\"><strong>Sorting a Three-Valued Sequence <\/strong><br \/>\n<strong>IOI&#8217;96 &#8211;  Day 2<\/strong><\/font><\/p>\n<p><font color=\"#008000\">Sorting is one of the most frequently performed  computational tasks. Consider the special sorting problem in which the records  to be sorted have at most<em> three<\/em> different key values. This happens for  instance when we sort medalists of a competition according to medal value, that  is, gold medalists come first, followed by silver, and bronze medalists come  last. <\/font><\/p>\n<p><font color=\"#008000\">In this task the possible key values are the integers 1,  2 and 3. The required sorting order is non-decreasing. However, sorting has to  be accomplished by a sequence of exchange operations. An exchange operation,  defined by two position numbers p and q, exchanges the elements in positions p  and q. <\/font><\/p>\n<p><font color=\"#008000\">You are given a sequence of key values. Write a program  that computes the minimal number of exchange operations that are necessary to  make the sequence sorted. <\/font><\/p>\n<h5><font color=\"#008000\">PROGRAM NAME: sort3<\/font><\/h5>\n<h5><font color=\"#008000\">INPUT FORMAT<\/font><\/h5>\n<p><font color=\"#008000\">Line 1:<br \/>\nN (1 &lt;= N &lt;= 1000), the number of  records to be sorted<\/font><\/p>\n<p><font color=\"#008000\">Lines 2-N+1:<br \/>\nA single integer from the set {1, 2,  3}<\/font><\/p>\n<h5><font color=\"#008000\">SAMPLE INPUT (file sort3.in) <\/font><\/h5>\n<pre><font color=\"#008000\">9\n2\n2\n1\n3\n3\n3\n2\n3\n1\n<\/font><\/pre>\n<h5><font color=\"#008000\">OUTPUT FORMAT<\/font><\/h5>\n<p><font color=\"#008000\">A single  line containing the number of exchanges required <\/font><\/p>\n<h5><font color=\"#008000\">SAMPLE OUTPUT (file sort3.out)<\/font><\/h5>\n<pre><font color=\"#008000\">4<\/font><\/pre>\n<h4>\u61d2\u5f97\u81ea\u5df1\u7ffb\u8bd1\u4e86\uff0c\u4eceNOCOW\u4e0a\u627e\u7ffb\u8bd1\u8d34\u4e0b\u6765\u3002\uff08P.S. NOCOW\u8fd9\u4e2a\u540d\u5b57\u8d77\u7684\u8fd8\u771f\u597d\u554a\uff09<\/h4>\n<p><font color=\"#0000ff\"><strong>\u4e09\u503c\u7684\u6392\u5e8f<\/strong><br \/>\n<strong>IOI&#8217;96 &#8211; Day 2<\/strong> <\/font><\/p>\n<p><font color=\"#0000ff\">\u8bd1 by !Starliu <\/font><\/p>\n<p><font color=\"#0000ff\">\u6392\u5e8f\u662f\u4e00\u79cd\u5f88\u9891\u7e41\u7684\u8ba1\u7b97\u4efb\u52a1\u3002\u73b0\u5728\u8003\u8651\u6700\u591a\u53ea\u6709\u4e09\u503c\u7684\u6392\u5e8f\u95ee\u9898\u3002\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\u662f\uff0c\u5f53\u6211\u4eec\u7ed9\u67d0\u9879\u7ade\u8d5b\u7684\u4f18\u80dc\u8005\u6309\u91d1\u94f6\u94dc\u724c\u5e8f\u7684\u65f6\u5019\u3002  <\/font><\/p>\n<p><font color=\"#0000ff\">\u5728\u8fd9\u4e2a\u4efb\u52a1\u4e2d\u53ef\u80fd\u7684\u503c\u53ea\u6709\u4e09\u79cd1\uff0c2\u548c3\u3002\u6211\u4eec\u7528\u4ea4\u6362\u7684\u65b9\u6cd5\u628a\u4ed6\u6392\u6210\u5347\u5e8f\u7684\u3002 <\/font><\/p>\n<p><font color=\"#0000ff\">\u5199\u4e00\u4e2a\u7a0b\u5e8f\u8ba1\u7b97\u51fa\uff0c\u7ed9\u5b9a\u7684\u4e00\u4e2a1,2,3\u7ec4\u6210\u7684\u6570\u5b57\u5e8f\u5217\uff0c\u6392\u6210\u5347\u5e8f\u6240\u9700\u7684\u6700\u5c11\u4ea4\u6362\u6b21\u6570\u3002 <\/font><\/p>\n<h5><font color=\"#0000ff\">PROGRAM NAME: sort3<\/font><\/h5>\n<h5><font color=\"#0000ff\">INPUT FORMAT<\/font><\/h5>\n<p><font color=\"#0000ff\">Line 1: <\/font><\/p>\n<p><font color=\"#0000ff\">N (1 &lt;= N &lt;= 1000) <\/font><\/p>\n<p><font color=\"#0000ff\">Lines 2-N+1: <\/font><\/p>\n<p><font color=\"#0000ff\">\u6bcf\u884c\u4e00\u4e2a\u6570\u5b57\uff0c\u5171N\u884c\u3002\uff081..3\uff09 <\/font><\/p>\n<h5><font color=\"#0000ff\">SAMPLE INPUT (file sort3.in) <\/font><\/h5>\n<pre><font color=\"#0000ff\">9<\/font><\/pre>\n<pre><font color=\"#0000ff\">2<\/font><\/pre>\n<pre><font color=\"#0000ff\">2<\/font><\/pre>\n<pre><font color=\"#0000ff\">1<\/font><\/pre>\n<pre><font color=\"#0000ff\">3<\/font><\/pre>\n<pre><font color=\"#0000ff\">3<\/font><\/pre>\n<pre><font color=\"#0000ff\">3<\/font><\/pre>\n<pre><font color=\"#0000ff\">2<\/font><\/pre>\n<pre><font color=\"#0000ff\">3<\/font><\/pre>\n<pre><font color=\"#0000ff\">1<\/font><\/pre>\n<h5><font color=\"#0000ff\">OUTPUT FORMAT<\/font><\/h5>\n<p><font color=\"#0000ff\">\u5171\u4e00\u884c\uff0c\u4e00\u4e2a\u6570\u5b57\u3002\u8868\u793a\u6392\u6210\u5347\u5e8f\u6240\u9700\u7684\u6700\u5c11\u4ea4\u6362\u6b21\u6570\u3002 <\/font><\/p>\n<h5><font color=\"#0000ff\">SAMPLE OUTPUT (file sort3.out)<\/font><\/h5>\n<pre><font color=\"#0000ff\">4<\/font><\/pre>\n<pre><\/pre>\n<h4>\u6211\u7684\u9898\u89e3\uff1a<\/h4>\n<p>\u8fd9\u9053\u9898\u6bd4<a href=\"http:\/\/sinya.yo2.cn\/\/?p=623795\" title=\"[USACO\u7ed3\u9898\u62a5\u544a]Castle\">Castle<\/a>\u8fd8\u7b80\u5355\u3002\u9996\u5148\u7edf\u8ba1\u51fa1\uff0c2\uff0c3\u51fa\u73b0\u6b21\u6570\uff0c\u8bb0\u4e3at[1],t[2]\u8fd8\u6709t[3]\u3002<\/p>\n<p>\u5219\u6392\u5e8f\u540e\uff0c\u524dt[1]\u9879\u662f1\uff0c\u63a5\u4e0b\u6765t[2]\u9879\u662f2\u2026\u2026<\/p>\n<p>\u7136\u540e\u524dt[1]\u9879\u5982\u679c\u4e0d\u662f1\uff0c\u5219\u8981\u628a\u540e\u9762\u76841\u6362\u5230\u524d\u9762\u6765\u3002\u5982\u679c\u662f2\uff0c\u90a3\u4e48\u5c31\u5c3d\u91cf\u53bb\u6362\u524d\u9762\u76841\uff0c3\u5c31\u5c3d\u91cf\u6362\u540e\u9762\u76841\u3002<\/p>\n<p>\u540e\u9762\u7684\u7b80\u5355\u7684\u54272\uff0c\u548c3\u6362\u8fc7\u6765\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u7a0b\u5e8f\uff1a<\/p>\n<p>{<br \/>\nID: Sinya1<br \/>\nPROG: sort3<br \/>\nLANG: PASCAL<br \/>\n}<\/p>\n<p>{WELCOME TO MY WEBSITE: <a href=\"http:\/\/sinya.yo2.cn\/%7D\">HTTP:\/\/SINYA.YO2.CN\/}<\/a><br \/>\n{I love my hometown,  Swatow, China!!!!!}<br \/>\n<font color=\"#ffffff\">{Anyway,I Love You,  Kekyang!!!!!}<\/font><\/p>\n<p>Program sort3;<\/p>\n<p>Var<br \/>\n<font color=\"#ffffff\">{}<\/font>i,j,n,r:integer;<br \/>\n<font color=\"#ffffff\">{}<\/font>a:array[1..1000]of byte;<font color=\"#ffffff\"><br \/>\n{}<\/font>t:array[1..3]of integer;<\/p>\n<p>Begin<br \/>\n<font color=\"#ffffff\">{}<\/font>Assign(input, &#8216;sort3.in&#8217;); Reset(input);<br \/>\n<font color=\"#ffffff\">{}<\/font>Assign(output,  &#8216;sort3.out&#8217;); Rewrite(output);<\/p>\n<p><font color=\"#ffffff\">{}<\/font>fillchar(t,sizeof(t),0);<br \/>\n<font color=\"#ffffff\">{}<\/font>r:=0;<\/p>\n<p><font color=\"#ffffff\">{}<\/font>readln(n);<br \/>\n<font color=\"#ffffff\">{}<\/font>for i:=1 to n do begin<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>readln(a[i]);<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>inc(t[a[i]])<br \/>\n<font color=\"#ffffff\">{}<\/font>end; <font color=\"#993366\">{\u8f93\u5165\u4e0e\u7edf\u8ba1}<\/font><\/p>\n<p><font color=\"#ffffff\">{}<\/font>j:=t[1];<br \/>\n<font color=\"#ffffff\">{}<\/font>for i:=1 to t[1]do<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>if a[i]=2 then begin<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>inc(r);<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>repeat<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>inc(j);<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>until a[j]=1;<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>a[i]:=1;<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>a[j]:=2;<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>end;<font color=\"#993366\"> {\u4ea4\u63621\u548c2}<\/font><\/p>\n<p><font color=\"#ffffff\">{}<\/font>for i:=1 to t[1]do<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>if a[i]=3 then begin<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>inc(r);<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>a[i]:=1;<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>end;<br \/>\n<font color=\"#ffffff\">{}<\/font>for i:=t[1]+1 to n do<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>if a[i]=1then<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>a[i]:=3; <font color=\"#993366\">{\u4ea4\u63621\u548c3}<\/font><\/p>\n<p><font color=\"#ffffff\">{}<\/font>for i:=t[1]+1 to t[1]+t[2]do<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>if a[i]=3 then<br \/>\n<font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font><font color=\"#ffffff\">{}<\/font>inc(r); <font color=\"#993366\">{\u7edf\u8ba1\u4ea4\u63622\u548c3\u6240\u9700\u6b21\u6570}<\/font><\/p>\n<p><font color=\"#ffffff\">{}<\/font>writeln(r);<\/p>\n<p><font color=\"#ffffff\">{}<\/font>Close(input);<br \/>\n<font color=\"#ffffff\">{}<\/font>Close(output);<br \/>\nEnd.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u9898 Sorting a Three-Valued Sequence IOI&#8217;96 &#8211; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,13],"tags":[86,131,344],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-14","category-13","tag-usaco","tag-131","tag-344"],"_links":{"self":[{"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=175"}],"version-history":[{"count":0,"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/175\/revisions"}],"wp:attachment":[{"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}