dynamic programming (ii)(iii)

22
Dynamic Programming (II)(III) 28/8/2004 11/9/2004 Presented by Eddy Chan

Upload: remedios-carrillo

Post on 02-Jan-2016

53 views

Category:

Documents


0 download

DESCRIPTION

Dynamic Programming (II)(III). 28/8/2004 11/9/2004 Presented by Eddy Chan. Problems to be covered. (mostly taken from acm.uva.es) 10003 Cutting Sticks 10544 Numbering the Paths 10564 Paths through the Hourglass Circular Massacre HKOI 2001 Senior Q4 Effective Enterprise - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Dynamic Programming (II)(III)

Dynamic Programming (II)(III)

28/8/2004

11/9/2004

Presented by Eddy Chan

Page 2: Dynamic Programming (II)(III)

Problems to be covered

(mostly taken from acm.uva.es) 10003 Cutting Sticks 10544 Numbering the Paths 10564 Paths through the Hourglass Circular Massacre HKOI 2001 Senior Q4 Effective Enterprise 10549 Russian Dolls 10453 Make Palindrome IOI2002 day 2 batch scheduling

Page 3: Dynamic Programming (II)(III)

10003 Cutting Sticks

Example0 4 5 7 8 10i\F(i,j)\j0 4 5 7 8 100 0 0 5 10 15 224 0 0 3 7 125 0 0 3 87 0 0 38 0 010 0

Page 4: Dynamic Programming (II)(III)

10003 Cutting Sticks

State: portion start, portion end Formula: F(i,j)=0 if |i-j|<=1 F(i,j)=min( F(i,k)+F(k,j) ) + j-i

i+1<=k<=j-1

Page 5: Dynamic Programming (II)(III)

10544 Numbering the Paths In sample input: AB AC BC CD CE ED If the query is : ACD

ABCD ABCED ACD ACED So the answer is 3

A

B C

D E

Page 6: Dynamic Programming (II)(III)

10544 Numbering the Paths After Topological sort:

ABCED Let No. of paths from i to j be P(i,j) Let F(S) be the number of the path S

I A B C E D

P(i,D) 4 2 2 1 1

F(ACD)=F(ABD)+1=P(B,D)+1=2+1

Page 7: Dynamic Programming (II)(III)

10544 Numbering the Paths Let A=x1, B=x2, …, Z=x26 F(xi1 xi2 … xiN)=P(x1,xiN)+…+P(x(i1-1),xiN)+ P(xj1,xiN) for all j1 s.t. (xi1,xj1) is a road and

j1<xi2+ P(xj2,xiN) for all j2 s.t. (xi2,xj2) is a road and

j2<xi3…+1

Page 8: Dynamic Programming (II)(III)

10564 Paths through the Hourglass A way to store the values: Table[r,c]--L->Table[r+1,c] Table[r,c]--R->Table[r+1,c+1]6 416 7 2 3 6 8 1 8 0 7 1 2 6 5 7 3 1 0 7 6 8 8 8 6 5 3 9 5 9 5 6 4 4 1 3 2 6 9 4 3 8

Page 9: Dynamic Programming (II)(III)

10564 Paths through the Hourglass F(0,j,Table[0,j])=1 F(0,j,k)=0 if k<>Table[0,j] F(i,j,s)=F(i-1,j-1,s-Table[i,j])+F(i-1,j,s-

Table[i,j]) We can use another storage P(i,j,s) to store

the previous value, so that we can trace back the path

Page 10: Dynamic Programming (II)(III)

HKOI 2001 Senior Q4 Effective Enterprise(The question can be obtained in the official website of HKOI)

A, B, C are the number of ppl recruited in Department A, B and C respectively

Ca(i), Cb(i), Cc(i) are the contributions of person i to department A, B and C respectively

F(A,B,C,i)=max(F(A-1,B,C,i-1)+Ca(i),F(A,B-1,C,i-1)+Cb(i),F(A,B,C-1,i-1)+Cc(i),F(A,B,C,i-1))

Page 11: Dynamic Programming (II)(III)

