{"id":256,"date":"2023-07-19T14:41:43","date_gmt":"2023-07-19T06:41:43","guid":{"rendered":"https:\/\/solost.cn\/?p=256"},"modified":"2023-07-19T14:41:43","modified_gmt":"2023-07-19T06:41:43","slug":"385","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=256","title":{"rendered":"\u201c\u8303\u5f0f\u676f\u201d2023\u725b\u5ba2\u6691\u671f\u591a\u6821\u8bad\u7ec3\u84251 &#8211; H &#8211; Matches"},"content":{"rendered":"<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/ac.nowcoder.com\/acm\/contest\/57355\/H\">\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u76ee\u5927\u610f<\/h2>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u5e8f\u5217ai,bi,\u73b0\u5728\u53ef\u4ee5\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\u5e8f\u5217\u4ea4\u6362\u5176\u4e2d\u7684\u4e24\u4e2a\u6570\u5b57\uff0c\u95ee\u7ecf\u8fc7\u81f3\u591a\u4e00\u6b21\u64cd\u4f5c\u540e\u6700\u5c0f\u7684|ai - bi|\u7684\u603b\u548c\u662f\u591a\u5c11\uff1f<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u53c2\u8003\u4e86\u8fd9\u7bc7<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/644182417\">\u9898\u89e3<\/a>\uff0c\u53ef\u4ee5\u5f97\u51fa\u6211\u4eec\u8981\u60f3\u628a\u4ed6\u51cf\u5c0f\uff0c\u90a3\u6211\u4eec\u5fc5\u987b\u4ea4\u6362\u4e24\u79cd\u4e0d\u540c\u5f62\u5f0f\u7684\u5e8f\u5217\uff0c\u7136\u540e\u95ee\u9898\u5c31\u8f6c\u6362\u6210\u4e3a\u6c42\u82e5\u5e72\u5e8f\u5217\u4e2d\u6700\u957f\u516c\u5171\u5e8f\u5217\u957f\u5ea6\u7684\u95ee\u9898<\/p>\n<h2>\u4ee3\u7801<\/h2>\n<pre><code class=\"lang-cpp\">#include &lt;algorithm&gt;\n#include &lt;array&gt;\n#include &lt;bitset&gt;\n#include &lt;cmath&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;queue&gt;\n#include &lt;set&gt;\n#include &lt;stack&gt;\n#include &lt;unordered_map&gt;\n#include &lt;unordered_set&gt;\n#include &lt;vector&gt;\n\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS                       \\\n    ios_base::sync_with_stdio(0); \\\n    cin.tie(0);                   \\\n    cout.tie(0);\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair&lt;ll, ll&gt; pll;\ntypedef pair&lt;int, int&gt; pii;\ntypedef unsigned long long ull;\n\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\n\nvoid solve()\n{\n    int n;\n    cin &gt;&gt; n;\n    vector&lt;int&gt; a(n), b(n);\n    for (int i = 0; i &lt; n; i++) {\n        cin &gt;&gt; a[i];\n    }\n    for (int i = 0; i &lt; n; i++) {\n        cin &gt;&gt; b[i];\n    }\n\n    ll sum = 0;\n    vector&lt;array&lt;int, 3&gt;&gt; seg(n);\n    for (int i = 0; i &lt; n; i++) {\n        sum += abs(a[i] - b[i]);\n        if (a[i] &lt; b[i])\n            seg[i] = { a[i], b[i], 0 };\n        else\n            seg[i] = { b[i], a[i], 1 };\n    }\n    sort(seg.begin(), seg.end());\/\/\u6309\u7167\u5de6\u7aef\u70b9\u6392\u5e8f\n    int mx[2] = { (int)-2e9, (int)-2e9 };\n    ll ans = sum;\n    for(auto [l, r, f] : seg){\n        auto mxx = mx[f ^ 1];\/\/\u7531\u4e4b\u524d\u7684\u63a8\u8bba\u53ef\u5f97\u5339\u914d\u4e0d\u540c\u7c7b\u578b\u7684\u624d\u80fd\u4f7f\u5f97\u548c\u8d8a\u5c0f\n        if(mxx &gt; l)\n            ans = min(ans, sum - 2ll * (min(r, mxx) - l));\n        \/\/\u5f53\u524d\u7ebf\u6bb5\u7684\u5de6\u7aef\u70b9\u4e00\u5b9a\u8981\u5c0f\u4e8e\u4e0b\u4e00\u4e2a\u7ebf\u6bb5\u7684\u5de6\u7aef\u70b9\uff0c\u4e00\u5b9a\u662f\u80fd\u5305\u88f9\u4e0b\u4e00\u4e2a\u7ebf\u6bb5\u7684\u5de6\u7aef\u70b9\u7684\uff0c\n        \/\/\u7136\u540e\u6700\u957f\u516c\u5171\u5c31\u53d6\u51b3\u4e8e\u5f53\u524d\u7ebf\u6bb5\u7684\u53f3\u7aef\u70b9\u8fd8\u662f\u5df2\u7ecf\u5339\u914d\u8fc7\u7ebf\u6bb5\u7684\u6700\u957f\u53f3\u7aef\u70b9\u7684\u5dee\u503c\n        mx[f] = max(r, mx[f]);\n    }\n    cout &lt;&lt; ans &lt;&lt; endl;\n\n}\nint main()\n{\n    \/\/ IOS;\n    int _ = 1;\n    \/\/ cin &gt;&gt; _;\n    while (_--) {\n        solve();\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u94fe\u63a5 \u94fe\u63a5 \u9898\u76ee\u5927\u610f \u7ed9\u5b9a\u4e24\u4e2a\u5e8f\u5217ai,bi,\u73b0\u5728\u53ef\u4ee5\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\u5e8f\u5217\u4ea4\u6362\u5176\u4e2d\u7684\u4e24\u4e2a\u6570\u5b57\uff0c\u95ee\u7ecf\u8fc7\u81f3\u591a\u4e00\u6b21\u64cd\u4f5c\u540e\u6700\u5c0f\u7684|ai - bi|\u7684\u603b\u548c\u662f\u591a\u5c11\uff1f \u9898\u89e3 \u53c2\u8003\u4e86\u8fd9\u7bc7\u9898\u89e3\uff0c\u53ef\u4ee5\u5f97\u51fa\u6211\u4eec\u8981\u60f3\u628a\u4ed6\u51cf\u5c0f\uff0c\u90a3\u6211\u4eec\u5fc5\u987b\u4ea4\u6362\u4e24\u79cd\u4e0d\u540c\u5f62\u5f0f\u7684\u5e8f\u5217\uff0c\u7136\u540e\u95ee\u9898\u5c31\u8f6c\u6362\u6210\u4e3a\u6c42\u82e5\u5e72\u5e8f\u5217\u4e2d\u6700\u957f\u516c\u5171\u5e8f\u5217\u957f\u5ea6\u7684\u95ee\u9898 \u4ee3\u7801 #include &lt;algorithm&gt; #include &lt;array&gt; #include &lt;bitset&gt; #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;queue&gt; #include &lt;set&gt; #include &lt;stack&gt; #include &lt;unordered_map&gt; #include &lt;unordered_set&gt; #include &lt;vector&gt; #define x first #define y second #define endl &#039;\\n&#039; #define IOS \\ ios_base::sync_with_stdio(0); \\ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5,7],"class_list":["post-256","post","type-post","status-publish","format-standard","hentry","category-solution","tag-5","tag-7"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=256"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/256\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}