Anda di halaman 1dari 12

Codeforces #172 Tutorial

xiaodao

Contents
1 Problem 2A. Word Capitalization 2 Problem 2B. Nearest Fraction 3 Problem A. Rectangle Puzzle 4 Problem B. Maximum Xor Secondary 5 Problem C. Game on Tree 6 Problem D. k-Maximum Subsequence Sum 7 Problem E. Sequence Transformation 2 2 3 7 8 9 11

Problem 2A. Word Capitalization

Brief Description
Capitalize a given word.

Tags
implementation, strings

Analysis
As the rst problem in the problemset, this one is simple and direct. Just implement what is written in the statement under your favorite language.

Negative Example
h6363817: There are always some competitors start write down the code before they get a fully understanding or ignore on the restriction. They are not a single case. Cyber.1: Some of our coder are unfamiliar with the builtin-function, so they cost more time on the implementation than the others and it can enlarge their chance of making mistakes.

Suggested Code
astep shloub Hohol: The shortest Ruby, Python and Haskell Code.

Problem 2B. Nearest Fraction

Brief Description
Find the nearest fraction to fraction
x y

whose denominator is no more than n.

Tags
brute-force, greedy, math, number theory, ternary search

Analysis
This problem can be solved by a straight forward method since we are at Div II, simply enumerate all possible denominator, and calculate the nearest fraction for each denominator. Actually this is the limit denominator method implemented by the Python fractions module. As you would expect, we can do better. Check the source code if you are interested.

Negative Example
clonoboyka somay.jain: There are some competitor use strange algorithms that their complexity is hard to evaluate, and others are get into trouble with precision issues. I hope this problem could give you a lesson, they are just some rules that everybody need to know.

Suggested Code
wafrelka: A clean c++ implementation during the contest. havaliza: A short Python code as we mentioned above.

Problem A. Rectangle Puzzle

Brief Description
Calcuate the overlapping area with the rectangle before rotated.

Tags
geometry

Analysis
It may always be a good idea to a try simplied version before the situation gets into complicated. In this problem, a simplied version is to consider the square.

Here is our square situation. You can see the area can be calculated by squares Area - triangles Area. In the rectangle situation, however, things maybe a little bit confused. But if you come back to the problem after draw some pictures, you can found there are only two cases we need to dig out.

The rst situation is just like the square case we discusssed just now, the second situation will be better if you directly investigate the parallelogram. And here the key point is to nd the critical point between them. .. .. So what is the critical point? ..

You can calculate by exterior-interior angles, similar triangle, solving a system of linear equations of two unknowns even binary search on the angle or any other way you like. In the end, you ll nd = 2 arctan h .
w

And as long as you clearly distinguish these two cases, the greater part of the problem has been completed. For the other part of the competitors, they prefer to use a well implemented half-plane intersection algorithm to solve it. Check tourists solution for detail. This problem can also solved by convex-hull, just try to get all the intersection points.

Negative Example
UESTC Nocturne: A negative example hacked by scottai1, which didnt consider the second case mentioned above. This become a common phenomenon around other negative examples. (scottai1 himself failed because he use cout without setprecision.) liouzhou 101 wuzhengkai: Same as UESTC Nocturne, Peter50216 hacked wuzhengkais solution in the halfway, so wuzhengkai can struggle himself out during the remain contest. 6

Other two guys, unfortunately, do not have even that comfort.

Suggested Code
peter50216: The rst math solution during the contest. You can see how peter50216 arrange his code, so that it can stay away from error. dhh1995: The shortest cpp code written after the contest. sune2: The rst correct solution during the contest. This is the best half-plane intersection template I have ever seen. daizhenyang: Another well implemented half-plane intersection template. tourist: A light half-plane intersection function written by hand during the contest.

Problem B. Maximum Xor Secondary

Brief Description
... Youve been given a sequence of distinct positive integers. Your task is to nd a interval [l, r], so that the maximum element xor the second maximum element within the interval is as large as possible.

Analysis
The intervals value only depends on its maximum element and second maximum element, so apparently, there must be a lot of inessential interval which we neednt check them. But how can we do this? Maintain a monotone decreasing stack can help us. While a new element (Ai ) came into view, pop the top element (s) in the stack, and check the corresponding interval, until Ai > s. We can easily see that we wont lost the answer as long as it exists. All the element at most push and pop once, and only been checked when pop. So this is a O(n) algorithm.

Negative Example
ramtin95: Some competitors are misunderstanding on the term bitwise excluding OR. We fell so sorry about that but we cant give back your points. watashi: Some competitors use strange algorithm that we didnt know what they are doing. ...

Suggested Code
havaliza: The rst correct solution in the contest, which turn out to be a clean implementation for the algorithm explained above.

Problem C. Game on Tree

Brief Description
You are played a single-player game on a rooted tree. On each step, you choose a remaining node randomly, remove the subtree rooted by the node until all of them have been removed. Return the expectation of the number of steps in this game.

Analysis
The answer is
n i=1

1 Depth[i]

Here we denoted the Depth[root] = 0. So why it works? Let us observe each vertex independently, when one vertex has been removed, you need either to remove it directly, or to remove one of its ancestors. All these choices are equiprobable, so the probability of direct removal is
1 Depth[i] .