Circular Massacre (similar to judge 1030) There are n people sit around a table,

numbered 1 to n clockwisely. Every k person will be killed, until there is only 1 person left. Find the survivor.

Page 12: Dynamic Programming (II)(III)

Circular Massacre

Example: n=10, k=21 2 3 4 5 6 7 8 9 10

The sequence of death: 2 4 6 8 10 3 7 1 9

and the survivor is 5

F(n)=(F(n-1)+2-1) mod (n) +1

If ppl are numbered from 0 to n-1 F(n)=(F(n-1)+2) mod (n)

Page 13: Dynamic Programming (II)(III)

Circular Massacre

k=2 N F(n) 1 1 2 1 3 3 4 1 5 3 6 5 7 7 8 1 9 3 10 5

Page 14: Dynamic Programming (II)(III)

Circular Massacre

Explanation: When we have 10

people, after the first one is killed, there are 9 people left. If we know the survivor of the case n=9, by mapping the values to the case n=10, we know the survivor for n=10.

Page 15: Dynamic Programming (II)(III)

Circular Massacre

If now we kill one person every k people, which is the survivor?

F(n)=(F(n-1)+k) mod (n) Example: k=3

Page 16: Dynamic Programming (II)(III)

Circular Massacre

We want to have a better solution to the problem.

Look at the table for k=3: F(n)=(F(n-1)+3-1) mod (n)

+1 If we know the highlighted

values (F(N)=1 to k-1), we know the survivor.

N F(N)

1 1 14 2 27 20

2 2 15 5 28 23

3 2 16 8 29 26

4 1 17 11 30 29

5 4 18 14 31 1

6 1 19 17 32 4

7 4 20 20

8 7 21 2

9 1 22 5

10 4 23 8

11 7 24 11

12 10 25 14

13 13 26 17

Page 17: Dynamic Programming (II)(III)

Circular Massacre

Rule for k=3 if N-F(N)=2m , next value is N+m+1,

F(N+m+1)=2 if N-F(N)=2m+1, next value is N+m+1,

F(N+m+1)=1

general case: if N-F(N)=(k-1)m+i, next value is N+m+1,

F(N+m+1)=k-1-i

Page 18: Dynamic Programming (II)(III)

10549 Russian Dolls

1. Sorting (so that the later dolls cannot contain the previous dolls)

2. DP

F(the current doll, the number of dolls for first person, the number of dolls for second person, the last doll for first person, the last doll for second person)

Page 19: Dynamic Programming (II)(III)

10549 Russian Dolls

Note that:

1. the number of dolls for second person = the current doll - number of dolls for first person

and

2. there should be at least one person has the last doll

Page 20: Dynamic Programming (II)(III)

10453 Make Palindrome

Same as doing longest common subsequence

F[i][j] = F[i-1][j-1] + 1 if A[i]=B[j] F[i][j] = max{F[i-1][j], F[i][j-1]}

Page 21: Dynamic Programming (II)(III)

IOI2002 day 2 batch scheduling Question & handout: http://olympiads.win.tue.nl/ioi/ioi2002/contest/day2/batch/batch.pdfhttp://olympiads.win.tue.nl/ioi/ioi2002/contest/day2/batch/batch-handout.pdf (refer to the handout for the question): Let Ci be the minimum total cost of all partitionings of jobs Ji, Ji

+1, … , Jn into batches. Let Ci(k) be the minimum total cost when the first batch is selected as

{Ji, Ji +1, … , Jk-1} That is, Ci(k) = Ck + (S + Ti + Ti+1 + …+ Tk-1) * (Fi + Fi+1 + … + Fn). Then we have thatCi = min { Ci(k) | k = i+1, … , n+1} for 1<=i<=n, and Cn+1 = 0.

Page 22: Dynamic Programming (II)(III)

IOI2002 day 2 batch scheduling Notice that if you do it from job 1 to job n, you

have to add one more parameter in your DP table, which is the number of batches. This will increase the runtime to O(n3)

There is an O(n) solution, you may read it yourself if you are interested.