{"id":2682,"date":"2022-02-24T13:11:23","date_gmt":"2022-02-24T05:11:23","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2682"},"modified":"2022-02-24T13:11:23","modified_gmt":"2022-02-24T05:11:23","slug":"pythonbisect","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/02\/24\/pythonbisect\/","title":{"rendered":"python \u4e8c\u5206\u67e5\u627e \uff1abisect\u6a21\u5757"},"content":{"rendered":"\n<p>bisect\u6a21\u5757\u7684\u6e90\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"Bisection algorithms.\"\"\"\r\n\r\ndef insort_right(a, x, lo=0, hi=None):\r\n\r\n    if lo &lt; 0:\r\n        raise ValueError('lo must be non-negative')\r\n    if hi is None:\r\n        hi = len(a)\r\n    while lo &lt; hi:\r\n        mid = (lo+hi)\/\/2\r\n        if x &lt; a&#91;mid]: hi = mid\r\n        else: lo = mid+1\r\n    a.insert(lo, x)\r\n\r\ninsort = insort_right   # backward compatibility\r\n\r\ndef bisect_right(a, x, lo=0, hi=None):\r\n\r\n    if lo &lt; 0:\r\n        raise ValueError('lo must be non-negative')\r\n    if hi is None:\r\n        hi = len(a)\r\n    while lo &lt; hi:\r\n        mid = (lo+hi)\/\/2\r\n        if x &lt; a&#91;mid]: hi = mid\r\n        else: lo = mid+1\r\n    return lo\r\n\r\nbisect = bisect_right   # backward compatibility\r\n\r\ndef insort_left(a, x, lo=0, hi=None):\r\n\r\n    if lo &lt; 0:\r\n        raise ValueError('lo must be non-negative')\r\n    if hi is None:\r\n        hi = len(a)\r\n    while lo &lt; hi:\r\n        mid = (lo+hi)\/\/2\r\n        if a&#91;mid] &lt; x: lo = mid+1\r\n        else: hi = mid\r\n    a.insert(lo, x)\r\n\r\n\r\ndef bisect_left(a, x, lo=0, hi=None):\r\n\r\n    if lo &lt; 0:\r\n        raise ValueError('lo must be non-negative')\r\n    if hi is None:\r\n        hi = len(a)\r\n    while lo &lt; hi:\r\n        mid = (lo+hi)\/\/2\r\n        if a&#91;mid] &lt; x: lo = mid+1\r\n        else: hi = mid\r\n    return lo\r\n\r\n# Overwrite above definitions with a fast C implementation\r\ntry:\r\n    from _bisect import *\r\nexcept ImportError:\r\n    pass<\/code><\/pre>\n\n\n\n<h3>\u65b9\u6cd5\u4ecb\u7ecd<\/h3>\n\n\n\n<p>bisect\u6a21\u5757\u91c7\u7528\u7ecf\u5178\u7684\u4e8c\u5206\u7b97\u6cd5\u67e5\u627e\u5143\u7d20\u3002\u6a21\u5757\u63d0\u4f9b\u4e0b\u9762\u51e0\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p><strong>bisect.bisect_left(a, x, lo=0, hi=len(a))<\/strong>\uff1a\u8fd4\u56de\u5f85\u63d2\u5165\u503c\u5e94\u8be5\u653e\u7f6e\u7684\u4f4d\u7f6e\u4e0b\u6807\uff0c\u5982\u679c <strong>a<\/strong> \u4e2d\u6709\u548cx\u76f8\u540c\u7684\u5143\u7d20\uff0c\u5c31\u5728\u8fd4\u56de\u8be5\u5143\u7d20\u5de6\u8fb9\u7684\u4e0b\u6807\uff0c\u5373\u5728\u5de6\u8fb9\u63d2\u5165<\/p>\n\n\n\n<p>\u5b9a\u4f4dx\u5728\u5e8f\u5217a\u4e2d\u7684\u63d2\u5165\u70b9\uff0c\u5e76\u4fdd\u6301\u539f\u6765\u7684\u6709\u5e8f\u72b6\u6001\u4e0d\u53d8\u3002\u53c2\u6570lo\u548chi\u7528\u4e8e\u6307\u5b9a\u67e5\u627e\u533a\u95f4\u3002\u5982\u679cx\u5df2\u7ecf\u5b58\u5728\u4e8ea\u4e2d\uff0c\u90a3\u4e48\u63d2\u5165\u70b9\u5728\u5df2\u5b58\u5728\u5143\u7d20\u7684\u5de6\u8fb9\u3002\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u5217\u8868\u7684\u6574\u6570\u4e0b\u6807\u3002<\/p>\n\n\n\n<p><strong>bisect.bisect_right(a, x, lo=0, hi=len(a))<\/strong><\/p>\n\n\n\n<p>\u548c\u4e0a\u9762\u7684\u533a\u522b\u662f\u63d2\u5165\u70b9\u5728\u53f3\u8fb9\u3002\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4f9d\u7136\u662f\u4e00\u4e2a\u5217\u8868\u4e0b\u6807\u6574\u6570\u3002<\/p>\n\n\n\n<p><strong>bisect.bisect(a, x, lo=0, hi=len(a))<\/strong><\/p>\n\n\n\n<p>\u7b49\u540c\u4e8ebisect_right()\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u524d\u9762\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u90fd\u662f\u83b7\u53d6\u63d2\u5165\u4f4d\u7f6e\uff0c\u4e5f\u5c31\u662f\u5217\u8868\u7684\u67d0\u4e2a\u4e0b\u6807\u7684\uff0c\u5e76\u4e0d\u5b9e\u9645\u63d2\u5165\u3002\u4f46\u4e0b\u9762\u4e09\u4e2a\u65b9\u6cd5\u5b9e\u9645\u63d2\u5165\u5143\u7d20\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p><strong>bisect.insort_left(a, x, lo=0, hi=len(a))<\/strong><\/p>\n\n\n\n<p>\u5c06x\u63d2\u5165\u6709\u5e8f\u5e8f\u5217a\u5185\uff0c\u5e76\u4fdd\u6301\u6709\u5e8f\u72b6\u6001\u3002\u76f8\u5f53\u4e8e<code>a.insert(bisect.bisect_left(a, x, lo, hi), x)<\/code>\u3002\u78b0\u5230\u76f8\u540c\u7684\u5143\u7d20\u5219\u63d2\u5165\u5230\u5143\u7d20\u7684\u5de6\u8fb9\u3002\u8fd9\u4e2a\u64cd\u4f5c\u901a\u5e38\u662f O(log n) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002<\/p>\n\n\n\n<p><strong>bisect.insort_right(a, x, lo=0, hi=len(a))<\/strong><\/p>\n\n\n\n<p>\u540c\u4e0a\uff0c\u53ea\u4e0d\u8fc7\u78b0\u5230\u76f8\u540c\u7684\u5143\u7d20\u5219\u63d2\u5165\u5230\u5143\u7d20\u7684\u53f3\u8fb9\u3002<\/p>\n\n\n\n<p><strong>bisect.insort(a, x, lo=0, hi=len(a))<\/strong><\/p>\n\n\n\n<p>\u7b49\u540c\u4e8einsort_right()\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>bisect\u6a21\u5757\u7684\u6e90\u7801\uff1a \u65b9\u6cd5\u4ecb\u7ecd bisect\u6a21\u5757\u91c7\u7528\u7ecf\u5178\u7684\u4e8c\u5206\u7b97\u6cd5\u67e5\u627e\u5143\u7d20\u3002\u6a21\u5757\u63d0\u4f9b\u4e0b\u9762\u51e0\u4e2a\u65b9\u6cd5\uff1a bis &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/02\/24\/pythonbisect\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">python \u4e8c\u5206\u67e5\u627e \uff1abisect\u6a21\u5757<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2682"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2682"}],"version-history":[{"count":2,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2682\/revisions"}],"predecessor-version":[{"id":2684,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2682\/revisions\/2684"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2682"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}