The Apr 8 - Apr 14 week was light on contests from the usual suspects, but Google Code Jam Round 1A filled the void (problems, results, top 5 on the left). Gennady.Korotkevich required just 26 minutes to wrap things up, with others following in approximately 5-minute increments. Congratulations to everybody who made it to Round 2!

In my previous summary, I have mentioned two problems. The first once came from ICPC World Finals: you are given a street with

The solutions gives a whole new meaning to the "meet in the middle" term. First of all, suppose all periods

Moreover, only slightly more complex solution exists in case any two periods are either relatively prime, or one divides the other. In each group of periods where one divides the other we'll pick the largest one, and go from left to right which units of time modulo that largest period have already seen red, and the probabilities for different groups can still be simply multiplied since they're independent.

However, we have arbitrary periods up to 100 in this problem, so the above condition does not hold. In order to overcome this, let's split the time into

The only remaining task is to find which value of

More generally, if the numbers were up to

We have essentially decomposed the problem

The second problem I mentioned came from Codeforces: you have

The key idea in solving this problem is to postpone the choice of

We will end up with at most

In some sense, the main obstacle in solving this problem is not realizing that it is actually easy :)

Thanks for reading, and check back for more!

In my previous summary, I have mentioned two problems. The first once came from ICPC World Finals: you are given a street with

*n*<=500 traffic lights on it arranged from left to right. The*i*-th traffic light stays red for*r*seconds, then green for_{i}*g*seconds, then repeats this cycle infinitely. The cycles for all traffic lights start at the same time 0,_{i}*r*and_{i}*g*are integers, and_{i}*r*+_{i}*g*does not exceed 100. Suppose you approach this street from the left at a random moment in time, and drive until you encounter a red light for the first time, or until you pass the entire street. What is the probability that you see red for the first time on 1st, 2nd, ...,_{i}*n*-th traffic light?The solutions gives a whole new meaning to the "meet in the middle" term. First of all, suppose all periods

*r*+_{i}*g*are relatively prime. In this case seeing red on each of the traffic lights are independent random events, and therefore our answers are_{i}*r*_{1}/(*r*_{1}+*g*_{1}),*g*_{1}/(*r*_{1}+*g*_{1})**r*_{2}/(*r*_{2}+*g*_{2}), ...Moreover, only slightly more complex solution exists in case any two periods are either relatively prime, or one divides the other. In each group of periods where one divides the other we'll pick the largest one, and go from left to right which units of time modulo that largest period have already seen red, and the probabilities for different groups can still be simply multiplied since they're independent.

However, we have arbitrary periods up to 100 in this problem, so the above condition does not hold. In order to overcome this, let's split the time into

*m*parts: first part will have units 0,*m*, 2*m*, ..., the second part will have units 1,*m*+1, 2*m*+1, ..., and so on. Each of the parts can be "compressed"*m*times to obtain an instance of the same problem, but a period of*p*gets replaced by a period of*p*/gcd(*p*,*m*).The only remaining task is to find which value of

*m*will guarantee that for any two numbers*p*and*q*up to 100, the numbers*p*'=*p*/gcd(*p*,*m*) and*q*'=*q*/gcd(*p*,*m*) are always relatively prime or one divides the other. We can write a small program for that, and it turns out that*m*can be as small as 2520=2^{3}*3^{2}*5*7.More generally, if the numbers were up to

*x*instead of 100,*m*could be equal to the product of largest powers of primes not exceeding sqrt(*x*). For p' and q' to have a non-trivial greatest common divisor means for them to have two different primes in their factorization, which would mean that original numbers*p*and*q*were a product of two prime powers each exceeding sqrt(*x*), which is a contradiction.We have essentially decomposed the problem

*m*times from the top, and that allowed to split it into independent problems of size at most*x*at the bottom, hence the "meet in the middle" mention above. The running time of this solution is O(*n***m***x*), which is fast enough.The second problem I mentioned came from Codeforces: you have

*n*game units, each capable of causing 1 unit of damage per second. You need to merge them into*m*bigger game units, and if*a*units were merged into the_{i}*i*-th bigger unit, it will cause*a*units of damage per second. The_{i}*m*bigger units will then attack an enemy army also consisting of*m*units, with the*j*-th enemy unit having*b*hit points. The attack will happen second-by-second, and in each second each of your big units attacks one enemy unit. It is allowed for multiple units to attack one enemy unit in one second. What is the fastest way to decrease the hit points of all enemy units to zero or negative? You can choose the way you merge the_{j}*n*units into*m*big units, but you can do it only once, before all attacks.The key idea in solving this problem is to postpone the choice of

*a*. Let's imagine we start with a single big unit of size_{i}*n*. Let it keep attacking the first enemy unit, until at some second its hit points drop to zero or below. If they dropped below zero, it means that we have wasted some attacking power; to avoid that, let us split this big unit into two now, one that can exactly finish the first enemy unit, and the other can now attack the second enemy unit during that second. In the following seconds, both units would keep attacking the second enemy unit, and when its hit points drop below zero, we will split one of our units again to avoid wasting attacking power.We will end up with at most

*m*+1 units after all splitting is done, since we split once per enemy unit, but we can notice that the last split was not necessary, so we can skip doing it and end up with*m*units as required. And we have clearly spent the smallest possible time, since we never wasted a single unit of damage during all seconds except the last one.In some sense, the main obstacle in solving this problem is not realizing that it is actually easy :)

Thanks for reading, and check back for more!

## No comments:

## Post a Comment