{"id":246,"date":"2023-07-03T16:04:30","date_gmt":"2023-07-03T08:04:30","guid":{"rendered":"https:\/\/solost.cn\/?p=246"},"modified":"2023-07-03T16:04:30","modified_gmt":"2023-07-03T08:04:30","slug":"374","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=246","title":{"rendered":"AtCoder Beginner Contest 306 &#8211; E &#8211; Best Performances"},"content":{"rendered":"<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/atcoder.jp\/contests\/abc306\/tasks\/abc306_e\">\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u76ee\u5927\u610f<\/h2>\n<p>\u9898\u76ee\u610f\u601d\u662f\uff0c\u957f\u5ea6\u4e3an\u7684\u5e8f\u5217\uff0cq\u6b21\u8be2\u95ee\uff0c\u6bcf\u6b21\u8f93\u5165p,w\u628aa[p]\u6539\u6210y,\u95ee\u4f60\u524dk\u5927\u7684\u5143\u7d20\u548c\u662f\u591a\u5c11<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u8fd9\u4e2a\u9898\u76ee\u7684\u96be\u70b9\u662f\u8981\u6bcf\u6b21\u4fee\u6539\u5143\u7d20\u540e\u8fd8\u8981\u6c42\u5e8f\u5217\u7684\u524dk\u5927\uff0c\u8fd9\u5c31\u6709\u70b9\u9ebb\u70e6\u4e86\uff0c\u5982\u679c\u4f7f\u7528\u66b4\u529b\u4e0d\u53ef\u80fd\u80fd\u8fc7\uff0cn,k=5e5;\u6240\u4ee5\u4f7f\u7528\u65b0\u7684\u4e00\u4e2aSTL\u5bb9\u5668<code>multiset<\/code>\uff0c\u8fd9\u4e2a\u5bb9\u5668\u548cset\u7684\u533a\u522b\u5c31\u662f\u8fd9\u4e2a\u5bb9\u5668\u53ef\u4ee5\u5b58\u50a8\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\u3002<\/p>\n<p>\u8fd8\u6709\u8981\u6ce8\u610f\u4e00\u4e2a\u70b9\uff0c\u5982\u679c\u4f60\u8981erase\u4e00\u4e2a\u5143\u7d20\uff0c\u4e0d\u8981\u8fd9\u8981\u79fb\u9664\uff0c<code>y.erase(*x.begin())<\/code>\uff0c\u6700\u597d\u5148\u7528\u4e00\u4e2a\u53d8\u91cf\u628a<code>*x.begin()<\/code>\u5b58\u8d77\u6765\uff0c\u4f8b\u5982<code>int t = *x.begin(), y.erase(t);<\/code><\/p>\n<pre><code>std::multiset&lt;int&gt; s;    \/\/ \u5b9a\u4e49 int \u7c7b\u578b\u7684\u591a\u91cd\u96c6 s\ns.insert(x);             \/\/ \u5411 s \u91cc\u9762\u63d2\u5165\u5143\u7d20 x\ns.erase(x);             \/\/ \u5220\u9664 s \u4e2d\u6240\u6709\u7b49\u4e8e x \u7684\u5143\u7d20\ns.extract(x);           \/\/ \u5220\u9664 s \u4e2d\u7684\u67d0\u4e00\u4e2a\u7b49\u4e8e x \u7684\u5143\u7d20<\/code><\/pre>\n<p>\u6211\u4eec\u9700\u8981\u6bcf\u6b21\u8be2\u95ee\u540e\u8f93\u51fa\u524dk\u5927\u5143\u7d20\u7684\u548c\u3002\u7528\u4e00\u4e2aset\u6765\u4e3a\u7ef4\u62a4\u524dk\u5927\u5143\u7d20x\uff0c\u53e6\u4e00\u4e2a\u7ef4\u62a4n-k\u5143\u7d20y\uff0c\u6bcf\u6b21\u5c06\u65b0\u5143\u7d20\u63d2\u5165y\u4e2d\uff0c\u7136\u540e\u6765\u8ba1\u7b97\uff0c\u5982\u679cx\u7684\u4e2a\u6570\u4e0d\u591fk\u4e2a\uff0c\u4ecey\u4e2d\u53d6\uff0c\u76f4\u5230\u8fbe\u5230k\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u591f\u4e86\uff0c\u7136\u540e\u6bd4\u8f83\uff0c\u6709\u6ca1\u6709y\u4e2d\u7684\u5143\u7d20\u5927\u5c0f\u8fd8\u8981\u6bd4x\u7684\u5927\uff0c\u5982\u679c\u6709\u5c31\u4ea4\u6362\uff0c\u6bcf\u6b21\u8ba1\u7b97\u4e00\u4e0b\u603b\u548c\u5c31\u597d\uff0cs\u4e0d\u662f\u91cd\u65b0\u7b97\uff0c\u800c\u662f\u51cf\u53bb\u4e86\u54ea\u4e2a\u5143\u7d20\uff0c\u518d\u52a0\u8fdb\u6765\u54ea\u4e2a\u5143\u7d20\u3002<\/p>\n<pre><code class=\"lang-cpp\">#include &lt;algorithm&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 p first\n#define w 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 = 5e5 + 10;\n\nint n, k, q;\nll s;\nmultiset&lt;int&gt; x, y;\n\nvoid balance()\n{\n    while (x.size() &lt; k) {\n        auto iy = y.end();\n        iy--;\n        x.insert((*iy));\n        s += (*iy);\n        y.erase(iy);\n    }\n    if (y.empty() || x.empty())\n        return;\n\n    while (1) {\n        auto ix = x.begin();\n        auto iy = y.end();\n        iy--;\n        int ex = (*ix);\n        int ey = (*iy);\n        int t = ey - ex;\n        if (t &lt;= 0) return;\n        s += t;\n        x.erase(ix);\n        x.insert(ey);\n        y.erase(iy);\n        y.insert(ex);\n    }\n}\n\nvoid add(int v)\n{\n    y.insert(v);\n    balance();\n}\n\nvoid erase(int v)\n{\n    auto ix = x.find(v);\n    if (ix != x.end()) {\n        s -= v;\n        x.erase(ix);\n    } else\n        y.erase(y.find(v));\n    balance();\n}\n\nvoid solve()\n{\n    cin &gt;&gt; n &gt;&gt; k &gt;&gt; q;\n    vector&lt;int&gt; a(n, 0);\n    for (int i = 0; i &lt; k; i++) {\n        x.insert(0);\n    }\n    for (int i = k; i &lt; n; i++) {\n        y.insert(0);\n    }\n\n    while (q--) {\n        int p, w;\n        cin &gt;&gt; p &gt;&gt; w;\n        p--;\n        add(w);\n        erase(a[p]);\n        a[p] = w;\n        cout &lt;&lt; s &lt;&lt; endl;\n    }\n}\nint main()\n{\n    IOS;\n    int t = 1;\n    \/\/ cin &gt;&gt; t;\n    while (t--) {\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 \u9898\u76ee\u610f\u601d\u662f\uff0c\u957f\u5ea6\u4e3an\u7684\u5e8f\u5217\uff0cq\u6b21\u8be2\u95ee\uff0c\u6bcf\u6b21\u8f93\u5165p,w\u628aa[p]\u6539\u6210y,\u95ee\u4f60\u524dk\u5927\u7684\u5143\u7d20\u548c\u662f\u591a\u5c11 \u9898\u89e3 \u8fd9\u4e2a\u9898\u76ee\u7684\u96be\u70b9\u662f\u8981\u6bcf\u6b21\u4fee\u6539\u5143\u7d20\u540e\u8fd8\u8981\u6c42\u5e8f\u5217\u7684\u524dk\u5927\uff0c\u8fd9\u5c31\u6709\u70b9\u9ebb\u70e6\u4e86\uff0c\u5982\u679c\u4f7f\u7528\u66b4\u529b\u4e0d\u53ef\u80fd\u80fd\u8fc7\uff0cn,k=5e5;\u6240\u4ee5\u4f7f\u7528\u65b0\u7684\u4e00\u4e2aSTL\u5bb9\u5668multiset\uff0c\u8fd9\u4e2a\u5bb9\u5668\u548cset\u7684\u533a\u522b\u5c31\u662f\u8fd9\u4e2a\u5bb9\u5668\u53ef\u4ee5\u5b58\u50a8\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\u3002 \u8fd8\u6709\u8981\u6ce8\u610f\u4e00\u4e2a\u70b9\uff0c\u5982\u679c\u4f60\u8981erase\u4e00\u4e2a\u5143\u7d20\uff0c\u4e0d\u8981\u8fd9\u8981\u79fb\u9664\uff0cy.erase(*x.begin())\uff0c\u6700\u597d\u5148\u7528\u4e00\u4e2a\u53d8\u91cf\u628a*x.begin()\u5b58\u8d77\u6765\uff0c\u4f8b\u5982int t = *x.begin(), y.erase(t); std::multiset&lt;int&gt; s; \/\/ \u5b9a\u4e49 int \u7c7b\u578b\u7684\u591a\u91cd\u96c6 s s.insert(x); \/\/ \u5411 s \u91cc\u9762\u63d2\u5165\u5143\u7d20 x s.erase(x); \/\/ \u5220\u9664 s \u4e2d\u6240\u6709\u7b49\u4e8e x \u7684\u5143\u7d20 s.extract(x); \/\/ \u5220\u9664 s \u4e2d\u7684\u67d0\u4e00\u4e2a\u7b49\u4e8e x \u7684\u5143\u7d20 \u6211\u4eec\u9700\u8981\u6bcf\u6b21\u8be2\u95ee\u540e\u8f93\u51fa\u524dk\u5927\u5143\u7d20\u7684\u548c\u3002\u7528\u4e00\u4e2aset\u6765\u4e3a\u7ef4\u62a4\u524dk\u5927\u5143\u7d20x\uff0c\u53e6\u4e00\u4e2a\u7ef4\u62a4n-k\u5143\u7d20y\uff0c\u6bcf\u6b21\u5c06\u65b0\u5143\u7d20\u63d2\u5165y\u4e2d\uff0c\u7136\u540e\u6765\u8ba1\u7b97\uff0c\u5982\u679cx\u7684\u4e2a\u6570\u4e0d\u591fk\u4e2a\uff0c\u4ecey\u4e2d\u53d6\uff0c\u76f4\u5230\u8fbe\u5230k\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u591f\u4e86\uff0c\u7136\u540e\u6bd4\u8f83\uff0c\u6709\u6ca1\u6709y\u4e2d\u7684\u5143\u7d20\u5927\u5c0f\u8fd8\u8981\u6bd4x\u7684\u5927\uff0c\u5982\u679c\u6709\u5c31\u4ea4\u6362\uff0c\u6bcf\u6b21\u8ba1\u7b97\u4e00\u4e0b\u603b\u548c\u5c31\u597d\uff0cs\u4e0d\u662f\u91cd\u65b0\u7b97\uff0c\u800c\u662f\u51cf\u53bb\u4e86\u54ea\u4e2a\u5143\u7d20\uff0c\u518d\u52a0\u8fdb\u6765\u54ea\u4e2a\u5143\u7d20\u3002 #include &lt;algorithm&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; [&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":[17,5,9],"class_list":["post-246","post","type-post","status-publish","format-standard","hentry","category-solution","tag-stl","tag-5","tag-9"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/246","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=246"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/246\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}