{"id":287,"date":"2016-11-25T20:29:12","date_gmt":"2016-11-25T12:29:12","guid":{"rendered":"http:\/\/kylelv.com\/?p=287"},"modified":"2017-12-20T19:30:35","modified_gmt":"2017-12-20T11:30:35","slug":"bzoj-1637-usaco2007-marbalanced-lineup-%e5%b7%ae%e5%88%86","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=287","title":{"rendered":"bzoj 1637: [Usaco2007 Mar]Balanced Lineup  &#8212; \u5dee\u5206"},"content":{"rendered":"<p><center><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_73 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fdef6c450fc\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fdef6c450fc\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.kylelv.com\/?p=287\/#1637_Usaco2007_MarBalanced_Lineup\" title=\"1637: [Usaco2007 Mar]Balanced Lineup\">1637: [Usaco2007 Mar]Balanced Lineup<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.kylelv.com\/?p=287\/#Description\" title=\"Description\">Description<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.kylelv.com\/?p=287\/#Input\" title=\"Input\">Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.kylelv.com\/?p=287\/#Output\" title=\"Output\">Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.kylelv.com\/?p=287\/#Sample_Input\" title=\"Sample Input\">Sample Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.kylelv.com\/?p=287\/#Sample_Output\" title=\"Sample Output\">Sample Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.kylelv.com\/?p=287\/#HINT\" title=\"HINT\">HINT<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"1637_Usaco2007_MarBalanced_Lineup\"><\/span>1637: [Usaco2007 Mar]Balanced Lineup<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span class=\"green\">Time Limit:\u00a05 Sec\u00a0\u00a0Memory Limit:\u00a064 MB<\/span><\/center><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>Farmer John \u51b3\u5b9a\u7ed9\u4ed6\u7684\u5976\u725b\u4eec\u7167\u4e00\u5f20\u5408\u5f71\uff0c\u4ed6\u8ba9 N (1 \u2264 N \u2264 50,000) \u5934\u5976\u725b\u7ad9\u6210\u4e00\u6761\u76f4\u7ebf\uff0c\u6bcf\u5934\u725b\u90fd\u6709\u5b83\u7684\u5750\u6807(\u8303\u56f4: 0..1,000,000,000)\u548c\u79cd\u65cf(0\u62161)\u3002 \u4e00\u76f4\u4ee5\u6765 Farmer John \u603b\u662f\u559c\u6b22\u505a\u4e00\u4e9b\u975e\u51e1\u7684\u4e8b\uff0c\u5f53\u7136\u8fd9\u6b21\u7167\u76f8\u4e5f\u4e0d\u4f8b\u5916\u3002\u4ed6\u53ea\u7ed9\u4e00\u90e8\u5206\u725b\u7167\u76f8\uff0c\u5e76\u4e14\u8fd9\u4e00\u7ec4\u725b\u7684\u9635\u5bb9\u5fc5\u987b\u662f\u201c\u5e73\u8861\u7684\u201d\u3002\u5e73\u8861\u7684\u9635\u5bb9\uff0c\u6307\u7684\u662f\u5728\u4e00\u7ec4\u725b\u4e2d\uff0c\u79cd\u65cf0\u548c\u79cd\u65cf1\u7684\u725b\u7684\u6570\u91cf\u76f8\u7b49\u3002 \u8bf7\u7b97\u51fa\u6700\u5e7f\u9614\u7684\u533a\u95f4\uff0c\u4f7f\u8fd9\u4e2a\u533a\u95f4\u5185\u7684\u725b\u9635\u5bb9\u5e73\u8861\u3002\u533a\u95f4\u7684\u5927\u5c0f\u4e3a\u533a\u95f4\u5185\u6700\u53f3\u8fb9\u7684\u725b\u7684\u5750\u6807\u51cf\u53bb\u6700\u505a\u8fb9\u7684\u725b\u7684\u5750\u6807\u3002 \u8f93\u5165\u4e2d\uff0c\u6bcf\u4e2a\u79cd\u65cf\u81f3\u5c11\u6709\u4e00\u5934\u725b\uff0c\u6ca1\u6709\u4e24\u5934\u725b\u7684\u5750\u6807\u76f8\u540c\u3002<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>\u884c 1: \u4e00\u4e2a\u6574\u6570: N \u884c 2..N + 1: \u6bcf\u884c\u4e24\u4e2a\u6574\u6570\uff0c\u4e3a\u79cd\u65cf ID \u548c x \u5750\u6807\u3002<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>\u884c 1: \u4e00\u4e2a\u6574\u6570\uff0c\u9635\u5bb9\u5e73\u8861\u7684\u6700\u5927\u7684\u533a\u95f4\u7684\u5927\u5c0f\u3002<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\">7<br \/>\n0 11<br \/>\n1 10<br \/>\n1 25<br \/>\n1 12<br \/>\n1 4<br \/>\n0 13<br \/>\n1 22<br \/>\n<\/span><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\">11<br \/>\n\u8f93\u5165\u8bf4\u660e<\/p>\n<p>\u67097\u5934\u725b\uff0c\u50cf\u8fd9\u6837\u5728\u6570\u8f74\u4e0a\u3002<\/p>\n<p>1 1 0 1 0 1 1<br \/>\n+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+<br \/>\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25<br \/>\n\u8f93\u51fa\u8bf4\u660e<\/p>\n<p>\u725b #1 (at 11), #4 (at 12), #6 (at 13), #7 (at 22) \u7ec4\u6210\u4e00\u4e2a\u5e73\u8861\u7684\u6700\u5927\u7684\u533a\u95f4\uff0c\u5927\u5c0f\u4e3a 22-11=11 \u4e2a\u5355\u4f4d\u957f\u5ea6\u3002<\/p>\n<p>&lt;&#8212;&#8212;&#8211; \u5e73\u8861\u7684 &#8212;&#8212;&#8211;&gt;<br \/>\n1 1 0 1 0 1 1<br \/>\n+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+&#8211;+<br \/>\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25<br \/>\n<\/span><\/div>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u8bbeID=1\u4e3a1\uff0cID=0\u4e3a-1\uff0c\u524d\u7f00\u548c\u4e3asum[]\uff0c\u82e5(i,j)\u4e3a\u5e73\u8861\u7684\uff0c\u90a3\u4e48\u6709sum[j]==sum[i-1],\u8bb0\u5f55\u4e00\u4e0b\u6bcf\u4e00\u4e2asum\u51fa\u73b0\u7684\u6700\u65e9\u4f4d\u7f6e\u662f\u54ea\uff0c\u626b\u4e00\u904d\u5373\u53ef\u3002<\/p>\n<pre class=\"lang:c++ decode:true \">#include&lt;cstdio&gt;\r\n#include&lt;algorithm&gt;\r\n#include&lt;iostream&gt; \r\n#define N 50050\r\nusing namespace std;\r\nint max(int a,int b){return a&gt;b?a:b;}\r\nstruct ljn{int x,y;}a[N];\r\nbool cmp(ljn q,ljn w){return q.y&lt;w.y;}\r\nint n,sum[N],ji[N&lt;&lt;1][2],ans;\r\nint main()\r\n{\r\n    scanf(\"%d\",&amp;n);\r\n    for(int i=0;i&lt;n;i++) scanf(\"%d%d\",&amp;a[i].x,&amp;a[i].y);\r\n    sort(a,a+n,cmp);\r\n    sum[0]=(a[0].x?1:-1);\r\n    for(int i=1;i&lt;n;i++)\r\n    {\r\n        sum[i]=sum[i-1];\r\n        sum[i]+=(a[i].x?1:-1);\r\n    }\r\n    for(int i=0;i&lt;n;i++) ji[sum[i]+n][0]?ji[sum[i]+n][1]=i:ji[sum[i]+n][0]=i;\r\n    for(int i=0;i&lt;(n&lt;&lt;1);i++) if(ji[i][1]) ans=max(ans,a[ji[i][1]].y-a[ji[i][0]+1].y);\r\n    printf(\"%d\",ans);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1637: [Usaco2007 Mar]Balanced Lineup Time Limit:\u00a05 Sec\u00a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-15"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":1,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":288,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions\/288"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}