mon0pole's blog

By mon0pole, 3 years ago, In English

As I didn't find kostka's blog for this round, So posting this blog
Round Link
I was only able to solve first problem
Please share your approach for problem 2 and problem 3

  • Vote: I like it
  • +35
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
Rev. 3   Vote: I like it +10 Vote: I do not like it

Problem 2: first 30 numbers are $$$2^0$$$ to $$$2^{29}$$$, then 70 random trash numbers (I used $$$10^6$$$ to $$$10^6 + 69$$$ and added all those as the 70th number.)

When the judge gives you the B array, you try to minimize the difference between the two subsets that add to B (i.e. ignoring A) You do this by maintaining the difference of the two subset sums, and if the first sum is larger then insert the current element to the second; else insert into first. You can keep the difference under $$$\operatorname{max}(B_i)$$$.

Then you just use the $$$2^0$$$ to $$$2^{29}$$$ to make up for the difference (grade school math). Finally output the smaller-sum subset with the adjustments, and also output the sum of the first 69 trash numbers (i.e. the 70th trash). You're done!

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    elaborating on the last sentence: you need to find the sum that you are looking for, then greedily add numbers from array B until you are less than $$$10^9$$$ from the target sum. Say that you are $$$k$$$ away from the target

    After that, you can use the binary representation of $$$k$$$ to write it as a sum of powers $$$2^{0},2^{1},... 2^{29}$$$, obtaining the desired sum.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Well I accidentally pressed 'Post' instead of 'Preview', but thanks for the elaboration anyway!

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      This explanation is much more comprehensive and briefer than the official tutorial. Thanks.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Hello, I have a question regarding the unused powers of 2. I get that you use the powers of 2 to make up the difference, but then what about the unused powers? How will you distribute them into the two subsets? Thank you!

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Perhaps a better way to understand the problem is as follows: let $$$s$$$ be the sum of all elements in $$${A}$$$ and $$${B}$$$ combined, which is even. Then we need to find a subset of $$$A\cup B$$$ whose elements add up to $$$s/2$$$.

        Once we find which elements of $$$A\cup B$$$ will be used in one of the sets, the unused ones will simply belong in the other set.

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
            Vote: I like it +8 Vote: I do not like it

          Thank you very much for answering, but that still doesn't tell me how the elements are chosen to be in which subset. Let's say for the elements corresponding to powers of 2.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you share your thought process on how you came up with the first 30 numbers being powers of two?

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Well, I probably wanted the A array to be more dynamic and cover a wider range of numbers. Also I wanted to let the A array cover a list of adjacent integers. So I came up with the binary representation.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

    Same, but I didn't handle difference between two subsets. If sum of all numbers is $$$S$$$ then we just need to get $$$\frac{S}{2}$$$ sum in first subset. We can get any number in range $$$[0,2^{30} - 1]$$$ using powers of two, so just take some numbers from trash and given array while desired sum is bigger than $$$2^{30} - 1$$$.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I'm not getting how the powers of 2, which are not used to make up the difference are distributed among sets??

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Split the powers of two into two sets which difference is the required difference.

»
3 years ago, # |
  Vote: I like it -12 Vote: I do not like it

Who else spent 1 hour on B and then realized $$$N$$$ is constant?

And why wouldn't they emphasize such a thing since the sample contains $$$N= 3$$$ !!!

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    The same happened with me too. I was wondering how can we generate a valid sequence A for every possible sequence of B using only 3 integers.

»
3 years ago, # |
  Vote: I like it +61 Vote: I do not like it

Problem C is range DP. Let $$$dp_{ij}$$$ be the answer to the given problem if we only considered exercises $$$i$$$ through $$$j$$$. Then, $$$dp_{ii}$$$ is simply twice the number of weights needed for exercise $$$i$$$. We can compute $$$dp_{ij}$$$ for $$$i < j$$$ by first placing all weights that are included in all exercises $$$i$$$ through $$$j$$$ (let there be $$$N$$$ such weights), and then iterating over the next exercise $$$k$$$ after which only these $$$N$$$ weights remain (it can be shown that such a $$$k$$$ does exist, satisfying $$$i \leq k < j$$$: try to prove it!). Then, we have $$$dp_{ij} = \min_k dp_{ik} + dp_{(k+1)j} - 2N$$$, subtracting $$$2N$$$ because we do not need to remove these $$$N$$$ weights after exercise $$$k$$$ or add them before exercise $$$k+1$$$. Our answer is then $$$dp_{1E}.$$$

We can precompute the values of $$$N$$$ for each interval in $$$O(E^2 W)$$$ time, and we can then handle our DP in $$$O(E^3)$$$ time, giving an $$$O(E^2(E+W))$$$ solution, sufficient to pass easily.

A screencast of my third-place finish will be uploaded to my YouTube channel tomorrow.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    I suggest that N be modified to C(i,j) for a clearer explanation.

»
3 years ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

For problem 3 I managed to pass test 1 using BFS, the node is a state composed of the current exercise I want to do and a string representing the current weights on the machine.

Edges go out like this:

  • With the current set of weights do as many exercises as you can (1 edge)

  • Remove the topmost weight if the string isn't empty (1 edge)

  • Add a new weight (at most 3 edges)

»
3 years ago, # |
  Vote: I like it +18 Vote: I do not like it

I wonder if anyone solved a problem using Punched Card Python...

»
3 years ago, # |
  Vote: I like it +11 Vote: I do not like it

Although I get a perfect score in the end, I am still astonished by the speed of the top contestants. How did they finish the contest in just 20 minutes? 20 minutes is just enough for me to finish first problem.

Also, I struggle for the third problem for almost 2 hours. I tried greedy at first, failed and failed again, struggle for an hour, then I found I need divide and conquer with memorization. I can imagine that if first two problems have more difficulty, I will not able to finish third problem. I am wondering if I can pass round 2.

My suggestion for Code jam is that, if you are under 2100, never try the large test set directly except for the very easy problem. Try to brute force and pass small test set first, then you can use your brute force code to get the correct answer for arbitrary small input, and this can help you examine your formal code to avoid unnecessary punishment.

Another suggestion is, learn how to use the test tools to test your code quickly in interactive problems, it is really helpful and can save you a lot of time in the contest. Download both local testing tools and interactive runner, and read the comments of interactive runner carefully, you will now how to test your code.

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

In problem 2, I reached the observation of powers of 2 quickly. But my main goal was to use the binary representation of the difference and use powers of 2 accordingly, but was stuck at the unused powers (they cannot be known in advance). I eventually found a bit complicated way to accommodate for this but unfortunately ran out of time.

Later, I knew from the editorial how this can be simplified using the observation that any odd number can be constructed using all powers of 2 (by putting some powers with positive signs and others with negative signs), e.g., 0 0 0 0 1 0 0 1 is equivalent to +1 -1 -1 -1 -1 +1 -1 -1.

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Orz mon0pole :)

»
3 years ago, # |
Rev. 2   Vote: I like it +33 Vote: I do not like it

Solved B using Fibonacci numbers instead of powers of 2 ¯\_(ツ)_/¯

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you elaborate?

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Zeckendorf's theorem states that "every positive integer can be represented uniquely as the sum of one or more distinct Fibonacci numbers in such a way that the sum does not include any two consecutive Fibonacci numbers" (Wikipedia article). Once you know this, it is easy to see that the approach is also valid.