Solution:: Quiz 3
Solution:: Quiz 3
Solution: Quiz 3
• Do not open this quiz booklet until directed to do so. Read all the instructions on this page.
• When the quiz begins, write your name on the top of every page of this quiz booklet.
• You have 60 minutes to earn a maximum of 60 points. Do not spend too much time on any
one problem. Skim them all first, and attack them in the order that allows you to make the
most progress.
• You are allowed three double-sided letter-sized sheet with your own notes. No calcula-
tors, cell phones, or other programmable or communication devices are permitted.
• Write your solutions in the space provided. Pages will be scanned and separated for grading.
If you need more space, write “Continued on S1” (or S2, S3) and continue your solution on
the referenced scratch page at the end of the exam.
• Do not waste time and paper rederiving facts that we have studied in lecture, recitation, or
problem sets. Simply cite them.
• When writing an algorithm, a clear description in English will suffice. Pseudo-code is not
required. Be sure to argue that your algorithm is correct, and analyze the asymptotic
running time of your algorithm. Even if your algorithm does not meet a requested bound,
you may receive partial credit for inefficient solutions that are correct.
• Pay close attention to the instructions for each problem. Depending on the problem,
partial credit may be awarded for incomplete answers.
Name:
School Email:
2 6.006 Solution: Quiz 3 Name
(a) [1 point] Write your name and email address on the cover page.
Solution: OK!
1. Subproblems
• Let the card values be C1 , C2 , . . . , C2n from top to the bottom
• Let x(2i) be the maximum advantage possible after dealing a total of 2i cards from the
top of the deck, i to each player, in the specified manner
2. Relate
• Either Annie deals her opponent one card or two (Guess!)
• If she deals one, her opponent gets the next card, and she gets the one after
• If she deals two, her opponent gets the next two cards, and she gets the two after
• x(2i) = max{x(2i − 2) − C2i−1 + C2i , x(2i − 4) − (C2i−3 + C2i−2 ) + (C2i−1 + C2i )}
• Subproblems x(2i) only depend on strictly smaller i, so acyclic
3. Base
• x(0) = 0 (nothing dealt yet)
• x(2) = −C1 + C2 (two cards dealt)
4. Solution
• Solve subproblems x(2i) in order of increasing i for 2 ≤ i ≤ n
• Solution to problem is subproblem x(2n), maximizing the entire 2n-card deck
5. Time
• # Subproblems is n + 1
• Work per subproblem is O(1)
• Total running time is O(n)
6.006 Solution: Quiz 3 Name 5
1. Subproblems
• Let si be 1 if plate pi is sweet and 0 otherwise
• x(i, j, s0 ): largest volume of food orderable from plates pi to pn , using at most j calories,
ordering exactly s0 sweet plates
2. Relate
• Either we order plate pi or not. Guess!
• If order pi , get vi in volume but use ci calories
• If pi is sweet, need to order one fewer sweet plate
vi + x(i + 1, j − ci , s0 − si )) if ci ≤ j and si ≤ s0
0
• x(i, j, s ) = max
x(i + 1, j, s0 ) always
0
• Subproblems x(i, j, s ) only depend on strictly larger i so acyclic
3. Base
• For all j ∈ {0, . . . , k}: x(n + 1, j, 0) = 0 and x(n + 1, j, s0 ) = −∞ for s0 6= 0
4. Solution
• Solve in order of decreasing i, then j and s0 in any order
• Solution to problem is subproblem x(1, k, s)
5. Time
• (# Subproblems) × (work per problem) = O(nsk) × O(1) = O(nsk) = O(n2 k)
6 6.006 Solution: Quiz 3 Name
1. Subproblems
• x(i, j): min cost of catching monsters mi to mn starting at location mj for j ≤ i
2. Relate
• Either acquire monster mi by purchasing or ride-sharing to location. Guess!
• If purchase spend ci dollars, else need to ride share to mi from mj
min(ci + x(i + 1, j), s(j, i) + x(i, i)) if j < i
• x(i, j) =
x(i + 1, j) if j = i
• Subproblems x(i, j) only depend on strictly larger i + j so acyclic
3. Base
• x(n + 1, j) = 0 for j ∈ [1, n]
4. Solution
• Solve in order of decreasing i, then decreasing j
• Solution given by x(1, 1)
5. Time
• (# Subproblems) × (work per problem) = O(n2 ) × O(1) = O(n2 )
6.006 Solution: Quiz 3 Name 7
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S1” on the problem statement’s page.
8 6.006 Solution: Quiz 3 Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S2” on the problem statement’s page.
6.006 Solution: Quiz 3 Name 9
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S3” on the problem statement’s page.
10 6.006 Solution: Quiz 3 Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S4” on the problem statement’s page.