Shik Chen
    • 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
    # SRM 718 Editorials [toc] ## Div II - Easy - RelativeHeights In this problem you are given the heights of ‘n’ towers. You are required to choose n-1 of the towers and rank them according to their heights (ranks range between 0 to n-2). So, there would be C(n,n-1) choices. Of these you need to print out the choices that have unique rank ordering. To be more specific, if the rank orderings, for some example sequence, are as follows: ``` {0,2,3,1,4} {4,3,2,1,0} {3,4,1,2,0} {0,2,3,1,4} {2,4,1,0,3} {3,4,1,2,0} ``` Of the above six possibilities, only 4 orderings look unique and the answer is thus 4. You can follow a brute force method for this problem. Pseudo Code: ``` For each tower Rank all the other towers according to their heights. Push this rank profile into a set Return the size of the set ``` My implementation is as follows: ```clike #include<bits/stdc++.h> #define endl '\n' using namespace std; class RelativeHeights { public:int countWays(vector <int> h) { int hsiz=h.size(); set<vector<int>> s; for(int i=0;i<hsiz;i++) { int pos=0; vector<pair<int,int>> mp; vector<int> ind(hsiz-1); for(int j=0;j<hsiz;j++) { if(i!=j) mp.push_back(make_pair(h[j],pos++)); } sort(mp.begin(),mp.end()); reverse(mp.begin(),mp.end()); for(int k=0;k<hsiz-1;k++) ind[mp[k].second]=k; s.insert(ind); } return s.size(); } }; ``` ## Div II - Medium - InterleavingParenthesisDiv2 We will use dp to solve this problem. Now consider dp[i][j] as total ways of interleaving for first ‘i’ characters of string s1 & ‘j’ characters of string s2. Now it’s pretty easy to see that the last character of this string of length i + j will either be ith character of string s1 or jth character of string s2. Therefore the transitions of dp will be dp[i][j] = dp[i - 1][j] + dp[i][j - 1] (check for i = 0 & j = 0) That’s pretty much it. Just check few cases like in the end total ‘(‘ characters should match ‘)’ characters and any intermediate dp state should have more ‘(‘ characters than ‘)’ if not that dp value will correspond to zero. Java Code : ```java public class InterleavingParenthesisDiv2 { public int mod = 1000000007; public int countWays(String s1, String s2) { int n = s1.length(), m = s2.length(); int[] p1 = new int[n+1], p2 = new int[m+1]; for (int i = 1; i <= n; i++) p1[i] = p1[i-1] + (s1.charAt(i-1) == '(' ? +1 : -1); for (int i = 1; i <= m; i++) p2[i] = p2[i-1] + (s2.charAt(i-1) == '(' ? +1 : -1); int[][] dp = new int[n+1][m+1]; dp[0][0] = 1; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (p1[i] + p2[j] < 0) { dp[i][j] = 0; continue; } if (i>0) dp[i][j] += dp[i-1][j]; if (j>0) dp[i][j] += dp[i][j-1]; dp[i][j] %= mod; } } return p1[n] + p2[m] == 0 ? dp[n][m] : 0; } } ``` ## Div II - Hard - ChainCity The solution of this problem is binary search. In each step of binary search, we will find how many transporters required if all buildings must be at most X/2 distances away from nearest transporter. Note that X is a center value of binary search range. If all buildings are at most X/2 distances away from nearest transporter, we can move from any building to another building at most X distances. Because, start building to nearest transporter is at most X/2, and destination from nearest transporter is at most X/2. So, total cost must be at most X. If less than or equal to k transporters required, X is possible answer and update the binary search range. If more than k transporters required, X is impossible answer and also update the search range. To find how many transporters required, just build a transporter from left to right if current building is more than X/2 distances away from nearest transporter. Example) dist = {3, 5, 4} k = 2 Buildings are at 0, 3, 8, 12 step 1) Range is [0, 12(= 3 + 5 + 4)], X = (0 + 12) / 2 = 6 - Building at 0 is not covered because no transporter is built. So, build transporter at 3(= 0 + 6 / 2) and the transporter is covers from 0 to 6. We don't need to care about left buildings of current building. Because we build transporter from left to right. So, build transporter at 3 is most efficient choice in this situation. - 3 is covered because transporter is at 3 - 8 is not covered. So, build transporter at 11(= 8 + 6 / 2) and covers from 8 to 14. - 11 is covered by transporter at 11. - Need 2 transporter to cover all buildings. So, 6 is possible answer. New range is [0, 6]. step 2) Range is [0, 6], X = 3 - Build transporter at 1.5 and [0, 3] is covered. Note that we can build transporter at anywhere. - 3 is covered. - 8 is not covered. So, build transporter at 9.5 and covers from 8 to 11 - 12 is not covered. build transporter at 13.5 - Need 3 transporter to cover all buildings. So, 3 is impossible answer. New range is [4, 6] step 3) Range is [4, 6], X = 4 ((int)4.5) - Build transporter at 2 and covers from 0 to 4 - 3 is covered - 8 is not covered. build transporter at 10 and covers from 8 to 12 - 12 is covered - Need 2 transporter and 4 is possible answer. - new range is [4, 4] - 4 is answer of this case. Complexity: D := distance between first building and last building N := number of buildings Time Complexity : O(NlogD) - logD : cost of binary search - N : cost to judge Space Complexity : O(N) - N : size of distances array ```java public class ChainCity { public int findMin(int[] dist, int k) { int[] pos = convert(dist); return solve(pos, k); } private int[] convert(int[] dist) { int[] pos = new int[dist.length + 1]; int cur = 0; for (int i = 0; i < dist.length; i++) { pos[i] = cur; cur += dist[i]; } pos[dist.length] = cur; return pos; } private int solve(int[] pos, int k) { int l = 0, r = pos[pos.length-1]; while (l < r) { int c = (l + r) / 2; if (isPossible(c, k, pos)) { r = c; } else { l = c + 1; } } return r; } private boolean isPossible(int x, int k, int[] pos) { int coverTo = -1; for (int p : pos) { if (p > coverTo) { k--; coverTo = p + x; } } return k >= 0; } } ``` ## Div I - Easy - InterleavingParenthesis This is a straightforward dynamic programming problem. We need to count the number of strings which satisfy the following two conditions: * contains an equal number of left and right parentheses * at no point in the string does the number of right parentheses exceed the number of left parentheses Let dp[i][j] be of the number of ways to combine the first i characters of s1 and the first j characters of s2 into a string such that the second condition is satisfied. If there are more right parentheses than left parentheses among the first i characters of s1 and the first j characters of s2, then we set dp[i][j]=0, because the second condition will be violated. Otherwise, we set dp[i][j]=dp[i-1][j]+dp[i][j-1], taking mod 10^9+7 as necessary. Finally, we must check if the first condition holds when we use all characters from both strings. If it does, the answer is dp[s1.length()][s2.length()]. Otherwise, the answer is 0. ```clike #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i=a; i<b; i++) #define F0R(i, a) for (int i=0; i<a; i++) const int MOD = 1000000007; class InterleavingParenthesis { public: int dp[2501][2501], cum1[2501], cum2[2501]; int countWays(string s1, string s2) { F0R(i,2501) F0R(j,2501) dp[i][j] = 0; F0R(i,2501) cum1[i] = cum2[i] = 0; F0R(i,s1.length()) { cum1[i+1] = cum1[i]; if (s1[i] == '(') cum1[i+1] ++; else cum1[i+1] --; } F0R(i,s2.length()) { cum2[i+1] = cum2[i]; if (s2[i] == '(') cum2[i+1] ++; else cum2[i+1] --; } dp[0][0] = 1; F0R(i,s1.length()+1) F0R(j,s2.length()+1) if (cum1[i]+cum2[j] >= 0) { if (i > 0) dp[i][j] = (dp[i][j]+dp[i-1][j]) % MOD; if (j > 0) dp[i][j] = (dp[i][j]+dp[i][j-1]) % MOD; } if (cum1[s1.length()]+cum2[s2.length()] == 0) return dp[s1.length()][s2.length()]; 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