Eric
    • 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
    # MCS Maximum Consecutive Sum ## (1)無要求範圍 1. 動態規劃 轉移方程式 ```cpp= dp[i]=max(dp[i-1]+a[i],a[i]); ma=(ma,dp[i]); ``` dp[i]為以a[i]為子序列末端最大連續元素和(對於每個a[i]可以是新的開頭或延伸前面的子序列,兩者取最大為此節點最佳解),在dp的時候要不斷更新最大值 最精簡寫法: ```cpp= int dp=0,ma=INT_MIN; for(int i=1;i<=n;i++){ cin >> v; dp=max(dp+v,v); ma=max(ma,dp); } ``` 2. 累積遍歷演算法 由上述dp狀態轉移可知dp[i]是由 ```cpp= 1. dp[i-1]+v[i] 2. v[i] ``` 取最大值而來,當2比較大時 ```cpp= dp[i-1]+v[i] < v[i] ``` 同減v[i] ```cpp= dp[i-1] < 0 ``` 代表前面的和小於0的時候dp[i]就是自己會是最佳解 所以我們可以推得code= ```cpp= for(int i=1;i<=n;i++){ dp[i]=dp[i-1]+v[i]; ma=max(ma,dp[i]); //要先取max避免只有負數的時候先被歸0就取不到負數了 if(dp[i]<0) dp[i]=0; } ``` <b>結論:當累積的前綴和小於0時,就把它歸零,因為對未來的結果沒有貢獻</b> ### 注意:此結論不可用於有限制數量和環狀陣列的題目 反例: 限制數量k: [1 3 2 -3 999 -1] k=3 最好[999] 環狀陣列: [5 -3 5] 往後蓋...5 -3 5 最好[5,5] ## (2)最大連續元素和要求為K個 利用前綴合直接計算 ```cpp= for(int i=1;i<=N;i++){ cin >> v[i]; sum[i]=sum[i-1]+v[i]; } int ma=INT_MIN; for(int l=0,r=k;r<=N;l++,r++){ int cachesum=sum[r]-sum[l]; ma=max(ma,cachesum); } ``` ## (3)最大連續元素和要求最大為K個 1. 計算前綴和 2. 遍歷數列 3. 利用set建立一個單調對列(set可自動排序方便找最小值) 4. 維護單調對列(移除過期元素),對列內元素數最大為K個 5. 在單調對列裡找出最小前綴和(*set.begin()),減掉當前前綴和就是以v[i]為結尾在往前K個內的最大連續元素和 6. 更新最大值 7. 更新單調對列 ```cpp= set<int> s; s.insert(0); for(int i=1;i<=n;i++){ if(i-k>0) s.erase(prefixsum[i-k-1]); cachesum=prefixsum[i]-*s.begin(); ans=max(ans,cachesum); s.insert(prefixsum[i]); } ``` pq優化 ```cpp= priority_queue<pii,vector<pii>,greater<pii>> pq; pq.push({0,0}); for(int i=1;i<=n;i++){ while(!pq.empty() && pq.top().S<i-k) pq.pop(); ans=max(ans,pre[i]-pq.top().F); pq.push({pre[i],i}); } ``` ## (4)環形陣列(circular subarray) LeetCode:918. Maximum Sum Circular Subarray #### solution 1 環形子陣列有兩種情況 1. 最大連續元素和在頭尾內 2. 最大連續完素和有包括頭尾 第一種就利用dp方法即可求得最大值,第二種利用總和減掉區間最小值(也適用dp方法)就是過頭尾的區間最大值 時間複雜度只有O(n) ```cpp= class Solution { public: int maxSubarraySumCircular(vector<int>& A) { int maxnow=0; int minnow=0; int total=0; int maxans=INT_MIN; int minans=INT_MAX; int maxinteger=INT_MIN; for(int i=0;i<A.size();i++){ maxinteger=max(maxinteger,A[i]); total+=A[i]; //求區間最大值 maxnow+=A[i]; maxans=max(maxans,maxnow); if(maxnow<0) maxnow=0; //求區間最小值 minnow+=A[i]; minans=min(minans,minnow); if(minnow>0) minnow=0; } int ans=max(maxans,total-minans); if(maxinteger<0) return maxinteger; else return ans; } }; ``` 有個例外:當A全為負數時,區間最小元素和會等於總和,這樣相減等於0,和區間最大取max會是0,所以應該要先判斷,如果全是負數就回傳陣列裡的最大值才是答案 #### solution 2 #優先對列 把[0,n-1]複製到[n,2n-1]並且把前綴和算出來,這樣就可以用上面(3)來計算,只是把k改成n個,因為不可以重複選 ```cpp= #define pii pair<int,int> #define F first #define S second class Solution { public: int maxSubarraySumCircular(vector<int>& nums) { int n=nums.size(); vector<int> pre(2*n+1); for(int i=0;i<n*2;i++){ pre[i+1]=pre[i]+nums[i%n]; } int ans=-1e9; priority_queue<pii,vector<pii>,greater<pii>> pq; pq.push({0,0}); for(int i=1;i<=2*n;i++){ while(!pq.empty() && pq.top().S<i-n) pq.pop(); ans=max(ans,pre[i]-pq.top().F); pq.push({pre[i],i}); } return ans; } }; ``` #### solution 3 #單調對列 #### solution 4 #線段樹 #### solution 5 #sparse table

    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