{"id":399,"date":"2009-03-28T19:45:10","date_gmt":"2009-03-28T11:45:10","guid":{"rendered":"http:\/\/sinyalee.com\/blog\/?p=399"},"modified":"2009-03-28T19:45:10","modified_gmt":"2009-03-28T11:45:10","slug":"apio-2007-%e9%a3%8e%e9%93%83%ef%bc%88mobiles%ef%bc%89-%e8%a7%a3%e9%a2%98%e6%8a%a5%e5%91%8a","status":"publish","type":"post","link":"https:\/\/sinyalee.com\/blog\/?p=399","title":{"rendered":"APIO 2007 \u98ce\u94c3\uff08Mobiles\uff09 \u89e3\u9898\u62a5\u544a"},"content":{"rendered":"<p>\u9898\u76ee\u4e0e\u6570\u636e\u79fb\u6b65APIO\u5b98\u65b9\u7f51\u7ad9\uff1a<a href=\"http:\/\/www.apio.olympiad.org\/\">http:\/\/www.apio.olympiad.org\/<\/a>  <\/p>\n<p>\u8fd9\u9053\u9898\u5c31\u662f\u8981\u4ea4\u4e92\u4e00\u4e9b\u8282\u70b9\u7684\u5de6\u53f3\u5b50\u6811\uff0c\u8ba9\u5b83\u53d8\u6210\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\u3002  <\/p>\n<p>\u9996\u5148\u8981\u9884\u5904\u7406\u4e00\u4e0b\u6bcf\u4e00\u68f5\u5b50\u6811\u7684\u6700\u5927\u6df1\u5ea6\u548c\u6700\u5c0f\u6df1\u5ea6\uff0c\u8fd9\u4e2a\u7528O(n)\u7684\u6811\u5f62DP\u8f7b\u677e\u89e3\u51b3\u3002\u7ecf\u8fc7\u4e86\u7325\u7410\u6050\u6016\u7684GDKOI\u4e4b\u540e\u4e00\u505a\u5230\u6811\u5f62DP\u5c31\u6709\u7206\u6808\u7684\u9634\u5f71\uff0c\u4f46\u662f\u8fd9\u9053\u9898\u8981\u79fb\u6210\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6df1\u5ea6\u4e0d\u53ef\u80fd\u8d85\u8fc7<a href=\"https:\/\/sinyalee.com\/blog\/wp-content\/uploads\/2009\/03\/image1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/sinyalee.com\/blog\/wp-content\/uploads\/2009\/03\/image-thumb1.png\" width=\"46\" height=\"20\"><\/a>\uff0c\u6240\u4ee5\u53ea\u8981\u6df1\u5ea6\u8d85\u8fc717\u5c31\u76f4\u63a5\u65e0\u89e3\u6389\uff08\u867d\u7136\u6211\u7a0b\u5e8f\u5f88\u6709\u4eba\u54c1\u5730\u7b97\u5230100\uff09\uff0c\u6240\u4ee5\u4e0d\u53ef\u80fd\u7206\u6808\u3002  <\/p>\n<p>\u5982\u679c\u6574\u68f5\u6811\u7684\u53f6\u5b50\u8282\u70b9\u9ad8\u5ea6\u5dee\u8d85\u8fc71\uff0c\u76f4\u63a5\u65e0\u89e3\u6389\u3002\u5426\u5219\u8fdb\u5165\u4e0b\u4e00\u6b65\u3002  <\/p>\n<p>\u7531\u4e8e\u4ea4\u6362\u4e0d\u6539\u53d8\u5176\u6df1\u5ea6\uff0c\u6240\u4ee5\u53ea\u8981\u628a\u6df1\u7684\u79fb\u5230\u5de6\u8fb9\uff0c\u6d45\u7684\u79fb\u5230\u53f3\u8fb9\u5c31\u597d\u4e86\uff0c\u8fd9\u4e2a\u8d2a\u5fc3\u89e3\u51b3\u3002  <\/p>\n<p>\u5904\u7406\u67d0\u68f5\u6811\u65f6\uff0c\u5982\u679c\u5de6\u53f3\u5b50\u6811\u53f6\u5b50\u8282\u70b9\u9ad8\u5ea6\u5dee\u90fd\u662f1\uff0c\u90a3\u4e48\u76f4\u63a5\u65e0\u89e3\u3002\u5982\u679c\u9ad8\u5ea6\u5dee\u90fd\u662f0\uff0c\u90a3\u4e48\u628a\u4f4e\u7684\u79fb\u5230\u5de6\u8fb9\u3002\u5982\u679c\u53ea\u6709\u4e00\u68f5\u5b50\u6811\u9ad8\u5ea6\u5dee\u662f1\uff0c\u90a3\u4e48\u5982\u679c\u53e6\u5916\u7684\u5b50\u6811\u7684\u9ad8\u5ea6\u9ad8\u90a3\u4e48\u653e\u5230\u5de6\u8fb9\uff0c\u5426\u5219\u653e\u5230\u53f3\u8fb9\uff0c\u7136\u540e\u9012\u5f52\u5904\u7406\u6709\u9ad8\u5ea6\u5dee\u7684\u5b50\u6811\u3002  <\/p>\n<p>\u7531\u4e8e\u7a0b\u5e8f\u5199\u4e86\u4e00\u4e2a\u661f\u671f\uff0c\u6709\u70b9\u6de1\u5fd8\uff0c\u53ef\u80fd\u8fd8\u6ca1\u6709\u6db5\u76d6\u6240\u6709\u60c5\u51b5\uff0c\u6240\u4ee5\u5148\u8d34\u4e0a\u7a0b\u5e8f\u3002  <\/p>\n<p>{<br \/>ID: sinya1<br \/>PROG: mobiles<br \/>LANG: PASCAL<br \/>}  <\/p>\n<p>uses math;  <\/p>\n<p>const<br \/>&nbsp; pn=&#8217;mobiles&#8217;;<br \/>&nbsp; maxn=100000;  <\/p>\n<p>var<br \/>&nbsp; n,i:longint;<br \/>&nbsp; lc,rc:array[1..maxn]of longint;<br \/>&nbsp; minh,maxh:array[1..maxn]of longint;  <\/p>\n<p>procedure outpt(const x:longint);begin<br \/>&nbsp; assign(output,pn+&#8217;.out&#8217;);rewrite(output);<br \/>&nbsp; writeln(x);<br \/>&nbsp; close(output);<br \/>&nbsp; halt;<br \/>end;  <\/p>\n<p>procedure init;begin<br \/>&nbsp; assign(input,pn+&#8217;.in&#8217;);reset(input);<br \/>&nbsp; readln(n);<br \/>&nbsp; for i:=1 to n do readln(lc[i],rc[i]);<br \/>&nbsp; close(input);<br \/>end;  <\/p>\n<p>procedure dp1(const x,h:longint);forward;  <\/p>\n<p>procedure dp1_1(const x,h,ch:longint);begin<br \/>&nbsp; if ch=-1 then begin<br \/>&nbsp;&nbsp;&nbsp; minh[x]:=min(minh[x],h+1);<br \/>&nbsp;&nbsp;&nbsp; maxh[x]:=max(maxh[x],h+1);<br \/>&nbsp; end else begin<br \/>&nbsp;&nbsp;&nbsp; dp1(ch,h+1);<br \/>&nbsp;&nbsp;&nbsp; minh[x]:=min(minh[x],minh[ch]);<br \/>&nbsp;&nbsp;&nbsp; maxh[x]:=max(maxh[x],maxh[ch]);<br \/>&nbsp; end;<br \/>end;  <\/p>\n<p>procedure dp1(const x,h:longint);begin<br \/>&nbsp; if h&gt;100 then outpt(-1);<br \/>&nbsp; minh[x]:=maxlongint;<br \/>&nbsp; maxh[x]:=-maxlongint;<br \/>&nbsp; dp1_1(x,h,lc[x]);<br \/>&nbsp; dp1_1(x,h,rc[x]);<br \/>end;  <\/p>\n<p>function cmaxh(const x,h:longint):longint;begin<br \/>&nbsp; if x=-1 then exit(h);exit(maxh[x]);<br \/>end;  <\/p>\n<p>function cminh(const x,h:longint):longint;begin<br \/>&nbsp; if x=-1 then exit(h);exit(minh[x]);<br \/>end;  <\/p>\n<p>function dif(const x,h:longint):longint;begin<br \/>&nbsp; exit(cmaxh(x,h)-cminh(x,h));<br \/>end;  <\/p>\n<p>function dp2(const x,h:longint):longint;begin<br \/>&nbsp; if dif(lc[x],h+1)&gt;0 then begin<br \/>&nbsp;&nbsp;&nbsp; if dif(rc[x],h+1)&gt;0 then begin<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outpt(-1);<br \/>&nbsp;&nbsp;&nbsp; end else begin<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if cminh(rc[x],h+1)=minh[1] then<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(dp2(lc[x],h+1))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(dp2(lc[x],h+1)+1)<br \/>&nbsp;&nbsp;&nbsp; end;<br \/>&nbsp; end else begin<br \/>&nbsp;&nbsp;&nbsp; if dif(rc[x],h+1)&gt;0 then begin<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if cmaxh(lc[x],h+1)=maxh[1] then<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(dp2(rc[x],h+1))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(dp2(rc[x],h+1)+1)<br \/>&nbsp;&nbsp;&nbsp; end else begin<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if cmaxh(lc[x],h+1)=maxh[1] then <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else exit(1);<br \/>&nbsp;&nbsp;&nbsp; end;<br \/>&nbsp; end;<br \/>end;  <\/p>\n<p>begin<br \/>&nbsp; init;\/\/ read the data<br \/>&nbsp; dp1(1,1);\/\/ calculate the height<br \/>&nbsp; if maxh[1]-minh[1]&gt;1 then outpt(-1);<br \/>&nbsp; if maxh[1]-minh[1]=0 then outpt(0);<br \/>&nbsp; outpt(dp2(1,1));<br \/>end.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u4e0e\u6570\u636e\u79fb\u6b65APIO\u5b98\u65b9\u7f51\u7ad9\uff1ahttp:\/\/www.apio.olympiad.org\/ \u8fd9\u9053\u9898\u5c31\u662f\u8981\u4ea4\u4e92\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[23,131,344],"class_list":["post-399","post","type-post","status-publish","format-standard","hentry","category-13","tag-apio","tag-131","tag-344"],"_links":{"self":[{"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/399","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=399"}],"version-history":[{"count":0,"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/399\/revisions"}],"wp:attachment":[{"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sinyalee.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}