{"id":1914,"date":"2018-07-07T09:45:55","date_gmt":"2018-07-07T01:45:55","guid":{"rendered":"http:\/\/47.101.202.111\/?p=1914"},"modified":"2023-04-07T12:34:03","modified_gmt":"2023-04-07T04:34:03","slug":"pat-advanced-25points","status":"publish","type":"post","link":"http:\/\/139.196.114.170\/?p=1914","title":{"rendered":"PAT \u2013 ADVANCED \u2013 25POINTS (UPDATING)"},"content":{"rendered":"<h2>\u6808<\/h2>\n<h3>A1051 \u51fa\u6808\u5e8f\u5217<\/h3>\n<p>\u2460\u6309\u5e8f\u4e00\u76f4\u8fdb\u6808\uff0c\u6dfb\u52a0\u5143\u7d20\u5e94\u67e5\u5bb9\u91cf\u9632\u6b62\u8d85\u9650<br \/>\n\u2461\u6808\u9876\u5143\u7d20\u548c\u5e8f\u5217\u5339\u914d\u4e0a\uff0c\u5c31\u53ef\u4ee5\u7ee7\u7eed\u4e0b\u6b21\u5339\u914d<\/p>\n<pre><code class=\"language-cpp \">#include &lt;iostream&gt;\n#include &lt;stack&gt;\nusing namespace std;\n\nint main() {\n    int M, N, K, c, k;\n    int s[1000];\n    stack&lt;int&gt; stk;\n    bool f;\n    scanf(\"%d%d%d\", &amp;M, &amp;N, &amp;K);\n\n    for (int i = 0; i &lt; K; i++) {\n        c = k = 0;\n        f = true;\n        for (int j = 0; j &lt; N; j++)\n            scanf(\"%d\", &amp;s[j]);\n        while (!stk.empty())\n            stk.pop();\n\n        while (c &lt; N) {\n            stk.push(++c);\n            if (stk.size() &gt; M) {\n                f = false;\n                break;\n            }\n            while (!stk.empty() &amp;&amp; stk.top() == s[k]) {\n                stk.pop();\n                k++;\n            }\n        }\n\n        if (stk.empty() &amp;&amp; f) printf(\"YES\\n\");\n        else printf(\"NO\\n\");\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<h2>\u961f\u5217<\/h2>\n<h3>A1056 \u987a\u5e8f\u5bf9\u51b3<\/h3>\n<p>\u2460\u6bcf\u7ec4\u51b3\u51fa\u8d62\u5bb6\u7ee7\u7eed\u5bf9\u51b3\uff0c\u6bcf\u8f6e\u6309\u5e8f\u5bf9\u51b3\uff0c\u7b26\u5408FIFO\u4f7f\u7528\u961f\u5217\uff0c\u961f\u5217q\u6309\u5e8f\u4fdd\u5b58\u5f53\u524d\u8f6e\u9009\u624b<br \/>\n\u2461\u89e3\u51b3\u664b\u7ea7\u95ee\u9898\uff1a\u6bcf\u8f6e\u6e05\u7a7aq\uff0c\u664b\u7ea7\u7684\u4eba\u5b58\u5165q1\uff0c\u4e0b\u4e00\u8f6e\u5c06q\u7f6e\u4e3aq1\u3002\u603b\u5bf9\u51b3\u8f6e\u6570 $\\scriptsize ct0 = \\left \\lceil \\frac{np}{ng} \\right \\rceil$<br \/>\n\u2462\u89e3\u51b3\u5206\u7ec4\u95ee\u9898\uff1a\u6bcf\u8f6e $\\scriptsize ct = \\left \\lceil \\frac{q.size()}{ng} \\right \\rceil$ \u7ec4\uff0c\u6bcfng\u4e2a\u4eba\u6309\u5e8f\u6bd4\u5927\u5c0f\uff0c\u6700\u540e\u4e0d\u6ee1ng\u65f6\u961f\u5217\u7a7a\u5219\u8df3\u51fa\uff0c\u5b9e\u73b0\u6700\u540e\u4e00\u7ec4\u65e0\u8bba\u51e0\u4e2a\u4eba\u81ea\u6210\u4e00\u7ec4\u3002<br \/>\n\u2463\u89e3\u51b3\u6807\u53f7\u95ee\u9898\uff1a\u6dd8\u6c70\u7684\u4eba\u7ed9\u5176\u6807\u53f7n\uff0c\u5012\u7f6e\u5faa\u73af\u5373 $\\scriptsize n = ct + 1$<br \/>\n\u2464\u4f55\u65f6\u7ed3\u675f\uff1a\u961f\u5217\u4e2d\u4ec5\u6709\u4e00\u4e2a\u5143\u7d20\u65f6\u51b3\u51fa\u6700\u7ec8\u8d62\u5bb6<br \/>\n\u5410\u69fd\uff1a\u672c\u9898\u63cf\u8ff0\uff01\u7b2c\u4e00\u6b21\u8bfb\u7684\u65f6\u5019\u6ca1\u61c2\u54c7\u2026\u2026\u53c8\u81ed\u53c8\u957f\u53c8\u96be\u61c2\uff0c(\u30d8\uff65_\uff65)\u30d8\u2533\u2501\u2533\uff0c\u5168\u9760\u6837\u4f8b\u731c<\/p>\n<pre><code class=\"language-cpp \">#include &lt;iostream&gt;\n#include &lt;bits\/stdc++.h&gt;\n#include &lt;queue&gt;\nusing namespace std;\n\nint main() {\n    int np, ng, tp, maxi;\n    queue&lt;int&gt; q;\n    queue&lt;int&gt; q1;\n    scanf(\"%d%d\", &amp;np, &amp;ng);\n    int w[np], n[np];\n    memset(n, 0, sizeof(n));\n    for (int i = 0; i &lt; np; i++)\n        scanf(\"%d\", &amp;w[i]);\n    for (int i = 0; i &lt; np; i++) {\n        scanf(\"%d\", &amp;tp);\n        q.push(tp);\n    }\n\n    while (q.size() &gt; 1) {\n        int ct = q.size() % ng ? q.size() \/ ng + 1 : q.size() \/ ng;\n        for(int k = ct; k &gt; 0; k--) {\n            maxi = q.front();\n            for (int i = 0; i &lt; ng; i++) {\n                if (!q.empty()) {\n                    tp = q.front();\n                    if (w[maxi] &lt; w[tp]) {\n                        n[maxi] = ct + 1;\n                        maxi = tp;\n                    }\n                    else if(w[maxi] &gt; w[tp])\n                        n[tp] = ct + 1;\n                    q.pop();\n                }\n                else break;\n            }\n            q1.push(maxi);\n        }\n\n        while(!q1.empty()) {\n            q.push(q1.front());\n            q1.pop();\n        }\n    }\n\n    n[q.front()]++;\n    int i = 0;\n    for (; i &lt; np - 1; i++)\n        printf(\"%d \", n[i]);\n    printf(\"%d\\n\", n[i]);\n\n    return 0;\n}\n<\/code><\/pre>\n<h2>\u94fe\u8868<\/h2>\n<h3>A1061 \u53cd\u8f6c\u94fe\u8868<\/h3>\n<p>\u8be5\u9898\u9700\u89e3\u51b3\u4e0b\u8ff0\u95ee\u9898\uff1a<br \/>\n\u2460\u5982\u4f55\u8868\u793a\u94fe\u8868<br \/>\n\u2461\u5982\u4f55\u53cd\u8f6c\u94fe\u8868\uff1a\u9012\u5f52\u3001\u6808\u8fed\u4ee3\u548c\u8fed\u4ee3\u6cd5\uff0c\u6b64\u5904\u4f7f\u7528\u8fed\u4ee3\u6cd5\u3002\u8981\u4f7f\u5f53\u524d\u8282\u70b9cur\u6307\u5411\u524d\u4e00\u8282\u70b9pre\uff0c\u5fc5\u987b\u63d0\u524d\u4fdd\u5b58pre\uff1b\u800c\u4fee\u6539\u5f53\u524d\u8282\u70b9\u6307\u9488\u4f1a\u5bfc\u81f4\u4e22\u5931\u6307\u5411\u540e\u7eed\u94fe\u8868\u6307\u9488\uff0c\u6545\u8fd8\u9700\u4fdd\u5b58\u540e\u7eed\u8282\u70b9next<br \/>\n\u2462\u5982\u4f55\u63a7\u5236\u5206\u6bb5\u6570\u91cf<br \/>\n\u2463\u89e3\u51b3\u5206\u6bb5\u4e4b\u95f4\u7684\u53cd\u8f6c<br \/>\n\u2464\u89e3\u51b3\u9996\u5c3e\u95ee\u9898<\/p>\n<h3>A1032 \u76f8\u4ea4\u94fe\u8868<\/h3>\n<p>\u6b64\u9898\u6240\u7ed9\u6570\u636e\u7279\u6b8a\uff0c\u94fe\u8868\u65e0\u73af\u4e14\u5355\u4e32\u5185\u65e0\u91cd\u590d\u5b57\u7b26\uff0c\u5373\u4e3a\u627e\u76f8\u540c\u8282\u70b9\u95ee\u9898\uff0c\u4ee5\u4e0b\u7ed9\u51fa\u53cc\u6307\u9488\u5b9e\u73b0\uff1a<br \/>\n\u2460<strong>\u54c8\u5e0c\u8868<\/strong> \u7b2c\u4e00\u4e32\u8bfb\u5165\u5404\u5b57\u7b26\u5b58set\uff0c\u7b2c\u4e8c\u4e32\u4f9d\u6b21\u5230set\u5339\u914d\u5373\u53ef<br \/>\n\u65f6\u95f4 $\\scriptsize O(n_1+n_2)$\uff0c\u7a7a\u95f4 $\\scriptsize O(n_1)$<br \/>\n\u2461<strong>\u53cc\u6307\u9488<\/strong> \u6307\u5411\u4e24\u4e2a\u4e32\u5934\u7684\u6307\u9488\u540c\u65f6\u4f9d\u6b21\u6bd4\u5bf9\u540e\u79fb\uff0c\u4e24\u6307\u9488\u5fc5\u5728\u7b49\u957f\u4e24\u4e32\u76f8\u4ea4\u5904\u76f8\u9047\u3002\u4e24\u4e32\u957f\u5ea6\u4e0d\u540c\u65f6\u8fd8\u60f3\u7528\u6b64\u65b9\u6cd5\uff0c\u5c31\u8981 <strong>\u6784\u9020\u7b49\u957f\u4e32<\/strong> \u4f7f\u5f97\u4e24\u6307\u9488\u5728\u76f8\u4ea4\u5904\u76f8\u9047\uff0c\u8bbe\u4e24\u4e32\u516c\u5171\u957f\u5ea6 $\\scriptsize l$\uff0c\u5404\u81ea\u7684\u975e\u516c\u5171\u957f\u5ea6 $\\scriptsize a$ \u548c $\\scriptsize b$\uff0c\u5219\u4e24\u4e32\u957f\u5ea6\u5206\u522b\u4e3a $\\scriptsize l + a$ \u548c  $\\scriptsize l + b$\uff0c\u5206\u522b\u53e0\u52a0\u5bf9\u65b9\u7684\u975e\u516c\u5171\u957f\u5ea6\u5373\u53ef\u6784\u9020\u76f8\u4ea4\u5904\u4e3a\u672b\u5c3e\u7684\u76f8\u7b49\u957f\u5ea6 $\\scriptsize l + a + b$ \uff0c\u5999\u86d9\uff01<br \/>\n\u65f6\u95f4 $\\scriptsize O(n_1+n_2)$\uff0c\u7a7a\u95f4 $\\scriptsize O(1)$<\/p>\n<pre><code class=\"language-cpp \">#include &lt;iostream&gt;\nusing namespace std;\n\nconst int maxn = 100001;\n\nstruct Node {\n    char letr;\n    int next;\n    Node(char letr, int next) : letr(letr), next(next) {}\n} *node[maxn];\n\nint main() {\n    int H1, H2, N;\n    scanf(\"%d%d%d\", &amp;H1, &amp;H2, &amp;N);\n    if (H1 == -1 || H2 == -1) {\n        printf(\"-1\\n\");\n        return 0;\n    }\n\n    int a, n; char d;\n    while (~scanf(\"%d %c %d\", &amp;a, &amp;d, &amp;n))\n        node[a] = new Node(d, n);\n\n    int p1 = H1, p2 = H2;\n    while (p1 != p2) {\n        p1 = p1 == -1 ? H2 : node[p1]-&gt;next;\n        p2 = p2 == -1 ? H1 : node[p2]-&gt;next;\n    }\n\n    if (p1 == -1) printf(\"-1\\n\");\n    else printf(\"%05d\\n\", p1);\n\n    return 0;\n}\n<\/code><\/pre>\n<h3>A1052 \u6392\u5e8f\u94fe\u8868<\/h3>\n<p>\u5341\u5927\u6392\u5e8f\u65b9\u6cd5\u4e2d\uff0c\u5f52\u5e76\u6392\u5e8f\u548c\u5feb\u901f\u6392\u5e8f\u65f6\u95f4\u6700\u4f18\uff0c\u4e3a $\\scriptsize O(nlogn)$\uff0c\u4e0b\u5148\u7ed9\u51fa\u94fe\u8868\u5f52\u5e76\u6392\u5e8f\u65b9\u6cd5\uff0c\u7136\u800c\u672c\u9898\u53ef\u4f7f\u7528\u5e93\u51fd\u6570sort\u907f\u514d\u5199\u590d\u6742\u7684\u903b\u8f91<\/p>\n<h4>\u5f52\u5e76\u6392\u5e8f<\/h4>\n<p>\u2460\u5212\u5206\u5b50\u94fe\u8868\uff1a<br \/>\na\uff09\u5212\u5206\u5b50\u96c6[low,mid)\u3001[mid,high]\u8981\u627e\u5230\u94fe\u8868\u4e2d\u70b9mid\uff0c\u65b9\u6cd5\u662f\u4f7f\u7528\u53cc\u6307\u9488\uff0c\u4f7f\u6307\u9488\u4f4d\u79fbfast:slow=2\uff0cfast\u5230\u672b\u5c3e\uff0cslow\u5373\u4e3a\u4e2d\u70b9mid<br \/>\nb\uff09\u8282\u70b9\u4e2a\u6570\u4e3a0\u62161\u65f6\u81ea\u6210\u5e8f\uff0c\u7a7a\u76f4\u63a5\u8fd4\u56de\uff0c\u5355\u8282\u70b9\u8981\u65ad\u6389\u540e\u7eed\uff0c\u5426\u5219\u5c06\u8fd4\u56de\u8be5\u8282\u70b9\u4e3a\u5934\u7684\u94fe\u8868<br \/>\n\u2460\u5f52\u5e76\u4e24\u6761\u6709\u5e8f\u94fe\u8868\uff1a\u8d85\u8fc7\u5355\u8282\u70b9\u7684\u5b50\u94fe\u8868\u4f7f\u7528\u53cc\u6307\u9488\u4f9d\u6b21\u6bd4\u8f83\u540e\u79fb\u65b9\u6cd5\u7b5b\u9009\u51fa\u540e\u4e00\u8282\u70b9\uff0c\u6302\u5230\u8bb0\u5f55\u7684\u5c3e\u8282\u70b9\u4e0a\u53bb<br \/>\n\u6b64\u89e3\u6cd5\u6682\u672a\u8ba1\u6570\u6709\u6548\u8282\u70b9\uff08\u672a\u6392\u9664\u4e0d\u5728\u94fe\u8868\u4e2d\u7684\u8282\u70b9\uff09<\/p>\n<pre><code class=\"language-cpp \">#include &lt;iostream&gt;\nusing namespace std;\n\nconst int maxn = 100001;\n\nstruct Node {\n    int addr;\n    int key;\n    int next;\n    Node(int addr, int key, int next) : addr(addr), key(key), next(next) {}\n} * node[maxn];\n\nNode *merge(Node *l, Node *r) {\n    Node *vHead = new Node(-1, -1, -1);\n    Node *cur = vHead;\n    while (l &amp;&amp; r) {\n        if (l-&gt;key &lt;= r-&gt;key) {\n            cur-&gt;next = l-&gt;addr;\n            l = node[l-&gt;next];\n        }\n        else {\n            cur-&gt;next = r-&gt;addr;\n            r = node[r-&gt;next];\n        }\n        cur = node[cur-&gt;next];\n    }\n    cur-&gt;next = l ? l-&gt;addr : r ? r-&gt;addr : -1;\n    return node[vHead-&gt;next];\n}\n\nNode *part(Node *lo, Node *hi) {\n    if (lo == nullptr)\n        return lo;\n    if (node[lo-&gt;next] == hi) {\n        lo-&gt;next = -1;\n        return lo;\n    }\n    Node *fast = lo, *slow = lo;\n    while (fast != hi) {\n        slow = node[slow-&gt;next];\n        fast = node[fast-&gt;next];\n        if (fast != hi)\n            fast = node[fast-&gt;next];\n    }\n    return merge(part(lo, slow), part(slow, hi));\n}\n\nint main() {\n    int H, N;\n    scanf(\"%d%d\", &amp;H, &amp;N);\n\n    int a, k, n;\n    while (~scanf(\"%d%d%d\", &amp;a, &amp;k, &amp;n))\n        node[a] = new Node(a, k, n);\n\n    Node *h = part(node[H], nullptr);\n\n    printf(\"%d %05d\\n\", cnt, h-&gt;addr);\n    for (; node[h-&gt;next]; h = node[h-&gt;next])\n        printf(\"%05d %d %05d\\n\", h-&gt;addr, h-&gt;key, h-&gt;next);\n    printf(\"%05d %d -1\\n\", h-&gt;addr, h-&gt;key);\n\n    return 0;\n}\n<\/code><\/pre>\n<h3>A1097 \u53bb\u91cd\u94fe\u8868<\/h3>\n<p>\u4f7f\u7528 <strong>\u5c3e\u63d2\u6cd5<\/strong>\uff0c\u7528\u4e24\u6761\u9759\u6001\u94fe\u8868\u5206\u522b\u4fdd\u5b58\u53bb\u91cd\u94fe\u8868\u548c\u91cd\u590d\u94fe\u8868<br \/>\n\u2460\u4e24\u6761\u94fe\u8868\uff1a\u5206\u522b\u8bb0\u5f55\u53bb\u91cd\u3001\u91cd\u590d\u94fe\u8868\u5934H\u3001R\u548c\u5c3etailH\u3001tailR<br \/>\n\u2461\u68c0\u91cd\u52a0\u5c3e\uff1a\u6bcf\u6b21\u68c0\u6d4bset\u4e2d\u662f\u5426\u5df2\u6709\u5f53\u524d\u5143\u7d20\uff0c\u7b5b\u51fa\u5f53\u524d\u8282\u70b9cur\u4f5c\u4e3a\u65b0\u7684\u5c3e\u90e8tail=cur\u5e76\u4fee\u6539tail.next=-1\uff0c\u4fee\u6539tail.next\u8981\u63d0\u524d\u4fdd\u5b58 <strong>\u9632\u6b62\u540e\u7ee7\u6307\u9488\u4e22\u5931<\/strong><br \/>\n\u2462\u8fb9\u754c\u8f93\u5165\uff1a\u7a7a\u94fe\u8868\u3001\u4e0d\u91cd\u94fe\u8868\u3001\u5168\u91cd\u94fe\u8868\u3001\u5355\u5143\u7d20\u94fe\u8868<\/p>\n<pre><code class=\"language-cpp \">#include &lt;iostream&gt;\n#include &lt;cmath&gt;\n#include &lt;unordered_set&gt;\nusing namespace std;\n\nconst int maxn = 100001;\n\nstruct Node {\n    int addr;\n    int key;\n    int next;\n    Node(int addr, int key, int next) : addr(addr), key(key), next(next) {}\n} *node[maxn];\n\nint main() {\n    int H, N, R = -1;\n    scanf(\"%d%d\", &amp;H, &amp;N);\n\n    int a, k, n;\n    while (~scanf(\"%d%d%d\", &amp;a, &amp;k, &amp;n))\n        node[a] = new Node(a, k, n);\n\n    unordered_set&lt;int&gt; st;\n    Node *cur = node[H], *tailH = nullptr, *tailR = nullptr;\n    while (cur) {\n        k = abs(cur-&gt;key);\n        if (!st.count(k)) {\n            st.insert(k);\n            if (tailH) tailH-&gt;next = cur-&gt;addr;\n            tailH = cur;\n            cur = node[cur-&gt;next];\n            tailH-&gt;next = -1;\n        }\n        else {\n            if (tailR == nullptr) R = cur-&gt;addr;\n            else tailR-&gt;next = cur-&gt;addr;\n            tailR = cur;\n            cur = node[cur-&gt;next];\n            tailR-&gt;next = -1;\n        }\n    }\n\n    Node *h;\n    for (h = node[H]; node[h-&gt;next]; h = node[h-&gt;next])\n        printf(\"%05d %d %05d\\n\", h-&gt;addr, h-&gt;key, h-&gt;next);\n    printf(\"%05d %d -1\\n\", h-&gt;addr, h-&gt;key, h-&gt;next);\n\n    if (R != -1) {\n        for (h = node[R]; node[h-&gt;next]; h = node[h-&gt;next])\n            printf(\"%05d %d %05d\\n\", h-&gt;addr, h-&gt;key, h-&gt;next);\n        printf(\"%05d %d -1\\n\", h-&gt;addr, h-&gt;key);\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6808 A1051 \u51fa\u6808\u5e8f\u5217 \u2460\u6309\u5e8f\u4e00\u76f4\u8fdb\u6808\uff0c\u6dfb\u52a0\u5143\u7d20\u5e94\u67e5\u5bb9\u91cf\u9632\u6b62\u8d85\u9650 \u2461\u6808\u9876\u5143\u7d20\u548c\u5e8f\u5217\u5339\u914d\u4e0a\uff0c\u5c31\u53ef\u4ee5\u7ee7\u7eed\u4e0b\u6b21\u5339\u914d &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/139.196.114.170\/?p=1914\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cPAT \u2013 ADVANCED \u2013 25POINTS (UPDATING)\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1914"}],"collection":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1914"}],"version-history":[{"count":28,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1914\/revisions"}],"predecessor-version":[{"id":1965,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1914\/revisions\/1965"}],"wp:attachment":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1914"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}