Yanshuo Li
    • 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
    • Make a copy
    • 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 Make a copy 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
    # HW-4 ### 1 **演算法 :** Step 0: 把所有點依照 X 軸由小到大排序,若 X 軸相同就依照 Y 軸由大到小。 Step 1: 若 n = 1,回傳 S 中唯一一個點的 rank = 0 並結束 Step 2: 找出所有點的 X 軸中位數,畫出垂直於 X 軸的直線 L,將 S 中的點分為兩個集合 $S_L 和 S_R$ Step 3: 遞迴使用 2D ranking 演算法分別求出 $S_L 和 S_R$ 中所有點的 rank Step 4: 掃描所有在 S = $S_L \cup S_R$ 中的點,設定 cnt = 0。若掃到 $S_L$ 中的點則 cnt 加一;反之,若掃到 $S_R$ 中的點,令為 P,則將 P 點的 rank 加上目前的 cnt ( 在 $S_L$ 中 y 座標比 P 小的點個數)。最後回傳所有點的 rank。 **Time : O(nlgn)** Step 0 : O(nlgn) Step 1 : O(1) Step 2 : O(1) Step 3 : 2$T(\frac{n}{2})$ Step 4 : O(n) => Step 1 ~ 4 總共 O(nlgn),再加上前處理 O(nlgn) => 2D Ranking Time complexity 為 O(nlgn) ![image](https://hackmd.io/_uploads/r1ehuKRLT.png) ![image](https://hackmd.io/_uploads/B1ypdFC86.png) ![image](https://hackmd.io/_uploads/BJ_TutCLT.png) ### 2 ![image](https://hackmd.io/_uploads/S1OiRlgvp.png) ![image](https://hackmd.io/_uploads/rkD3CxePp.png) (a) (b) 若好晶片有超過 $\frac{n}{2}$ 個,則可利用以下演算法保證篩出一個好晶片,接著再利用這個好晶片去比對每個晶片就可以知道每個晶片是好的還是壞的。 1. 將所有晶片兩兩配對,若 n 為奇數,多出來的晶片這輪先不處理。 2. 若結果出現至少一壞,捨棄這兩個晶片,因為我們只能知道其中一個是壞掉,無法判斷哪個是好的。 3. 若結果出現兩好,捨棄其中一張,這樣還是可以確保好晶片數量超過 $\frac{n}{2}$。 4. 重複以上動作直到剩下 1 或 2 張晶片,則剩下的晶片一定是好的。 Step 3 證明 : 為何捨棄其中一個可以確保好晶片數量超過 $\frac{n}{2}$? 由結果可知,這兩個晶片可能都是好的或都是壞的。 Case 1: 兩個晶片都是好的 (a )如果 n 為偶數,在最差的清況下,好晶片有 $\frac{n}{2} + 1 個$,壞晶片有$\frac{n}{2} - 1 個$,好晶片多壞晶片兩個,所以丟掉一個好晶片還是滿足好晶片數量大於一半。 (b) 如果 n = 2k + 1 為奇數,在最差的清況下,好晶片有 k + 1 個,壞晶片有 k 個。如果在 Step 1 被留下來的是**好晶片**,則剩下的配對全部都是一好一壞,則這些晶片都會被 Step 2 丟棄掉,最後剩下一個晶片一定是好的,故成立。如果在 Step 1 被留下來的是**壞晶片**,則留下來兩兩配對的好晶片有 k + 1 個,壞晶片有 k - 1 個,好晶片多壞晶片兩個,所以丟掉一個好晶片還是滿足好晶片數量大於一半。 Case 2: 兩個晶片都是壞的 因為題目已知超過 $\frac{n}{2}$ 為好晶片,所以丟掉一個壞晶片還是符合好晶片數量大於一半。 (c\) ### 3 ### 4 [Edit Distance 筆記](https://hackmd.io/@_EUdnTNeRl-LUa2kz_WvLw/S1mZ_XYua) ### 8 [Weighted Interval Scheduling](https://hackmd.io/@_EUdnTNeRl-LUa2kz_WvLw/SJKCLFYwp) ### 9 解法二 : [Erdős–Gallai Theorem](https://hackmd.io/@_EUdnTNeRl-LUa2kz_WvLw/B1_y5F_Pp) ![image](https://hackmd.io/_uploads/r10NG0rLa.png) ![image](https://hackmd.io/_uploads/rJ0Bz0BUp.png) ### 10 ![image](https://hackmd.io/_uploads/r1Pg1e9Sa.png) ### 11 想法 : 把所有數字進行 XOR 運算,結果就是缺少的數字 ![image](https://hackmd.io/_uploads/r1mtC4rDa.png) ``` int findMissing_1(int bit, vector<int> indices, vector<vector<int>> array){ if(bit == 0){ int digit = array[indices[0]][bit] == 0 ? 1 : 0; return digit; } vector<int> A0, A1; int n0 = 0, n1 = 0; for(int i:indices){ if(array[i][bit] == 0){ n0++; A0.push_back(i); } else{ n1++; A1.push_back(i); } } if(n0 < n1){ return findMissing_1(bit-1, A0, array) + pow(2, bit) * 0; } else{ return findMissing_1(bit-1, A1, array) + pow(2, bit) * 1; } } vector<int> toBinary(int n) { vector<int> r; while(n!=0) { int tmp = (n%2==0 ?0:1); r.push_back(tmp); n/=2; } return r; } vector<vector<int>> complement(vector<vector<int>> array, int n){ int N = pow(2, ceil(log2(n))); for (int i=0;i<N;i++){ if(i >= n+1){ vector<int> temp = toBinary(i); array.push_back(temp); }else{ reverse(array[i].begin(), array[i].end()); } } return array; ``` ### 12 ![image](https://hackmd.io/_uploads/ryuEEtEIa.png) ![image](https://hackmd.io/_uploads/S1k8NYVUT.png) ![image](https://hackmd.io/_uploads/rkpINK4L6.png) Function `P(max_k, max_type, i, j)` ![image](https://hackmd.io/_uploads/BJEO4YNUa.png) ![image](https://hackmd.io/_uploads/BJm36cBI6.png)

    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