1 / 19

Infinite Coding

Infinite Coding. HKOI 2012 Senior Q1. Problem. N activities M of them are coding ‘Cut and paste’ operation with cost w[ i ] Minimum cost to group all coding. Solution. First, lets assume that we cannot cross midnight. Coding. Observation.

knut
Download Presentation

Infinite Coding

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. Infinite Coding HKOI 2012 Senior Q1

  2. Problem • N activities • M of them are coding • ‘Cut and paste’ operation with costw[i] • Minimum cost to group all coding

  3. Solution • First, lets assume that we cannot cross midnight Coding

  4. Observation • Lemma: There exists a optimal solution that do not involves moving all coding sessions • Proof: We can always group all coding sessions by moving M-1 of them Choose a coding session and fix it

  5. Solution Fix

  6. How do we group them Focus on the left side.. Move away Move away

  7. How do we group them Left of the line: Move the coding sessions to the fix one Right of the line: Move the non-coding sessions away How to find the optimal line?

  8. How do we group them Try all possible position Sum up the cost of coding session Sum up the cost of non-coding session +

  9. Algorithm 1 • Try all coding session and fix it. • Left side: try all possible position for the line. • Sum up the cost. • Choose the best line • Right side: do the same thing. • Choose the best one at all. • Complexity: O(N3) • Works for N up to 100, 25 marks O(N) O(N) O(N)

  10. Speed up Adding is too slow! Sum up the cost of coding session Sum up the cost of non-coding session Do it in O(1)? +

  11. Speed up • F(i) = Sum of cost of coding session in 1, 2, 3 .., i • G(i) = Sum of cost of coding session in 1, 2, 3 .., i • Compute F() and G() at the beginning

  12. Speed up • j i G(j) F(i) – F(j)

  13. Algorithm 2 • Compute F() and G() • Try all coding session and fix it. • Left side: try all possible position for the line. • Compute the cost • Choose the best line • Right side: do the same thing. • Choose the best one at all. • Complexity: O(N2) • Works for N up to 3000, 50 marks O(N) O(N) O(N) O(1)

  14. Speed up • What we are doing • For each i, find j (j < i) such that F(i) – F(j) + G(j)is smallest Define H(j) = F(j) – G(j) • For each i, find j (j < i) such that F(i) – H(j) is smallest

  15. Speed up • Finding greatest H(j) for j < i • Pre-compute • opt(i) = greatest H(j) for j < i • opt(i) = max(opt(i-1), H(i-1)) Cost = F(i) + opt(i)

  16. Algorithm 3 • Compute F(), G(), H() and opt() • Try all coding session and fix it. • Compute the optimal cost for left side • Compute the optimal cost for right side - Choose the best one at all. • Complexity: O(N) • Works for N up to 500000, 100 marks O(N) O(N) O(1) O(1)

  17. Wait.. How about the cross midnight scenario? Coding Coding

  18. Think in another way Grouping Coding Session = Grouping non-Coding Session

More Related