Presentation is loading. Please wait.

Presentation is loading. Please wait.

(())()(()) ((())()(())()(()))

Similar presentations

Presentation on theme: "(())()(()) ((())()(())()(()))"— Presentation transcript:

1 (())()(()) ((())()(())()(()))
USACO (())()(()) ((())()(())()(()))

2 USACO November Contest
The first USACO competition was last week. Lynbrook had a very strong performance as a whole Congratulations to Andy Wang (1000), Raphael Chang (933), and Edward Lee (867) for promoting to Silver Kudos to Johnny Ho (875) for scoring highest out of the US seniors on Gold Congratz to Steven Hao (938) for proving that he can O(n^2) bash problems Good job to the random 5th grader who got a perfect on Silver Full results and problems are at

3 Bronze Contest: "Typo" Input: an arbitrary string of parentheses of length n, n < 100,000 Output: the number of ways you can flip exactly one of the parentheses to make the string balanced (not necessarily positive) Example: (((()) answer = 3 change either the 2nd, 3rd, or 4th parens Solution: Prefix sums The stuff we covered on the Halloween PotW

4 Bronze Contest: "Typo" Figure out which type of parentheses is in excess (kind of like a limiting reaction) (((()): 4 ('s, 2 )'s You should convert one of the ('s Orient it so that there are too many )'s instead of ('s Reversing/flipping the string does not change answer (((()) turns into (()))) Now you should convert one of the )'s Convert parentheses into numbers (()))) turns into {+1, +1, -1, -1, -1, -1} Take prefix sums {1, 2, 1, 0, -1, -2} A ) can be converted if and only if it lands at or before the first negative prefix sum

5 Silver/Gold Contest: "Balanced Cow Breeds"
Input: an arbitrary string of parentheses of length n, n < 1000 Output: the number of ways you can color each parenthesis such that each color forms its own balanced string of parentheses Example: (()) (()) Solution: Dynamic programming The stuff we were just about to cover :(

6 Silver/Gold Contest: "Balanced Cow Breeds"
Use recursion. int recurse(int index, int nestA, int nestB) If nextA or nestB become negative, quit immediately If index reaches the end of the string, return 1 if and only if nestA == 0 and nestB == 0 Otherwise, you can color index in 2 ways. You can change this to a 2D DP state nestB can be calculated from index and nestA This makes your algorithm use O(N^2) memory The algorithm visits O(N^2) states, each in constant time, for overall O(N^2)

7 "Concurrently Balanced Strings"
K strings of parentheses, each of length N. Count the number of ranges (a, b) satisfying: For any of the K strings, the substring from a to b is a balanced string of parentheses. K <= 10, N <= Example K = 3, N = 14, Answer: 3 N is large, need faster than quadratic. N is only A good N^2 may pass % of the test cases Similar for Gold Problem 3. s_1 = )()((())))(()) s_2 = ()(()()()((()) s_3 = )))(()()))(())

8 "Concurrently Balanced Strings"
Consider a single string. (Let K = 1) Compute for each prefix of the string its "sum" Sum of a string is # left parens - # right parens. sums[i] is the score of the prefix ending at index i. We can check if a substring from A to B is balanced. Sum of this substring is sums[B] - sums[A] This must be 0, so sums[A] == sums[B] For all A < C < B, sums[C] >= sums[A] Iterate over B Count the number of possible A. Also keep track of the negative condition. Use a map for O(n log n) Store K sums instead of 1 for original problem Steven's Code (Expertly uses map<int, int>)

9 In summary Getting partial credit by O(n^2) bashing O(nlogn) problems is a highly effective strategy Silver/bronze problems are all fairly standard Dynamic programming Graph theory (Dijkstra’s, BFS, DFS) Greedy Sorting Silver/bronze contestants have to be very careful to pass Test on both small and large test cases Now just pray that we don’t have any more problems about parentheses

10 PotW Last week's PotW is extended
The one about Karen's sleeping habits The problem remains largely unsolved Hints for Subtask 3, where N <= Try all possible rooms as centers Try the rooms in a specific order depth-first search Avoid recomputing the sum of distances to other rooms Imagine moving the center from one room to an adjacent room What needs to be precomputed?

Download ppt "(())()(()) ((())()(())()(()))"

Similar presentations

Ads by Google