{"id":365,"date":"2018-08-02T14:17:59","date_gmt":"2018-08-02T14:17:59","guid":{"rendered":"https:\/\/www.haoyuan.info\/?p=365"},"modified":"2018-08-02T14:17:59","modified_gmt":"2018-08-02T14:17:59","slug":"%e7%89%9b%e5%ae%a2%e5%a4%9a%e6%a0%a1%e7%ac%ac%e4%ba%94%e5%9c%ba-d-inv","status":"publish","type":"post","link":"https:\/\/haoyuan.info\/?p=365","title":{"rendered":"\u725b\u5ba2\u591a\u6821\u7b2c\u4e94\u573a D.inv"},"content":{"rendered":"<p>\u94fe\u63a5\uff1ahttps:\/\/www.nowcoder.com\/acm\/contest\/143\/D<br \/>\n\u6765\u6e90\uff1a\u725b\u5ba2\u7f51<\/p>\n<h2>\u9898\u9762<\/h2>\n<p>Kanade has an even number n and a permutation b of all of the even numbers in [1,n]<br \/>\nLet a denote an array [1,3,5&#8230;.n-1]\u00a0, now you need to find a permutation of [1,n] satisfy both a\u00a0and b\u00a0are subsequence of it and minimize the number of inverse pair of it.<\/p>\n<h2>\u9898\u610f<\/h2>\n<p>\u7ed9\u5b9a\u4e00\u4e2a [1,n] \u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u6392\u5217 b\uff0c\u5176\u4e2d n \u662f\u5076\u6570 \u73b0\u5728\u6709\u4e00\u4e2a\u6570\u7ec4 a=[1,3,5&#8230;..n-1]<br \/>\n\u8981\u6c42\u5f52\u5e76 a \u548c b\uff0c\u4f7f\u5f97\u4ed6\u4eec\u5f52\u5e76\u540e\u9006\u5e8f\u5bf9\u6570\u91cf\u6700\u5c11 1&lt;=n&lt;=200000<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u6ca1\u6709\u770b\u8fd9\u9898\uff0c\u7b49\u7740\u8eab\u4f53\u8212\u670d\u70b9\u518d\u770b\u53d1\u73b0\u662f\u677f\u9898\uff0c\u5f88\u96be\u53d7\u3002<br \/>\n\u76f8\u5f53\u4e8e\u662f\u8981\u5c06\u6bcf\u4e2a 2i+1 \u63d2\u5165\u5230 b \u4e2d \u901a\u8fc7\u63a8\u5bfc\u53ef\u4ee5\u53d1\u73b0\uff0c\u6bcf\u4e2a2i+1\u63d2\u5165\u5230 b \u4e2d\u7684\u6700\u4f18\u4f4d\u7f6e\u4e00\u5b9a\u662f\u9012\u589e\u7684 \u6240\u4ee5\u76f4\u63a5\u5bf9\u4e8e\u6bcf\u4e2a 2i+1 \u8ba1\u7b97\u4ed6\u80fd\u4ea7\u751f\u7684\u6700\u5c11\u7684\u9006\u5e8f\u5bf9\u6570\u91cf\uff0c\u52a0\u8d77\u6765\u5373\u53ef<br \/>\n\u6811\u72b6\u6570\u7ec4\u7b97\u9006\u5143 \u7136\u540e\u7ef4\u62a4\u4e00\u4e2a\u6700\u5927\u5806\uff0c\u8d2a\u5fc3\u7684\u5c06\u76ee\u6807\u6570\u5b57\u653e\u5230\u6700\u5927\u503c\u7684\u540e\u9762\u5373\u53ef<\/p>\n<h2>AC\u4ee3\u7801<\/h2>\n<pre><code class=\" line-numbers\">#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\n#include &lt;cstring&gt;\n#include &lt;queue&gt;\n#include &lt;array&gt;\n\n#define lowbit(x) ((x) &amp; (-x))\n\nusing namespace std;\nconst int maxn = 3e5 + 6;\nusing ll = long long;\nint n, a[maxn], tree[maxn];\nll ans = 0;\npriority_queue&lt;int&gt; q;\n\nvoid add(int x) {\n    for (; x &lt;= n; x += lowbit(x))++tree[x];\n}\n\nint sum(int x) {\n    int ret = 0;\n    for (; x; x -= lowbit(x))ret += tree[x];\n    return ret;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n    cout.tie(0);\n    cin &gt;&gt; n;\n    n &gt;&gt;= 1;\n    for (int i = 1; i &lt;= n; i++)cin &gt;&gt; a[i];\n    for (int i = 1; i &lt;= n; i++)\n        add(a[i] &gt;&gt;= 1), ans += i - sum(a[i]);\n    for (int i = 1; i &lt;= n; i++) {\n        q.push(a[i]);\n        if (q.top() &gt; a[i])\n            ans += q.top() - a[i], q.pop(), q.push(a[i]);\n    }\n    cout &lt;&lt; ans &lt;&lt; \"\\n\";\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u94fe\u63a5\uff1ahttps:\/\/www.nowcoder.com\/acm\/contest\/143\/D \u6765\u6e90\uff1a\u725b\u5ba2\u7f51 \u9898\u9762 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[39,48,49,22],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-c","category-48","category-49","category-22"],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8UC2c-5T","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=365"}],"version-history":[{"count":1,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":366,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions\/366"}],"wp:attachment":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}