{"id":364,"date":"2017-02-20T20:06:25","date_gmt":"2017-02-20T12:06:25","guid":{"rendered":"http:\/\/kylelv.com\/?p=364"},"modified":"2017-12-28T14:37:42","modified_gmt":"2017-12-28T06:37:42","slug":"bzoj-1704-usaco2007-marface-the-right-way-%e8%87%aa%e5%8a%a8%e8%bd%ac%e8%ba%ab%e6%9c%ba-%e8%b4%aa%e5%bf%83","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=364","title":{"rendered":"bzoj 1704: [Usaco2007 Mar]Face The Right Way \u81ea\u52a8\u8f6c\u8eab\u673a  &#8212; \u8d2a\u5fc3"},"content":{"rendered":"<p>&nbsp;<\/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-69fdef88d4427\" 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-69fdef88d4427\"  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=364\/#1704_Usaco2007_MarFace_The_Right_Way_%E8%87%AA%E5%8A%A8%E8%BD%AC%E8%BA%AB%E6%9C%BA\" title=\"1704: [Usaco2007 Mar]Face The Right Way \u81ea\u52a8\u8f6c\u8eab\u673a\">1704: [Usaco2007 Mar]Face The Right Way \u81ea\u52a8\u8f6c\u8eab\u673a<\/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=364\/#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=364\/#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=364\/#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=364\/#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=364\/#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=364\/#HINT\" title=\"HINT\">HINT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.kylelv.com\/?p=364\/#Source\" title=\"Source\">Source<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"1704_Usaco2007_MarFace_The_Right_Way_%E8%87%AA%E5%8A%A8%E8%BD%AC%E8%BA%AB%E6%9C%BA\"><\/span>1704: [Usaco2007 Mar]Face The Right Way \u81ea\u52a8\u8f6c\u8eab\u673a<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\">Time Limit: 5 Sec\u00a0\u00a0Memory Limit: 64 MB<\/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<div>\u00a0\u00a0\u00a0\u519c\u592b\u7ea6\u7ff0\u6709N(1\u2264N\u22645000)\u53ea\u725b\u7ad9\u6210\u4e00\u6392\uff0c\u6709\u4e00\u4e9b\u5f88\u4e56\u7684\u725b\u671d\u524d\u7ad9\u7740\uff0e\u4f46\u662f\u6709\u4e9b\u4e0d\u4e56\u7684\u725b\u5374\u671d\u540e\u7ad9\u7740\uff0e\u519c\u592b\u7ea6\u7ff0\u9700\u8981\u8ba9\u6240\u6709\u7684\u725b\u90fd\u671d\u524d\u7ad9\u7740\uff0e\u5e78\u8fd0\u7684\u662f\u7ea6\u7ff0\u6700\u8fd1\u4e70\u4e86\u4e00\u4e2a\u81ea\u52a8\u8f6c\u8eab\u673a\uff0e\u8fd9\u4e2a\u795e\u5947\u7684\u673a\u5668\u80fd\u4f7fK(1\u2264K\u2264N)\u53ea\u8fde\u7eed\u7684\u725b\u8f6c\u8eab\uff0e\u00a0\u00a0\u56e0\u4e3a\u7ea6\u7ff0\u4ece\u6765\u90fd\u4e0d\u6539\u53d8K\u7684\u4ef7\u503c\uff0c\u8bf7\u5e2e\u52a9\u4ed6\u6c42\u51faK\uff0c\u4f7f\u65cb\u8f6c\u6b21\u6570M\u8fbe\u5230\u6700\u5c0f\uff0e\u540c\u65f6\u8981\u6c42\u51fa\u5bf9\u5e94\u7684M.<\/div>\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<div>\u00a0\u00a0\u00a0\u00a0\u7b2c1\u884c\uff1a\u6574\u6570N.<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u7b2c2\u884c\u5230\u7b2cN+1\u884c\uff1a\u7b2ci+l\u884c\u8868\u793a\u725bj\u7684\u671d\u5411\uff0cF\u8868\u793a\u671d\u524d\uff0cB\u8868\u793a\u671d\u540e\uff0e<\/div>\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>\u00a0\u00a0\u00a0\u4e00\u884c\u4e24\u4e2a\u6570\uff0c\u5206\u522b\u662fK\u548cM\uff0c\u4e2d\u95f4\u7528\u7a7a\u683c\u9694\u5f00<\/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\">7<br \/>\nB<br \/>\nB<br \/>\nF<br \/>\nB<br \/>\nF<br \/>\nB<br \/>\nBINPUT DETAILS:<\/p>\n<p>There are seven cows and they are facing backward, backward, forward,<br \/>\nbackward, forward, backward, and backward, respectively.<\/p>\n<\/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\">\n<p>3 3<\/p>\n<p>OUTPUT DETAILS:<\/p>\n<p>For K = 3, the machine must be operated three times: turn cows (1,2,3),<br \/>\n(3,4,5), and finally (5,6,7):<\/p>\n<p>B &gt; F F F<br \/>\nB &gt; F F F<br \/>\nF &gt; B &gt; F F<br \/>\nB B &gt; F F<br \/>\nF F &gt; B &gt; F<br \/>\nB B B &gt; F<br \/>\nB B B &gt; F<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>&nbsp;<\/p>\n<p>\u5f53K=3\u65f6\u795e\u5947\u7684\u673a\u5668\u65cb\u8f6c3\u6b21\uff1a(1\uff0c2\uff0c3)\uff0c(3\uff0c4\uff0c5)\uff0c\u548c(5\uff0c6\uff0c7)<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Source\"><\/span>Source<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u8d2a\u5fc3&#8230;\u5148\u679a\u4e3ek, \u7136\u540e\u4ece\u5de6\u5f80\u53f3\u626b\u4e00\u904d, \u53d1\u73b0\u4f4d\u7f6ep\u7684\u725b\u7684\u72b6\u6001\u4e0d\u7b26\u5408\u5c31\u5c06 [p, p + k ) \u7684\u725b\u90fd\u8f6c\u8eab, \u5047\u5982p + k &#8211; 1 \u5df2\u7ecf\u8d85\u8fc7\u4e86\u6700\u53f3\u8fb9\u725b\u7684\u4f4d\u7f6e\u90a3\u8fd9\u4e2ak\u5c31\u4e0d\u7b26\u5408\u8981\u6c42. \u7b26\u5408\u8981\u6c42\u7684\u5c31\u53ef\u4ee5\u7528\u6765\u66f4\u65b0answer.\u8fd9\u4e2a\u8d2a\u5fc3\u7684\u6b63\u786e\u6027\u662f\u5f88\u663e\u7136\u7684.\u524dp &#8211; 1\u5934\u725b\u90fd\u5df2\u671d\u524d, \u518d\u6539\u52a8\u5b83\u4eec\u4e5f\u505a\u4e0d\u5230\u66f4\u4f18; \u800c\u8981\u8ba9\u7b2cp\u5934\u725b\u8f6c\u8eab, \u90a3\u5c31\u53ea\u80fd\u8ba9[p, p + k )\u7684\u725b\u8f6c\u8eab.<\/p>\n<p>\u8003\u8651\u5982\u4f55\u5224\u65ad\u4f4d\u7f6ep\u7684\u725b\u7684\u72b6\u6001, \u6211\u4eec\u53d1\u73b0p\u7684\u72b6\u6001\u4e0e\u5b83\u672c\u8eab\u548c[ p &#8211; k &#8211; 1, p )\u8fd9\u4e2a\u533a\u95f4\u5185\u7684\u725b\u7684\u8f6c\u8eab\u6b21\u6570\u6709\u5173, \u56e0\u4e3a\u8f6c\u8eab\u4e24\u6b21\u76f8\u5f53\u4e8e\u6ca1\u8f6c, \u7528\u5f02\u6216\u8fdb\u884c\u64cd\u4f5c\u53ef\u4ee5\u505a\u5230O(1). \u679a\u4e3eO(n), \u626b\u63cfO(n), \u603b\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n\u00b2)<\/p>\n<pre class=\"lang:c++ decode:true \">#include&lt;cstdio&gt;\r\n#include&lt;cstring&gt;\r\n#include&lt;iostream&gt;\r\nusing namespace std;\r\n#define N 5010\r\nchar c;bool lj[N],a[N],t;int n,k,ans;\r\nint main()\r\n{\r\n    scanf(\"%d\",&amp;n);\r\n    for(int i=1;i&lt;=n;i++)\r\n    {\r\n        scanf(\"\\n%c\",&amp;c);\r\n        a[i]=(c=='F'?0:1);\r\n    }\r\n    for(int i=n;i&gt;0;i--)\r\n    {\r\n        memset(lj,0,sizeof(lj));t=ans=0;\r\n        for(int j=1;j&lt;=n;j++)\r\n        { \r\n            if(!(a[j]^lj[j-1]^lj[j])) lj[j]^=lj[j-1];\r\n            else \r\n            {\r\n                if(j+i-1&lt;=n) ans++;\r\n                else{t=1;break;}\r\n                lj[j]=lj[j]^lj[j-1]^1;lj[j+i]^=1;\r\n            }\r\n        }\r\n        if(!t){k=i;break;}\r\n    }\r\n    printf(\"%d %d\\n\",k,ans);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 1704: [Usaco2007 Mar]Face The Right Way \u81ea\u52a8\u8f6c\u8eab\u673a Ti [&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":[6],"class_list":["post-364","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-6"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/364","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=364"}],"version-history":[{"count":2,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/364\/revisions"}],"predecessor-version":[{"id":366,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/364\/revisions\/366"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}