蔣立元
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    時間複雜度與二分搜 === sprout 2021 --- 請問這個程式會執行多久? ```cpp= for (i = 0; i < 10; i++) { sum += a[i]; } ``` --- ```cpp= for (i = 0; i < 10; i++) { sum += a[i]; } ``` | 指令 | 次數 | | ----------- | ---- | | i = 0 | 1 | | sum += a[i] | 10 | | i++ | 10 | | i < 10 | 11 | --- | 指令 | 次數 | 花費時間 | | ----------- | ---- |----| | i = 0 | 1 | c1 | | sum += a[i] | 10 | c2 | | i++ | 10 | c3 | | i < 10 | 11 | c4| 總共:$c1 + 10 \times c2 + 10 \times c3 + 11 \times c4$ --- 每個指令的執行速度不盡相同 且需要考量的點很多 (不同架構、快取...) 因此在評估程式的效能時 通常可以無視這之間的差距 (但是 +, - 會比 /, % 快速) --- ![](https://i.stack.imgur.com/rbPmq.png) --- | 指令 | 次數 | 花費時間 | | ----------- | ---- |----| | i = 0 | 1 | c1 | | sum += a[i] | 10 | c2 | | i++ | 10 | c3 | | i < 10 | 11 | c4| 總共執行:$1 + 10 + 10 + 11$ 次基本運算 --- ```cpp= for (i = 0; i < n; i++) { sum = sum + a[i]; } ``` --- | 指令 | 次數 | | ----------- | ---- | | i = 0 | 1 | | sum += a[i] | n | | i++ | n | | i < 10 | n + 1 | 總共執行:1 + n + n + (n + 1) 次基本運算 也就是 $3n + 2$ --- 請問這個程式會執行多久? ```cpp= for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { sum += a[i][j]; } } ``` --- | 指令 | 次數 | | ----------- | ---- | | i = 0 | n | | i < n | n + 1 | | i++ | n | | j = 0 | n | | j < n | n * (n + 1) | | j++ | n * n | | sum += a[i][j] | n * n | --- 總共執行:$n + (n + 1) + n + n + n \times (n + 1) + n \times n + n \times n$ 也就是 $3n^2 + 5n + 1$ --- 比較一下這兩個多項式 $3n + 2$ vs $3n^2 + 5n + 1$ 哪一個看起來比較大? n 我要帶多少進去? --- $100n + 1000$ vs $2^n + n$ n = 5? n = 12? --- 我們需要比較兩個多項式的漸進行為 因此衍生出了 Big-O 符號 --- ## Big-O $f(n) = O(g(n))$ 代表 $g(n)$ 是 $f(n)$ 的漸進上界 講白話一點就是 $f(n) = O(g(n))$ 代表當n夠大時 我們能找到一個常數 $c$ 使得 $c \times g(n)$ 大於 $f(n)$ --- 對於一個式子 我們取用次方數最高、n 很大時影響最大的項 並且將常數移除 當作時間複雜度 --- $3n + 2 => O(n)$ $3n^2 + 5n + 1 => O(n^2)$ $2^n + 5 => O(2^n)$ --- ```cpp for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { sum += a[i][j]; } } ``` 時間複雜度:$O(n^2)$ --- How about this? ```cpp for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { sum += a[i][j]; } } ``` --- ```cpp for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { sum += a[i][j]; } } ``` $n + (n - 1) + (n - 2) + .... + 1 = \frac{(1 + n) \times n}{2} = \frac{n^2+n}{2}$ 時間複雜度:$O(n^2)$ --- 較差的時間複雜度在 $n$ 小時 可能有較好的執行時間 ![](https://social.msdn.microsoft.com/Forums/getfile/329411) --- 電腦一秒約可以做 $10^8$ 次基本運算 因此可以將程式的時間複雜度算出來 帶入數字判斷量級是否小於 $10^8$ --- 有經驗的人可以透過測資大小直接推算 題目要求的程式的時間複雜度大概是什麼 | N | 複雜度 | | --- | ------ | | $10$ | $O(n!)$ | | $20$ | $O(2^n\times n)$ | | $2000$ | $O(n^2)$ | | $10^5$ | $O(nlogn)$ | | $10^7$ | $O(n)$ | | $10^{18}$ | $O(1)$ | --- ## 二分搜 --- 猜數字 1 ~ 100 中猜我想的數字 我會告訴你我想的比你猜多還大或小 請問我最小猜幾次能猜到 --- 猜數字 1 ~ 100 中猜我想的數字 我會告訴你我想的比你猜多還大或小 請問我最小猜幾次能保證猜到? --- 100, 50, 25, 13, 7, 4, 2, 1 --- 給你遞增數列 $[0, 1, 2, 3, 6, 8, 9, 10]$ 求 $2$ 在哪 從左到右掃過陣列是 $O(N)$ 沒有用到遞增數列的性質! ---- ### 觀察 $[0, 1, 2, 3, 6, 8, 9, 10]$ * 數字是升序的 ---- $[?, ?, ?, [3], ?, ?, ?, ?]$ * 先問中間的數字 * 2 一定在 3 的左邊! ---- $[?, ?, ?, 3, X, X, X, X]$ ---- $[?, [1], ?, 3, X, X, X, X]$ * 再問中間的數字 * 2 一定在 1 的右邊! ---- $[X, 1, ?, 3, X, X, X, X]$ ---- $[X, 1, [2], 3, X, X, X, X]$ * 再問中間的數字... ---- ## 遞迴 ```cpp int find_index(int *a, int L, int R, int Q) { if (R < L) return -1; // 解空間是空集合 -> 無解 int M = (L + R) / 2; if (a[M] == Q) return M; // 找到了 if (a[M] < Q) return find_index(a, M+1, R, Q); // 在右邊 return find_index(a, L, M-1, Q); // 在左邊 } ``` --- ## 迴圈 ```cpp int ans = -1; while (l <= r) { int mid = (l + r) / 2; if (arr[mid] == please_find) { ans = mid; break; } if (arr[mid] < please_find) { l = mid + 1; } else { assert(please_find < arr[mid]); r = mid - 1; } } ``` --- 複雜度分析 $N$ 為序列長度 因為每一次可能的解答的集合都會減少一半 $N$ 一直除以$2$要除幾次才能等於 $1$ 因此複雜度為 $O(logN)$ --- 我們也可以利用二分搜的概念去逼近答案 Ex: 找一個數字開根號的值為多少 --- ```cpp double sqrt(double n) { double l = -INF, r = INF; while(r - l < eps) { double mid = (l + r) / 2; if (mid * mid < n) l = mid; else r = mid; } return l; } ``` --- 練習時間 NEOJ 148 --- ## WA ```cpp int l = 1, r = 100; while(l != r){ int mid = (l+r)/2; if (less(mid)) r = mid-1; else l = mid; } guess(l); ``` --- ## AC ```cpp int l = 1, r = 101; while(r - l != 1){ int mid = (l+r)/2; if (less(mid)) r = mid; else l = mid; } guess(l); ``` --- ## Bonus problem NEOJ 364 --- 進入二檔 ---

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully