Presentation is loading. Please wait.

Presentation is loading. Please wait.

Songs HKOI 2012. Problem Description Dr. Jones wants to play the lowest key possible Musical notes are represented by positive integers Increasing the.

Similar presentations


Presentation on theme: "Songs HKOI 2012. Problem Description Dr. Jones wants to play the lowest key possible Musical notes are represented by positive integers Increasing the."— Presentation transcript:

1 Songs HKOI 2012

2

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

4 Example 1245 23456 Key = 0

5 Example 1245 23456 Key = 1

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

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

8 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

9 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

10 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

11 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,000 + 3000 x 3,000


Download ppt "Songs HKOI 2012. Problem Description Dr. Jones wants to play the lowest key possible Musical notes are represented by positive integers Increasing the."

Similar presentations


Ads by Google