And the sum of them is the result.

This is been known as the Linearity of the Expected Value. This property almost arise every way in those problems related with probability. This method will also work when the objects of the delete operation is under a partial order relation.

For example, a DAG or even a digraph(run a algorithm to get the Strongly connected component). After that, it reduce to the classical descendence counting problem.

But when the problem is like, delete a node and all its neighbours, such disordered a operation, this method will cut no ice.

Suggested Code
Petr: Check Petrs code for detail.

Related Problem
Codeforces #146 DIV 1 Problem D. Graph Game Topcoder Open Round 1C Level 3. TheKnights ACM-ICPC Amritapuri onsite regionals 2012. Problem F. The Loyalty of the Orcs

Problem D. k-Maximum Subsequence Sum

Brief Description
Giving a number sequence Ai , on this sequence you need to implement the following two operations: 0 x v: Change Ax to v . 1 l r k: Query the k-MSS in [l, r]. Analysis Consider the static problem, Apparently, we can use a dynamic programing to solve it. f0 [i][j ]: the j-MSS in [0, i]. 9

f1 [i][j ]: the quasi-j-MSS in [0, i], which the item Ai must be selected. The state transition is enumerating whether the ith element is selected or not. Thats easy for a clever guy such as you. So the brute-force method is doing a dynamic programming for each query. The operation Modify takes O(1) time, and Query takes O(nk ) . Its too slow to get Accepted. A simple optimization is using a data structure to speed up Query. The segment tree can be OK. In every node, we store the such values in the interval represented by the node: the max sum if choosing j subsegments the max sum if choosing j subsegments with rst element chosen the max sum if choosing j subsegments with last element chosen the max sum if choosing j subsegments with both rst element and last element chosen Its clear that if we know the values of two intervals A and B , we can infer the values of the interval which is concatenating A and B . Just enumerating how many subsegments chosen in A and chosen in B . The time complexity of concatenating two intervals is O(k 2 ). So both operation Modify and Query take O(k 2 log n). The time limit is 5s in order to save Javas life. If you do some optimization, the solution can be Accepted. You can see liouzhou 101s solution for details. It seems hard to be optimized using such thought. Now we are going to use another totally dierent thought. We construct a max-cost max-ow model to solve this problem. The source is S and the sink is T . There are other n +1 vertices. A bidirectional edge between Vertex i and Vertex i +1 has cost Ai and capacity 1. The unidirectional edge from S to Vertex i has cost 0 and capacity 1 and the unidirectional edge from Vertex i to T has cost 0 and capacity 1. Its obvious for correctness. We can use Successive shortest path algorithm to solve the ow problem. But its as slow as the brute-force method, or even worse. Considering how the ow problem is solved, that is, how Successive shortest path algorithm works. We nd the longest path(max-cost instead of min-cost) between S and T , and then augment. We can simplify the algorithm due to the special graph. The new algorithm is: nd the subsegment with maximum sum negative the elements in the subsegment repeat the two steps k times the answer is the sum of the sum of the subsegment you found each time.

10

So, the key point is doing these two operations quickly. Segment tree can also be used. To nd the MSS you need to store the sum of a interval the maximum partial sum from left the maximum partial sum from right the MSS in the interval To negative a subsegment, the minimum elements must be stored. To nd the position of MSS, all the positions are supposed to kept. The complexity of Modify is O(log n), and the complexity of Query is O(k log k ). It can pass all the test data easily but it has a really large constant. This is UESTC Nocturnes solution and FattyPenguins solution The thought is hard to come up with and also to code. So lets applaud again for UESTC Nocturne and FattyPenguin. Also thanks for the great optimization of liouzhou 101.

Related Problem
SPOJ 1716. Can you answer these queries III APIO 2007 Problem B. backup HDU 1024. Max Sum Plus Plus POJ Challenge 2011.04.10 Problem B. Birthday Present()Fr)

Problem E. Sequence Transformation

Brief Description
Youve got n, Q, A, B and a non-decreasing sequence X1 , X2 , . . . , Xn (1 X1 X2 . . . Xn Q). Your task is transform sequence X1 , X2 , . . . , Xn into some sequence Y1 , Y2 , . . . , Yn (1 Yi Q; A Yi+1 Yi B ), and minimizes the transformation price which dened as:
n

(Yi Xi )2
i=1

Analysis
The description of the problem seems quite easy to understand. And anyone can put it into the mathematical as follow: w(i, x) = (x Xi )2 g (i, x) = min{f (i 1, y ) | x b y x a} 11

f (i, x) = g (i, x) + w(i, x) Here w(i, x) stands for the price on the ith element, and f (i, x) is the minimum transformation price if we set Yi equal to x, and g (i, x) is the auxiliary function to calculate f (i, x). Now, we can approach those functions from dierent angles. One is take them as dynamic programming states and their transition function. But it turn out to be a O(nQ2 ) in the worst case. It will denitely not work and we need come up with an algorithm whose time complexity doesnt have Q into a factor. Since we cannot get any useful observation immediately after we write down the equation ... (Underconstruction ... ) ...

Related Problem
CTSC 2009. Sequence Transform(S C) ...

12

Anda mungkin juga menyukai