220 likes | 352 Views
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
E N D
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 • 10549 Russian Dolls • 10453 Make Palindrome • IOI2002 day 2 batch scheduling
10003 Cutting Sticks • Example 0 4 5 7 8 10 i\F(i,j)\j 0 4 5 7 8 10 0 0 0 5 10 15 22 4 0 0 3 7 12 5 0 0 3 8 7 0 0 3 8 0 0 10 0
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
A B C D E 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
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
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
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 41 6 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
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
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) )
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.
Circular Massacre • Example: n=10, k=2 1 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)
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
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.
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
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.
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
10549 Russian Dolls • Sorting (so that the later dolls cannot contain the previous dolls) • 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)
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
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]}
IOI2002 day 2 batch scheduling • Question & handout: http://olympiads.win.tue.nl/ioi/ioi2002/contest/day2/batch/batch.pdf http://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 that Ci = min { Ci(k) | k = i+1, … , n+1} for 1<=i<=n, and Cn+1 = 0.
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.