12
Problem A: Goalkeepers Setter: Mohammad Mahmudur Rahman Tester: Md. Towhidul Islam Talukder Category: Pre-school No need for an analysis. If you need a challenge, try to solve it with Max-Flow. ;-)

BGC2012 Contest

Embed Size (px)

DESCRIPTION

programming contest set analysis

Citation preview

Page 1: BGC2012 Contest

Problem A: GoalkeepersSetter: Mohammad Mahmudur RahmanTester: Md. Towhidul Islam TalukderCategory: Pre-school No need for an analysis. If you need a challenge, try to solve it with Max-Flow. ;-)

Page 2: BGC2012 Contest

Problem B: Distinct SubstringSetter: Tasnim Imran SunnyTester: Kazi Rakibul HossainCategory: String In this problem, the string ‘bca’ is considered equal to the string ‘abc’. It will be convenient if we denote all the rotations of a specific string by that string. Call it the ‘identifier string’ for now. For example: ‘bca’ has rotations ‘cab’, ‘abc’ and ‘bca’. Any of the string can be made an identifier string to the other strings. The question is which one? One idea is to make the lexicographically smallest rotation of the string the identifier string. So if we get a substring of the original string, we find it’s lexicographically smallest rotation and check if it’s already been counted. algorithm: for each substring:

find its lexicographically smallest rotationcheck it’s existance

Choosing start and end index of the original string can be done in O(N^2). The existence check is can be done in O(N) if we use a trie. The naive determination of lexicographically smallest rotation is O(N^2). That makes the algorithm O(N^4) which is too slow for this problem. Determining lexicographically smallest rotation in O(N): Fact: The lexicographically smallest rotation is the lexicographically smallest suffix + the prefix for the suffix of the given string. (Here + means string concatenation). Generally strings are concatenated with each other for use with rotations to simplify the operation. But for this problem the length of concatenated strings can get as much as 400, so it may be slow. lcp(a, b) is the length of the longest common prefix of the suffixes at index a and b of the original string. Suppose f(a, b, len) = comparison of two strings having starting indices a and b and length len. The value should be (less, equal, greater) based on comparison. We define comp(s, a, b, len) is a function that returns true if we are considering a substring of the original string starting at index s and of length len and it’s suffix starting at a is less than the suffix starting at b. Computation of comp has three cases:1. f(a, b, s+len-b) compares suffixes at a and b with length of the suffix at b

Page 3: BGC2012 Contest

if (1) returns equal (the suffixes have equal prefix of length s-len+b), Let, l1 = s+len-b, l2 = s+len-a 2. f(a + l1, s, s+len-a) compares suffixes at a + l1 and s with length of the suffix at s+len-a if (2) return equal. 3. f(s,b+l2,len-l1-l2) compares suffixes at s and b+l2 with length of the suffix at len-l1-l2. f(a, b, len) is computed the following way:

return equal : if lcp(a,b) >= lenreturn f(a + lcp(a,b), b + lcp(a,b))

The general algorithm is the following:for each left and right position (a,b) of the substring:

idx := afor each c in (a <= c <= b):

if (comp(a, idx, c, b-a+1))idx := c

check and insert rotation of substring (a,b) starting at idx into the trie if not found.

increment the answer counter if it’s a new substring The overall complexity is O(N^3).

