{"id":428,"date":"2017-04-05T19:09:21","date_gmt":"2017-04-05T11:09:21","guid":{"rendered":"http:\/\/kylelv.com\/?p=428"},"modified":"2018-01-03T18:10:42","modified_gmt":"2018-01-03T10:10:42","slug":"bzoj-4769-%e8%b6%85%e7%ba%a7%e8%b4%9e%e9%b1%bc-%e5%bd%92%e5%b9%b6%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=428","title":{"rendered":"bzoj 4769: \u8d85\u7ea7\u8d1e\u9c7c  &#8212; \u5f52\u5e76\u6392\u5e8f"},"content":{"rendered":"<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-69fdef40a3227\" 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-69fdef40a3227\"  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=428\/#4769_%E8%B6%85%E7%BA%A7%E8%B4%9E%E9%B1%BC\" title=\"4769: \u8d85\u7ea7\u8d1e\u9c7c\">4769: \u8d85\u7ea7\u8d1e\u9c7c<\/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=428\/#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=428\/#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=428\/#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=428\/#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=428\/#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=428\/#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=428\/#Source\" title=\"Source\">Source<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"4769_%E8%B6%85%E7%BA%A7%E8%B4%9E%E9%B1%BC\"><\/span>4769: \u8d85\u7ea7\u8d1e\u9c7c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><span class=\"green\">Time Limit:\u00a01 Sec\u00a0\u00a0Memory Limit:\u00a0128 MB<br \/>\n<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\u9a6c\u8fbe\u52a0\u65af\u52a0\u8d1e\u9c7c\u662f\u4e00\u79cd\u795e\u5947\u7684\u53cc\u811a\u8d1e\u9c7c\uff0c\u5b83\u4eec\u628a\u81ea\u5df1\u7684\u667a\u6167\u5199\u5728\u811a\u4e0a\u2014\u2014\u6bcf\u53ea\u8d1e\u9c7c\u7684\u5de6\u811a\u548c\u53f3\u811a\u4e0a\u4e2a\u6709\u4e00\u4e2a\u6570\u3002\u6709\u4e00\u5929\uff0cK\u53ea\u8d1e\u9c7c\u5174\u81f4\u6765\u6f6e(1\u2264k\u226410^5\uff09\uff0c\u6392\u6210\u4e00\u5217\uff0c\u4ece\u5de6\u5230\u53f3\u7b2ci\u53ea\u8d1e\u9c7c\u4f1a\u5728\u53f3\u811a\u5199Ai\uff081\u2264Ai\u226410^9\uff09\uff0c\u5de6\u811a\u4e0a\u5199\u4e0ai\uff081\u2264i\u2264K\uff09\uff0c\u7b2c\u4e8c\u5e74\uff0c\u8fd9K\u53ea\u8d1e\u9c7c\u6309\u53f3\u811a\u7684\u6570\u4ece\u5c0f\u5230\u5927\u6392\u6210\u4e00\u5217\uff0c\u7136\u540e\uff0c\u5b83\u4eec\u51b3\u5b9a\u91cd\u7f16\u53f7\uff0c\u4ece\u5de6\u5230\u53f3\u7b2ci\u53ea\u8d1e\u9c7c\u4f1a\u5728\u53f3\u811a\u4e0a\u5199\u4e0a\u5de6\u811a\u7684\u6570\uff0c\u5728\u5de6\u811a\u4e0a\u5199i\uff0c\u7b2c\u4e09\u5e74\uff0c\u5b83\u4eec\u6309\u7b2c\u4e8c\u5e74\u7684\u65b9\u6cd5\u91cd\u6392\u5217\u3001\u91cd\u7f16\u53f7&#8230;&#8230;n\u5e74\u540e(1\u2264n\u226410^5)\uff0c\u5bf9\u4e8e\u4ece\u5de6\u5230\u53f3\u7b2ci\u548c\u7b2cj\u8d1e\u9c7c\uff0c\u82e5i&lt;j\u4e14\u7b2ci\u53ea\u8d1e\u9c7c\u53f3\u811a\u4e0a\u7684\u6570\u6bd4\u7b2cj\u53ea\u8d1e\u9c7c\u53f3\u811a\u4e0a\u7684\u6570\u5927\uff0c\u5219\u79f0\u5b83\u4eec\u4e3a\u4e00\u5bf9\u201c\u8d85\u7ea7\u8d1e\u9c7c\u201d\u3002\u95ee\u4e00\u5171\u6709\u591a\u5c11\u5bf9\u201c\u8d85\u7ea7\u8d1e\u9c7c\u201d\u3002<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\u4e00\u51713\u884c\uff0c\u7b2c\u4e00\u884c\u4e00\u4e2a\u6b63\u6574\u6570k(1\u2264k\u226410^5\uff09\uff0c\u7b2c\u4e8c\u884ck\u4e2a\u6570\u4ece\u5de6\u5230\u53f3\u8f93\u5165Ai\uff081\u2264Ai\u226410^9\uff09\uff0c\u7b2c\u4e09\u884c\u4e00\u4e2a\u6b63\u6574\u6570n(1\u2264n\u226410^5\uff09\u3002<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u201c\u8d85\u7ea7\u8d1e\u9c7c\u201d\u5bf9\u6570\u3002<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class=\"content\"><span class=\"sampledata\">6\r\n5 2 6 3 1 7\r\n0<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class=\"content\"><span class=\"sampledata\">7<\/span><\/pre>\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>\u5bf9\u4e8e\u5168\u90e8\u6570\u636e\uff1aAi\u226410^9\u3002<br \/>\n30%\u7684\u6570\u636e\uff1an,k&lt;=400;<br \/>\n70%\u7684\u6570\u636e\uff1an,k&lt;=10000;<br \/>\n100%\u7684\u6570\u636e\uff1an,k&lt;=100000;<br \/>\n\u547d\u9898by\u00a0<a class=\"ke-insertfile\" href=\"http:\/\/rainbowoj.hustoj.com\/userinfo.php?user=wangjincheng\" target=\"_blank\" rel=\"noopener\">benny<\/a><\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Source\"><\/span>Source<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p><a href=\"http:\/\/rainbowoj.hustoj.com\/problemset.php?search=ROJ%E5%8E%9F%E5%88%9B\" target=\"_blank\" rel=\"noopener\">ROJ\u539f\u521b<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>upd: \u597d\u5427\uff0c\u7ecf\u8fc7\u51fa\u9898\u4eba\u7684\u4e0d\u61c8\u52aa\u529b\uff0c\u6211\u7684\u7a0b\u5e8f\u7ec8\u4e8eTLE\u4e86\u3002\u3002qwq<\/p>\n<p>\u90a3\u4e48\u4f18\u5316\u4e00\u4e0b\uff0c\u542c\u5510\u5927\u7237\u8bf4\u4e0d\u8bba\u600e\u4e48\u53d8\u6362\uff0c\u9006\u5e8f\u5bf9\u7684\u6570\u603b\u662f\u4e0d\u53d8\u7684\uff08\u4f3c\u4e4e\u597d\u6709\u9053\u7406qaq\uff09\uff0c\u90a3\u4e48\u79bb\u6563\u5316\u4ec0\u4e48\u7684\u90fd\u4e0d\u9700\u8981\u8fa3\uff0c\u53ea\u8981\u4e00\u6b21\u5f52\u5e76\u6392\u5e8f\u5c31\u597d<\/p>\n<pre class=\"lang:c++ decode:true \">#include&lt;map&gt;\r\n#include&lt;cmath&gt;\r\n#include&lt;queue&gt;\r\n#include&lt;cstdio&gt;\r\n#include&lt;cstring&gt;\r\n#include&lt;iostream&gt;\r\n#include&lt;algorithm&gt;\r\nusing namespace std;\r\n#define ll long long\r\n#define N 2000100 \r\ninline int read()\r\n{\r\n    int x=0,f=1;char ch=getchar();\r\n    while(ch&lt;'0'||ch&gt;'9'){if(ch=='-')f=-1;ch=getchar();}\r\n    while(ch&gt;='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\r\n    return x*f;\r\n}\r\nint a[N],tmp[N],k;\r\nll n,ji;\r\nvoid gb(int l,int r)\r\n{\r\n    if(l==r) return;\r\n    int mid=(l+r)&gt;&gt;1,cnt=l,h1=l,h2=mid+1;\r\n    gb(l,mid);gb(mid+1,r);\r\n    while(h1&lt;=mid&amp;&amp;h2&lt;=r)\r\n    {\r\n        while(a[h1]&gt;a[h2])\r\n        {\r\n            tmp[cnt++]=a[h2];\r\n            h2++;\r\n            ji+=mid-h1+1;\r\n            if(h2&gt;r) break;\r\n        }\r\n        tmp[cnt++]=a[h1];\r\n        h1++;\r\n    }\r\n    for(int i=h1;i&lt;=mid;i++) tmp[cnt++]=a[i];\r\n    for(int i=h2;i&lt;=r;i++) tmp[cnt++]=a[i];\r\n    for(int i=l;i&lt;=r;i++) a[i]=tmp[i];\r\n}\r\nint main()\r\n{\r\n    scanf(\"%lld\",&amp;n);\r\n    for(int i=1;i&lt;=n;i++) a[i]=read();\r\n    k=read();gb(1,n);\r\n    printf(\"%lld\\n\",ji);\r\n}<\/pre>\n<p>\u5f52\u5e76\u6392\u5e8f\u88f8\u9898\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u5f53\u91cd\u6392\u6b21\u6570 n \u4e3a\u5076\u6570\u65f6\uff0c\u5176\u7b49\u4ef7\u4e8e\u539f\u6570\u5217\uff0c\uff08\u5176\u5b9e\u5c31\u662f\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4e8c\u5143\u7684\u7ed3\u6784\u4f53\uff0c\u4e00\u6b21\u6309\u7b2c\u4e00\u4f4d\u6392\u5e8f\uff0c\u7b2c\u4e8c\u6b21\u6309\u7b2c\u4e8c\u4f4d\u6392\u5e8f\uff09\uff0c\u7136\u540e\u5c31\u662f\u76f8\u5f53\u4e8e\u6c42\u4e00\u4e2a\u6570\u5217\u7684\u9006\u5e8f\u5bf9\u4e2a\u6570<\/p>\n<p>\u521d\u59cb\u7684\u65f6\u5019\u5148\u79bb\u6563\u5316\u4e00\u4e0b\u5c31\u597d\uff08TLE\uff09<\/p>\n<pre class=\"lang:c++ decode:true \">#include&lt;map&gt;\r\n#include&lt;cmath&gt;\r\n#include&lt;queue&gt;\r\n#include&lt;cstdio&gt;\r\n#include&lt;cstring&gt;\r\n#include&lt;iostream&gt;\r\n#include&lt;algorithm&gt;\r\nusing namespace std;\r\n#define ll long long\r\n#define N 1000100 \r\ninline int read()\r\n{\r\n    int x=0,f=1;char ch=getchar();\r\n    while(ch&lt;'0'||ch&gt;'9'){if(ch=='-')f=-1;ch=getchar();}\r\n    while(ch&gt;='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\r\n    return x*f;\r\n}\r\nint n,a[N],tmp[N],k;\r\nll ji;\r\nvoid gb(int l,int r)\r\n{\r\n    if(l==r) return;\r\n    int mid=(l+r)&gt;&gt;1,cnt=l,h1=l,h2=mid+1;\r\n    gb(l,mid);\r\n    gb(mid+1,r);\r\n    while(h1&lt;=mid&amp;&amp;h2&lt;=r)\r\n    {\r\n        while(a[h1]&gt;a[h2])\r\n        {\r\n            tmp[cnt++]=a[h2];\r\n            h2++;\r\n            ji+=mid-h1+1;\r\n            if(h2&gt;r) break;\r\n        }\r\n        tmp[cnt++]=a[h1];\r\n        h1++;\r\n    }\r\n    for(int i=h1;i&lt;=mid;i++) tmp[cnt++]=a[i];\r\n    for(int i=h2;i&lt;=r;i++) tmp[cnt++]=a[i];\r\n    for(int i=l;i&lt;=r;i++) a[i]=tmp[i];\r\n}\r\nstruct qaz{int x,p;}tp[N];\r\nbool cmp(qaz q,qaz z){if(q.x==z.x)return q.p&lt;z.p;return q.x&lt;z.x;}\r\nint main()\r\n{\r\n    n=read();\r\n    for(int i=1;i&lt;=n;i++){tp[i].x=read();tp[i].p=i;}\r\n    sort(tp+1,tp+n+1,cmp);\r\n    for(int i=1;i&lt;=n;i++) a[tp[i].p]=i;\r\n    k=read();\r\n    if(k&amp;1)\r\n    {\r\n        for(int i=1;i&lt;=n;i++){tp[i].x=a[i];tp[i].p=i;}\r\n        sort(tp+1,tp+n+1,cmp);\r\n        for(int i=1;i&lt;=n;i++) a[i]=tp[i].p;\r\n    }\r\n    gb(1,n);\r\n    printf(\"%lld\\n\",ji);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>4769: \u8d85\u7ea7\u8d1e\u9c7c Time Limit:\u00a01 Sec\u00a0\u00a0Memory Limit:\u00a0128 MB Desc [&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":[44],"class_list":["post-428","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-44"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/428","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=428"}],"version-history":[{"count":1,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/428\/revisions"}],"predecessor-version":[{"id":429,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/428\/revisions\/429"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}