开发者

Help to understand Google Code Jam 2011 Candy Splitting problem

I'm participating in google code jam. Before anything I want to say that I don't want anyone to solve me a problem "to win", or something like that. I just want some help to understand a problem I could开发者_运维技巧n't solve in a round that has already FINISHED.

Here is the link of the problem, called Candy Splitting. I won't explain it here because it is nosense, I wont be able to explain it better than google does.

I would like to know some "good" solution to the problem, for example, I've downloaded the first English solution and I've seen the code has only 30 lines!!! Thats amazing! (Anyone can download it so I think there is no problem with saying it: the solution of theycallhimtom from here). I can't understand the solution even watching the code. (My ignorance of Java doesn't help.)

Thanks!


Google themselves provide discussions about the problems and the solution

See this link for the Candy Splitting problem: http://code.google.com/codejam/contest/dashboard?c=975485#s=a&a=2

Basically, the candies can be divided into two equal value piles (from Patrick's point of view) if

C[0] xor C[1] xor C[2] xor ... xor C[N] == 0.

One such split is the sum of all candy values except one. To maximimise the value of one pile, take the lowest value candy and put it in a pile of its own.

Why is it so?

The way I thought about it, is that by definition Patrick's addition is actually equal to xoring values. From the definition of the problem, we want

C[i] xor C[j] xor ... xor C[k] == C[x] xor C[y] xor ... xor C[z]

for some elements on each side.

Adding the RHS to both the LHS and RHS yields

C[i] xor C[j] xor ... xor C[k] xor C[x] xor C[y] xor ... xor C[z] == 0

Since xoring a value with itself gives 0, and the order of xor operations is not important, the RHS becomes 0.

Any of the elements in the LHS can be moved over the to right side and the equality still holds. Picking the lowest value element makes the best split between the piles.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