quiz2_sol - Introduction to Algorithms Massachusetts...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Algorithms December 9, 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik D. Demaine and Charles E. Leiserson Handout 33 Quiz 2 Solutions 0 2 4 6 8 0 10 12 14 10 20 30 40 50 60 70 80 90 100 110 120 130 140 # of students Quiz 2 Score Problem 1. Ups and downs Moonlighting from his normal job at the National University of Technology, Professor Silver- meadow performs magic in nightclubs. The professor is developing the following card trick. A deck of n cards, labeled 1 , 2 , . . . , n , is arranged face up on a table. An audience member calls out a range [ i, j ] , and the professor flips over every card k such that i k j . This action is repeated many times, and during the sequence of actions, audience members also query the professor about whether particular cards are face up or face down. The trick is that there are no actual cards: the professor performs these manipulations in his head, and n is huge. Unbeknownst to the audience, the professor uses a computational device to perform the manip- ulations, but the current implementation is too slow to work in real time. Help the professor by designing an efficient data structure that supports the following operations on n cards: F LIP ( i, j ) : Flip over every card in the interval [ i, j ] . I S -F ACE -U P ( i ) : Return TRUE if card i is face up and FALSE if card i is face down.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Handout 33: Quiz 2 Solutions Solution: Let F be the number of F LIP operations requested by the audience. Notice that perform- ing a single flip F LIP ( i, j ) is equivalent to performing two flips, F LIP ( i, ) and F LIP ( j + 1 , ) . One fast solution is to use a dynamic order-statistic tree, where an element with key i represents a flip of the interval [ i, ) . For both x = i and x = j + 1 , F LIP inserts x into the tree T if x ∪≡ T , and deletes x from T if x T . I S -F ACE -U P is implemented by returning true if the number of elements in the tree less than i (call it z ) is even, and false if z is odd. One way to compute z is to insert i into T , set z to be one less than the rank of i , and then delete i from T . This data structure requires O (min { F, n } ) space and supports F LIP and I S -F ACE -U P operations each in O (lg(min { F, n } )) time. A completely correct solution of this type received full credit. The following list describes other types of solutions that students submitted and the approximate number of points awarded to each. 1. Another solution is to create a static 1-d range tree T containing the elements from 1 to n as its keys. Each node x in the tree stores a bit that corresponds to a flip of the interval of all elements in the subtree rooted at x . F LIP ( i, j ) performs a range query on [ i, j ] , and flips the stored bit for the O (lg n ) disjoint subtrees that are found by the query. I S -F ACE -U P ( i ) walks down the range tree to the node for i and returns true if the sum of the bits of nodes along the path from the root to
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/03/2009 for the course CS 6.033 taught by Professor S during the Fall '09 term at MIT.

Page1 / 13

quiz2_sol - Introduction to Algorithms Massachusetts...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online