There is another algorithm which can find the minimum lexicographic rotation in O(N). It is described here: (http://online-judge.uva.es/board/viewtopic.php?t=42601&f=22#p132758) This problem can be solved using Suffix Automata too.

Page 4: BGC2012 Contest

Problem C: C for CountSetter: Anindya DasTester: Md. Mahbubul HasanCategory: Dynamic Programming, Combinatorics Let's pretend that the circular array is actually linear. Solution 1, (O(N*K*D) solution): Let f(n, k, d) meaning considering only first n objects, number of ways of taking k of them and distance from last taken object is d.

Transition: We can take the current object: f(n+1, k+1, 1)We skip the object: f(n+1, k, d+1)

We can take an object only when the last object is at least at d distance. Solution 2, (O(N*K) solution): Notice, if we take an object at some position i, we cannot take an object at a position less than i+d. So the recurrence can be simplified. f(n, k) meaning considering only first n objects, number of ways of taking k of them maintaining the distance condition.

Transition: We can take the current object: f(n+d, k+1)We skip the object: f(n+1, k)

The circular condition can be imposed by the position of the last taken object. With some effort it’s possible to arrive to a combinatorial solution to this problem.

Page 5: BGC2012 Contest

Problem D: Fishermans DilemmaSetter: Mir Wasi AhmedTester: Abu Obaida OpuCategory: Bruteforce, Well known technique involving prefix sums, Dynamic Programming At first we pre-calculate the number of fishes in the rectangle with upper left corner (1,1) and lower right corner (r,c) using dp and principle of inclusion-exclusion in an array called P[r][c]. This is a well known technique. The formula is the following: P[r][c] = P[r][c-1] + P[r-1][c] - P[r-1][c-1] + fishes[r][c] (fishes[r][c] := number of fishes in the cell (r,c)) Say we are at a position (rp, cp). We know the number of fishes in the rectangle (1,1) - (rp,cp) (like above). Given the upper left and lower right corner of any rectangle inside, we can find the sum of fishes inside it in O(1) using a similar technique (inclusion exclusion). We can choose the lower, upper, left and right edges of the rectangle and check. That would be O(N^4) and won't run in time. Instead we can choose lower, upper and right and solve the problem. The constraints let us do so. When going rightwards the amount of fishes in the rectangle with fixed upper and lower edge, the total amount of fish in the rectangle (upper, 1) - (lower, right) will always increase due to the positive number of fishes inside it. So, If you at at a right edge and found the left edge for which (upper, left) - (lower, right) is greater than or equal to K, you can rest assure that rectangles with left edges less than left will always comply to the criteria of being less than or equal to K. We will now claim that the left edge la for a right edge ra will always be greater than or equal to the left edge lb for a right edge rb with rb > ra. You can prove it using proof by contradiction. So we'll keep moving the left edge rightwards until it reaches to a position where going right will make an invalid rectangle. The overall complexity is O(N^3). During onsite contest, some of the teams used an O(N^3 lg N) algorithm. The idea is the same sans the last observation.

Page 6: BGC2012 Contest

Problem E: Chatgaiya Postman ProblemSetter: Md. Shiplu HawladerTester: Md. Mahbubul HasanCategory: Dynamic Programming, Combinatorics, Exponential States This problem is a modification to the Travelling salesman problem. The solution uses dynamic programming with state (mask of visited cities, starting city, current city, flag whether one loop is completed). At each city go to an unvisited city or return to the starting city (and mark the flag). Overall complexity O(N^3 * 2^N)

Page 7: BGC2012 Contest

Problem F: Ant's shopping mallSetter: Kazi Rakibul HossainTester: Md. Towhidul Islam TalukderCategory: Ad Hoc As the ant queen can start at any column of the top row, we can brute force on that. Then the queen's path with be the one along that column.Say the queen's chosen column is denoted by c. She is at i-th row (here, 1<=i<=R). If there is no shop, then fine, she can be at that point.If there is a shop, the owner needs to displace it. He can move it towards left or towards right. Notice that, to move the shop at (i, c) to the left, he needs to find an empty spot at (i-1,c). If it is occupied, the shop on it should be move to (i-2,c). And so on. It means we'll need to find the nearest blank point left to (i,c).Do the similar for moving right. The answer is the best of these two.If it's not possible to move a shop left or right, then it's impossible for the queen to make a trip. The overall time complexity is O(R*C^2) per testcase, which can be further optimized to O(R*C) with O(R*C) preprocessing.

Page 8: BGC2012 Contest

Problem G: I am dumb 3Setter: Md. Mahbubul HasanTester: Jane Alam JanCategory: Game theory Unfortunately, a similar problem came as division 1 hard of SRM 309. Actually it’s quite difficult to assure whether a problem appeared before in a contest or not with so many contests and online judges these days. Not too bad if you solved that before ;-). If you didn’t, it's better to read the editorial of that SRM. (http://community.topcoder.com/tc?module=Static&d1=match_editorials&d2=srm309)

Page 9: BGC2012 Contest

Problem H: The Cowhattan ExterminationSetter: Iqram MahmudTester: Md. Mahbubul HasanCategory: Ad-Hoc One thing to notice that you can kill at most one cow of a particular color. So to maximize the number of cows killed, you should kill one cow of each color. The answer is the number of distinct colors.

Page 10: BGC2012 Contest

Problem I: Balance the BlocksSetter: Abu Obaida OpuTester: Md. Mahbubul HasanCategory: Search It's a search problem, a really difficult one. Breadth-First-Search with pruning is the way to go. But straightforward implementation will not only time-out, but also consume a lot of memory. You need to find ways to optimize. Hint: Optimize on the basis of the following things

1. The boxes on corner rows/columns.2. States impossible to go.3. States hopeless for a better solution.

Page 11: BGC2012 Contest

Problem J: Bulky Process of Bulk ReductionSetter: Md. Mahbubul HasanTester: Abu Obaida OpuCategory: Data Structures, Segment Trees (This explanation requires knowledge about segment trees. If you don’t know about them, read this tutorial: http://wcipeg.com/wiki/Segment_tree) At first look, you may think - Update and Query operation on an array : smells like segment trees. Bingo! Now the question is: if we are at some node, how can we combine the results of the child nodes to get the answer? I’ll explain this with an example. Suppose we have a1, a2, a3, a4 in the range. Q(i,j) is the query value needed in this problem in the range [i, j]. Also, S(i,j) is the sum of values in the range [i, j]. We need to find Q(1,4) = 1*a1 + 2*a2 + 3*a3+4*a4. Writing them in a different way a1 +a2 +a3 +a4

a2 +a3 +a4+a3 +a4

+a4

Say we are at range[1,4]. It’s childs are [1,2] and[3,4]. Left child has a1 and a2. Q(1,2) can be written as: a1 +a2

+a2 The right child has a3 and a4. Q(3,4) can be written as: a3 +a4

+a4 From the desired sum of Q(1,4) see the red and blue part in that. And the green part too. a1 +a2 +a3 +a4

a2 +a3 +a4+a3 +a4

+a4

Page 12: BGC2012 Contest

The red part is the query sum of [1,2], blue part is the query sum of [3,4]. And the green part is actually the number of items in the left subtree times the sum of items in the right subtree (In this example: 2 items in [1,2] and the green part equals 2*(a3+a4).) So the formula should be: Q(i, j) = Q(i, m) + Q(m+1, j) + (m - i + 1) * S(m+1, j) [ here m = (i + j) / 2]S(i, j) = S(i, m) + S(m+1, j) [ S(i, i) = value at i-th index of the array] I won’t go into the details of implementation.