110林燈進階預備班
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 教材 --- # 動態規劃 --- ## 基本觀念 1. 定義子問題。 2. 找出問題與子問題之間的遞迴的方式進行計算。 3. 找出子問題的計算順序來避免以遞迴的方式進行計算。 --- ## 範例(費式數列) ---- ### 遞迴 如果依照遞迴的想法,可快速寫出下列程式: ```cpp= #include<iostream> using namespace std; long long fib(int n){ if(n<3)return 1; return fib(n-1)+fib(n-2); } int main(){ int n; cin>>n; cout<<fib(n)<<'\n'; return 0; } ``` ---- 但是我們真的需要這麼多的運算嗎? ```graphviz digraph FIB{ nodesep=0.1 "fib(5)"->{"fib(3) " "fib(4)"} "fib(3) "->{"fib(1)" "fib(2)"} "fib(4)"->{"fib(2) " " fib(3) "} " fib(3) "->{" fib(1) " " fib(2) "} } ``` ---- ### Top-down memoization ```cpp= #include<iostream> using namespace std; long long dp[100005]; long long fib(int n){ if(dp[n]!=-1)return dp[n]; return dp[n]=fib(n-1)+fib(n-2); } int main(){ for(int i=0;i<100005;++i)dp[i]=-1; dp[1]=1,dp[2]=1; int n; cin>>n; cout<<fib(n)<<'\n'; return 0; } ``` ---- ### Bottom-up ```cpp= #include<iostream> using namespace std; long long fib[100005]; int main(){ fib[1]=1,fib[2]=1; int n; cin>>n; for(int i=3;i<=n;++i) fib[i]=fib[i-1]+fib[i-2]; cout<<fib[n]<<'\n'; return 0; } ``` --- ## 習題1 ---- ### 題目連結 [Frog1](https://atcoder.jp/contests/dp/tasks/dp_a) * 給定N個石頭,第i個石頭高度為$h_i$ * 第i個石頭只能跳到第i+1或第i+2個石頭 * 彈跳的費用為$|h_i-h_j|$,j為目的地 * 求到第n個石頭的花費最小值? * $2\le N \le 10^5$ ---- ### 思路 * $dp[i]$為走到第i格時最小的花費。 * $dp[i]=min(dp[i-1]+距離,dp[i-2]+距離)$ --- ## 習題2 ---- ### 題目連結 [Frog2](https://atcoder.jp/contests/dp/tasks/dp_b) * 給定N個石頭,第i個石頭高度為$h_i$ * 給定最遠距離K * 第i個石頭能跳到第i+1,i+2....,i+K個石頭 * 彈跳有費用為$|h_i,h_j|$,j為目的地 * 求到第n個石頭的花費最小值? * $2\le N \le 10^5$,$1\le K \le100$ ---- ### 思路 * $dp[i]$為走到第i格時最小的花費。 * $dp[i]$能由dp[i-1]....dp[i-k]組成,要考慮前面所有狀態值找到最小值 * $dp[i]=min(dp[j]+abs(fg[i]-fg[j]),dp[i])$ * $i-k\le j\le i-1$ --- ## 習題3 ---- ### 題目連結 [最小監控鄰居的成本] * 給定N個區塊,第i區塊監測花費為$c_i$ * 監控第i塊區間時,i-1和i+1塊區間也都會被監控 * 求所有區塊都能監視到的最小成本 * $N \le 10^5$ ---- ### 思路 * 這題看似和前面類似但dp[i-1]的值可能來自沒有挑選到i-1的值 * 定義$dp[i]$為必選i * $if$ $i>2$ * $dp[i] = c[i] + min(dp[i-1], dp[i-2], dp[i-3])$ --- ## 習題4 ---- ### 題目連結 [LCS] * 給定兩字串s1,s2 * 求其最長相同子字串 ---- ### 思路 * 我們定義dp[i][j]為s1的前i個字元和s2的前j個字元中,最長的lcs。 * $if$ $s1[i]==s2[j]$ * $dp[i][j]=dp[i-1][j-1]+1$ * $if$ $s1[i]!=s2[j]$ * $dp[i][j]=max(dp[i-1][j],dp[i][j-1])$ * 至於兩個都不選的情況無需特別判斷,因為其狀態是包含在只刪一個的狀態中。 ---- ### 挑戰題 [洛谷1439](https://www.luogu.com.cn/problem/P1439) --- ## 習題5 ---- ### 題目連結 [LIS] * 給定N個數,求數列中最常遞增子數列長度 ---- ### 思路 * dp[i]為第i點結束最常LIS長度 * $j<i \; and \; s[j]<s[i]$ * $dp[i]=max(dp[j]+1)$ ---- ### 優化 * 原複雜度$O(n^2)$ -> $O(nlogn)$ * 數字較小結尾,lis數列越長 * ex: 1 3 4 -> 1 2 4 * lis數列具單調性,所以二分搜!!! ---- ### 挑戰題 [洛谷P1091](https://www.luogu.com.cn/problem/P1091) --- ## 習題6 ---- ### 題目連結 [背包問題](https://atcoder.jp/contests/dp/tasks/dp_d) * 給定N個物品,第i個物品重量為$W_i$,價值$V_i$ * 背包容量為W * 求物品重量和不超過W,價值總和之最大值? * $1\le n \le 100$ , $1\le W \le 10^5$ ---- ### 思路 * dp[i][j]=只考慮前i個物品,背包容量為j的最大價值總和 * 考慮第i樣物品 * 選: $dp[i][j]=dp[i-1][j-W_i]+V_i$ * 不選: $dp[i][j]=dp[i-1][j]$ ---- ### 挑戰題 [knapsack2](https://atcoder.jp/contests/dp/tasks/dp_e) --- ## 習題7 ---- ### 題目連結 [背包問題](https://atcoder.jp/contests/dp/tasks/dp_d) * 給定N個物品,第i個物品重量為$W_i$,價值$V_i$ * 背包容量為W * 每樣物品無限制選取數量 * 求物品重量和不超過W,價值總和之最大值? * $1\le n \le 100$ , $1\le W \le 10^5$ ---- ### 思路 * dp[i][j]=只考慮前i個物品,背包容量為j的最大價值總和 * 考慮第i樣物品 * 選: $dp[i][j]=dp[i-1][j-W_i]+V_i$ * 不選: $dp[i][j]=dp[i-1][j]$ ---- ### 挑戰題 [knapsack2](https://atcoder.jp/contests/dp/tasks/dp_e) ---

    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