{"id":1346,"date":"2024-02-05T03:16:31","date_gmt":"2024-02-04T19:16:31","guid":{"rendered":"http:\/\/www.forillusion.com\/?p=1346"},"modified":"2025-02-14T11:39:10","modified_gmt":"2025-02-14T03:39:10","slug":"monotone-queue","status":"publish","type":"post","link":"https:\/\/www.forillusion.com\/index.php\/monotone-queue\/","title":{"rendered":"\u5355\u8c03\u6808"},"content":{"rendered":"\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u7248<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u4ee5\u4e0b\u6240\u6709\u5c06b&#91;i]=q.top().second\u6539\u4e3ab&#91;i]=q.top().first \u5373\u53ef\u5f97\u5230\u5143\u7d20\u7684\u503c\u800c\u4e0d\u662f\u5750\u6807\u3002\n\/\/n\u4e3a\u6570\u7ec4\u89c4\u6a21\uff0c\u9ed8\u8ba4\u4e0b\u6807\u4ece1\u5f00\u59cb\n\/\/\u6570\u7ec4a\u4e3a\u539f\u6570\u7ec4\n\/\/\u6570\u7ec4b\u4e3a\u67e5\u627e\u5230\u7684\u5750\u6807\uff08\u5143\u7d20\u503c\uff09\nvoid findRMin(int n,int a&#91;],int b&#91;])   \/\/\u5bfb\u627e\u53f3\u8fb9\u7b2c\u4e00\u4e2a\u6bd4\u81ea\u5df1\u5c0f\u7684\u5143\u7d20\u7684\u5750\u6807\n{\n    stack&lt;pair&lt;int,int&gt;&gt; q;\n    for (int i=n;i&gt;=1;i--)\n    {\n        while (!q.empty()&amp;&amp;a&#91;i]&lt;=q.top().first)\n            q.pop();\n        if (q.empty()) b&#91;i]=0;\n        else b&#91;i]=q.top().second;\n        q.push(make_pair(a&#91;i],i));\n    }\n}\n\nvoid findLMin(int n,int a&#91;],int b&#91;])   \/\/\u5bfb\u627e\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u6bd4\u81ea\u5df1\u5c0f\u7684\u5143\u7d20\u7684\u5750\u6807\n{\n    stack&lt;pair&lt;int,int&gt;&gt; q;\n    for (int i=1;i&lt;=n;i++)\n    {\n        while (!q.empty()&amp;&amp;a&#91;i]&lt;=q.top().first)\n            q.pop();\n        if (q.empty()) b&#91;i]=0;\n        else b&#91;i]=q.top().second;\n        q.push(make_pair(a&#91;i],i));\n    }\n}\n\nvoid findRMax(int n,int a&#91;],int b&#91;])   \/\/\u5bfb\u627e\u53f3\u8fb9\u7b2c\u4e00\u4e2a\u6bd4\u81ea\u5df1\u5927\u7684\u5143\u7d20\u7684\u5750\u6807\n{\n    stack&lt;pair&lt;int,int&gt;&gt; q;\n    for (int i=n;i&gt;=1;i--)\n    {\n        while (!q.empty()&amp;&amp;a&#91;i]&gt;=q.top().first)\n            q.pop();\n        if (q.empty()) b&#91;i]=0;\n        else b&#91;i]=q.top().second;\n        q.push(make_pair(a&#91;i],i));\n    }\n}\n\nvoid findLMax(int n,int a&#91;],int b&#91;])   \/\/\u5bfb\u627e\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u6bd4\u81ea\u5df1\u5927\u7684\u5143\u7d20\u7684\u5750\u6807\n{\n    stack&lt;pair&lt;int,int&gt;&gt; q;\n    for (int i=1;i&lt;=n;i++)\n    {\n        while (!q.empty()&amp;&amp;a&#91;i]&gt;=q.top().first)\n            q.pop();\n        if (q.empty()) b&#91;i]=0;\n        else b&#91;i]=q.top().second;\n        q.push(make_pair(a&#91;i],i));\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u91ca<\/h2>\n\n\n\n<p>\u4ee5\u5bfb\u627e\u53f3\u8fb9\u7b2c\u4e00\u4e2a\u6bd4\u81ea\u5df1\u5c0f\u7684\u5143\u7d20\u7684\u5750\u6807\u4e3a\u4f8b\uff0c\u4ece\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\u4ece\u53f3\u5f80\u5de6\u904d\u5386\uff0c\u6808q\u4e2d\u6309\u7167\u4ece\u9876\u5230\u5e95\u964d\u5e8f\u5b58\u50a8\u3002<br>\u7b2c\u4e00\u4e2awhile\u5faa\u73af\u4f1a\u5f39\u51fa\u6808\u4e2d\u6240\u6709\u5927\u4e8e\u5f53\u524d\u5143\u7d20a[i]\u7684\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u6bd4\u81ea\u5df1\u8fd8\u5c0f\u7684\u5143\u7d20\uff0c\u8fd9\u4f7f\u5f97\u6808\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u6bd4a[i]\u5c0f\u3002<br>\u88ab\u5f39\u51fa\u5143\u7d20\u56e0\u4e3a\u5176\u5927\u4e8e\u5f53\u524d\u5143\u7d20a[i]\uff0c\u6240\u4ee5\u5bf9\u4e8ea[i]\u5de6\u8fb9\u7684\u5143\u7d20\u6765\u8bf4\uff0c\u88ab\u5f39\u51fa\u5143\u7d20\u4e0d\u53ef\u80fd\u4e3a\u7b2c\u4e00\u4e2a\u5c0f\u4e8e\u7684\u5143\u7d20\uff0c\u6240\u4ee5\u88ab\u5f39\u51fa\u3002<br>\u968f\u540e\u6839\u636e\u6808\u4e2d\u662f\u5426\u6709\u5143\u7d20\u6765\u5224\u65ad\u53f3\u8fb9\u662f\u5426\u6709\u5143\u7d20\u5c0f\u4e8e\u5b83\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u6ca1\u6709\uff0c\u4e0d\u4e3a\u7a7a\u5219\u6808\u9876\u7aef\u7684\u5143\u7d20\u662f\u7b2c\u4e00\u4e2a\u5c0f\u4e8e\u5b83\u7684\u5143\u7d20\u3002<br>\u968f\u540e\u5c06\u5143\u7d20a[i]\u548c\u5750\u6807i\u538b\u5165\u6808\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6a21\u7248 \u89e3\u91ca \u4ee5\u5bfb\u627e\u53f3\u8fb9\u7b2c\u4e00\u4e2a\u6bd4\u81ea\u5df1\u5c0f\u7684\u5143\u7d20\u7684\u5750\u6807\u4e3a\u4f8b\uff0c\u4ece\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\u4ece\u53f3\u5f80\u5de6\u904d\u5386\uff0c\u6808q\u4e2d\u6309\u7167\u4ece\u9876\u5230\u5e95\u964d\u5e8f\u5b58\u50a8\u3002\u7b2c\u4e00\u4e2awhile &#8230;<\/p>","protected":false},"author":1,"featured_media":1347,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,35],"tags":[43,12,34,22],"class_list":["post-1346","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3","category-35","tag-43","tag-12","tag-34","tag-22"],"_links":{"self":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/comments?post=1346"}],"version-history":[{"count":1,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1346\/revisions"}],"predecessor-version":[{"id":1734,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1346\/revisions\/1734"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media\/1347"}],"wp:attachment":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media?parent=1346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/categories?post=1346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/tags?post=1346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}