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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # lIS 全名longest increasing subsequence,最長遞增子序列 ### 1.Robinson-Schensted-Knuth Algorithm 概念:以greedy為基礎,二元搜尋加速 1. 初始作業 ```cpp= //測資:a[] vector<int> stk{a[0]},len(a.size()); //先放入一個數避免取stk.back()時segmentation fault len[0]=1; ``` 2. 遍歷一次a,如果該元素大於stk.back()的話就放入尾端 否則就用lower_bound找到第一個大於或等於該元素的元素,並把他替換掉(該元素成為lis的淺能比lower_bound回傳元素的淺能還大) ```cpp= for(int i=1;i<a.size();i++){ if(a[i]>stk.back()){ stk.push_back(a[i]); len[i]=stk.size(); }else{ auto it=lower_bound(stk.begin(),stk.end(),a[i]); *it=a[i]; len[i]=it-stk.begin()+1; } } ``` 3. 從尾開始到回去找,因為這樣才能找到成為lis淺能最大的,先設一個變數=LIS,接著從len的最後倒回去找如果len[i]==now,就代表在做LIS時有放進一個元素a[i],該元素屬於LIS,所以要把他加到ans ```cpp= vector<int> ans; int now=stk.size(); for(int i=a.size()-1;i>=0;i--){ if(len[i]==now){ ans.push_back(a[i]); now--; } } } ``` 4. 最後顛倒過來就是正的LIS了 ```cpp= reverse(ans.begin(),ans.end()); for(auto i:ans){ cout << i << " "; } ``` #非嚴格遞增子序列要使用upper_bound# 說明:因為同為value的值可以出現很多次,所以要找大於value中最小的值來替換 ```cpp= for(int i=1;i<testlen;i++){ if(test[i]>dparr.back()){ dparr.push_back(test[i]); lislen++; dp[i]=lislen; }else{ auto it=upper_bound(dparr.begin(),dparr.end(),test[i]); *it=test[i]; dp[i]=it-dparr.begin()+1; } } ``` 另一種寫法 ```cpp= for(int i=0;i<testlen;i++){ int value=test[i]; int len=upper_bound(dparr.begin(),dparr.end(),value)-dparr.begin(); if(len==aparr.size()){ //找不到 dparr.push_back(value); }else{ test[len]=value; } } ``` <a href="https://zerojudge.tw/ShowProblem?problemid=d242">ZJ d242: 00481 - What Goes Up</a> ```cpp= #include <bits/stdc++.h> using namespace std; int main(){ int x; vector<int> a; while(cin >> x) a.push_back(x); vector<int> stk{a[0]},len(a.size()); len[0]=1; for(int i=1;i<a.size();i++){ if(a[i]>stk.back()){ stk.push_back(a[i]); len[i]=stk.size(); }else{ auto it=lower_bound(stk.begin(),stk.end(),a[i]); *it=a[i]; len[i]=it-stk.begin()+1; } } cout << stk.size() << '\n' << "-" << '\n'; vector<int> ans; int now=stk.size(); for(int i=a.size()-1;i>=0;i--){ if(len[i]==now){ ans.push_back(a[i]); now--; } } for(int i=ans.size()-1;i>=0;i--){ cout << ans[i] << '\n'; } return 0; } ``` ### 2.Dynamic Programming 看當前元素有沒有比前面的元素小,有的話,代表LIS可以變長,就把LIS更新到當前的陣列裡 DP狀態轉移: ``` if(dp[j]<dp[i]) dp[i]=max(dp[i],dp[j]+1) for j in range(0,i-1); ``` Code: ```cpp= int getlcs(vector<int> &nums){ int n=nums.size(); vector<int> lis(n,1); //每個LIS初始都是自己一個 int ans=0; for(int i=0;i<n;i++){ for(int j=0;j<i;j++){ if(nums[i]>nums[j]){ //如果當前元素nums[i]有大於前面的任何一個元素nums[j] lis[i]=max(lis[i],lis[j]+1); //那就更新到lis[i]=前面元素nums[j]的lis+1 } } ans=max(ans,lis[i]); //更新最大值 } return ans; } ``` 題目:<a href="https://zerojudge.tw/ShowProblem?problemid=c115">c115: 00437 - The Tower of Babylon</a> Solution 1:看前面有沒有長寬都比我小的,如果有代表可以疊上去,就把答案更新到lis[i]裡面 ```cpp= vector<int> lis(n*3,0); int ans=0; for(int i=0;i<n*3;i++){ lis[i]=arr[i].h; //初始化,每個lis都是自己 for(int j=0;j<i;j++){ if(arr[i].x<arr[j].x && arr[i].y<arr[j].y){ lis[i]=max(lis[i],lis[j]+arr[i].h); } } ans=max(ans,lis[i]); } ``` suoution 2:看我後面有沒有長寬都比我小的,如果有代表後面那個方塊可以疊加到當前方塊身上,就把當前的高度更新到lis[j] ```cpp= vector<int> lis(n*3,0); int ans=0; for(int i=0;i<n*3;i++){ lis[i]+=arr[i].h; //把前面最高的高度加上自己的高度就是當前最高的高度 for(int j=i+1;j<n*3;j++){ if(arr[i].x>arr[j].x && arr[i].y>arr[j].y){ lis[j]=max(lis[i],lis[j]); } } ans=max(ans,lis[i]); } ``` Code : ```cpp= #include <bits/stdc++.h> using namespace std; struct node{ int x,y,h; }; bool cmp(node a,node b){ if(a.x==b.x) return a.y>b.y; return a.x>b.x; } int main(){ int n,a,b,c,casen=1; while(cin >> n && n){ vector<node> arr(n*3); for(int i=0;i<n;i++){ cin >> a >> b >> c; arr.push_back({max(a,b),min(a,b),c}); arr.push_back({max(a,c),min(a,c),b}); arr.push_back({max(b,c),min(b,c),a}); } sort(arr.begin(),arr.end(),cmp); vector<int> lis(n*3,0); int ans=0; for(int i=0;i<n*3;i++){ lis[i]+=arr[i].h; for(int j=i+1;j<n*3;j++){ if(arr[i].x>arr[j].x && arr[i].y>arr[j].y){ lis[j]=max(lis[i],lis[j]); } } ans=max(ans,lis[i]); } cout << "Case " << casen++ <<": maximum height = " << ans << '\n'; } return 0; } ```

    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