1 / 11

Songs

Songs. HKOI 2012. Problem Description . Dr. Jones wants to play the lowest key possible Musical notes are represented by positive integers Increasing the key of the song by x means that all notes of the song are increased by x. Example. Key = 0. Example. Key = 1. Constraints.

leora
Download Presentation

Songs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Songs HKOI 2012

  2. Problem Description • Dr. Jones wants to play the lowest key possible • Musical notes are represented by positive integers • Increasing the key of the song by x means that all notes of the song are increased by x.

  3. Example Key = 0

  4. Example Key = 1

  5. Constraints • In test cases worth 20% • 1 ≤ A, B ≤100 • In test cases worth 50% • 1 ≤ A, B ≤ 3000 • A ≤N ≤3000 • In all test cases, • 1 ≤ A,B ≤ 3000 • A ≤N ≤ 1,000,000 • Both lists are sorted in ascending order • The highest note that the instrument can play is not greater than 2N.

  6. Solution 1: Loop the answer • We start from K = 0 -> 2N • For each note in A • Find corresponding note A[i] + K in B • Linear Search --- O(B) • If all notes can be played, output K • Time complexity: O(NAB)

  7. Solution 1a: Loop the answer Improved • We start from K = 0 -> 2N • For each note in A • Check whether corresponding note A[i] + K in B • O(1) • If all notes can be played, output K • Time complexity: O(NA) • 3000 x 1,000,000

  8. Solution 2: Loop the song • It is mentioned in the task that the song starts with 1. • To be able the play that note, the choice of keys (answers) are limited. • That means the answers can only be one of B[i]-1

  9. Solution 2: Loop the song • For each K = B[i]-1 • For each note in A • Check whether corresponding note A[i] + K in B • O(1) • If all notes can be played, output K • Time complexity: O(AB) • 3000 x 3,000

  10. Solution 1b: Loop the answer Improved!! • We start from K = 0 -> 2N • For each note in A • Check whether corresponding note A[i] + K in B • O(1) • If not, break • If all notes can be played, output K • Time complexity: O(N + A2) • 1,000,000 + 3000 x 3,000

More Related