Download
songs n.
Skip this Video
Loading SlideShow in 5 Seconds..
Songs PowerPoint Presentation

Songs

121 Vues Download Presentation
Télécharger la présentation

Songs

- - - - - - - - - - - - - - - - - - - - - - - - - - - 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