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 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.
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)
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
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
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
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 + A 2 ) 1,000, x 3,000