ibrahim_habib's blog

By ibrahim_habib, history, 5 years ago, In English

Hello everyone,

Soon starts round 1C of Google Codejam. It's the last subround of round 1 and the last chance for people qualified from qualification round but haven't qualified from round 1A or 1B. To qualify one has to be in the top 1500 participants in this subround.

Let's discuss the problems here (after the round is finished, of course !).

I wish you good luck in this round.

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

| Write comment?
»
5 years ago, # |
  Vote: I like it +14 Vote: I do not like it

Can we submit solutions in 1C even if we've already made the top 1500 in another round? I wanted to participate for fun.

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

For B I assigned letters according to frequence of the first character of R[i].
In other words, d[i] (1 <= i <=9) is equal to the most frequent R[j][0] among all R[j].
Can anybody explain why it's correct?

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

    It's more probable to have 1 as a first character then any other. Why? You have two randomizations — one randomizes upper_bound, the second randomizes number itself. Let's check just the digits. You can see that number 1 can be achieved for every upper_bound, and number 2 cannot be achieved for upper_bound equal to 1. You can generalize it even further, I don't have a formal proof.

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

    I did exactly the same but I have no idea why my solution got WA on second test set :( can anyone help me find my bug please?

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

      Try changing int M to long long M. It's strange but helped me. I just described this case here

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

        WOW.

        I got RE on test 2, and was debugging it for at least 20 minutes. Then I gave up. Had no idea what the problem could be. It's sad to hear that changing int skip to ll skip fixes the problem.

        My code
      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +5 Vote: I do not like it

        @Mindstorm Thanks, it worked :'( :'(

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

What was the intended solution for C?

I only managed the first two testcases, my solution was O(n^2 * log(n) * d), which TLEd on bigger constraints.

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

    Me too, but I think O(n * log(n) * d * d) is possible:

    Store all a[i] in a map as reduced fractions — (a[i]/1) pairs.

    Try each unique slice (s) D times — from (a[i]/1) to (a[i]/D):

    • Try all multiples of s from 1 to D O(D*log(N)) in increasing orders as we benefit from fewer slices to get s value.
    • If you need additional slices try to get them from the next D biggest that are not multiples. O(D+log(N) which is smaller than above)
  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    There are nd possible slice sizes, as you've noticed for the n^2d solution. The trick for nd^2 is to save, for each slice size, a list of the original pancakes that can create that size of slice. This can all be done in nd time plus some sorting. To test whether k cuts are possible, we only have to consider slice sizes with at least d-k pancakes in the corresponding lists. While these lists may be large, we never have to consider more than the k smallest pancakes in each, hence the runtime.

  • »
    »
    5 years ago, # ^ |
    Rev. 4   Vote: I like it +1 Vote: I do not like it

    I solved post-contest in $$$O(nd \log nd)$$$

    There are two things you want to know about each of the $$$O(nd)$$$ possible slice sizes: first is the maximum number of cuts you can "save" by choosing that size, second is whether it is possible to even make enough slices of that size. It's easy to obtain these properties in $$$O(n)$$$ per slice, but we can do better.

    For the first subproblem, pre-sort $$$A$$$. When generating the slice-size $$$\dfrac {A_i} k$$$, you already know $$$A_i$$$ is evenly divisible by it, so you save a cut as long as the number of slices for that size hasn't exceeded $$$d$$$ yet. A couple of hashmaps will store these statistics fine — be sure to reduce each fraction to lowest terms via the GCD before using them as keys.

    For the second subproblem, collect all generated slice-sizes, sort them (recalling how to compare fractions), then binary search for the largest slice-size that can make enough slices.

»
5 years ago, # |
Rev. 5   Vote: I like it 0 Vote: I do not like it

In problem B suppose we get input :

1
5 a
5 a
5 a
(repeated 1e4 times)

How to find the string corresponding to 0123456789 ? Since inputs are random , why above test case is wrong ? If it's correct then how can we find the string ?

Then it seems that is can have multiple answers .But was that mentioned in question ?

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

    Input is randomly chosen, it's not hand-made

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

      I have written in comment that "inputs are random" so above is possible .above input can be one of the random inputs . possibility is very less but then also it can be one of the random of inputs .

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

        you have to choose uniformly across all valid possibilities

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

          It was mentioned that probability of choosing is "uniform and random" but that does not say that above test case cannot happen . Suppose i generate uniformly between $$$1$$$ and $$$100$$$ , for $$$100$$$ times , ideally it should give $$$100$$$ different numbers , but possibility that same number occurs all $$$100$$$ times is NOT ZERO.

          • »
            »
            »
            »
            »
            »
            5 years ago, # ^ |
            Rev. 3   Vote: I like it +18 Vote: I do not like it

            The probability that all the numbers are same in the case you have given is $$$1/100^{100}$$$. Mathematically speaking, you will have to run the given computation at least $$$100^{100}$$$ times to get a high chance of getting all the numbers to be same.

            Practically, what is to be expected: that the $$$1/100^{100}$$$ case somehow occurred and even the tester's solution failed, or that we got an appropriate test case?

            The chances of the former happening is 1 in a Googol Squared, so I'll let you decide for yourself.

            • »
              »
              »
              »
              »
              »
              »
              5 years ago, # ^ |
                Vote: I like it -12 Vote: I do not like it

              "chances of the former happening is 1 in a Googol Squared" sure .thus it can occur single time in first place itself in Googol Squared computations .

              They should have mentioned this in the problem that such type of test cases won't occur .But then they can't say this because it would be opposite to uniform and random .

              I finally understood during contest that such type of test cases won't occur (Test case 3 tells this) but i was confused for half hours related to this.

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

                You need to realize that often in CP you don't have to solve given tasks for all possible inputs, you only need to pass the tests that will be given to your program. The test case is not formally wrong (it satisfies all constraints in the statement), there even exists a unique correct output for it (the string D that was generated on the server). You just have no way of figuring what D was based on that particular test input.

                In fact, every valid string D could be an answer for every test case. It could happen with non-zero probability that the first generated digit was nearly always nine, yet model solution would guess that corresponding letter was one.

                But again, your goal is to pass the test cases fed to your program: if you (in any task, not just this one) send a solution that will produce output that's judged as correct with probability close to one, you effectively solved the problem (in CF there are hacks as well that one has to care about though).

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

        Calculate the probability of that happening. It will be tending to 0.

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

          That is what i said in my previous comment . But it's not zero so it can happen .

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

            We have randomised algorithms which work on the basis of probability of an event being close to 0, but not exactly zero. Check out this problem for example https://codeforces.me/contest/364/problem/D

            • »
              »
              »
              »
              »
              »
              »
              5 years ago, # ^ |
              Rev. 2   Vote: I like it -14 Vote: I do not like it

              Problem did not mentioned that algorithm they are using will avoid test cases of above type . They said it's uniform and random and thus above can happen .That's basic mathematics. Also if algorithm they use avoid test case of above type then how it's random ?

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

        Yes. You may get 664 heads in a row when tossing a coin.

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

I did following on B: character for $$$0$$$ is easy to find, after this considered all the permutations of the letters ($$$9!$$$ permutations) and found the mean of all the numbers represented by such a mapping, the one which is closest to $$$\frac{10^u}{4}$$$ is the answer. But got WA on Test 3. Why is it wrong?

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

    Maybe overflow when you calculate the mean. I think it can happen that the sum of all numbers with an incorrect digit map can exceed 2^64 (I've tried the same solution at first, but it was to slow... Ho did you implement permutations? I've used next_permutation from STL)

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

      I took care of overflow, and verified the same there wasn't any overflow. The overall complexity of my code was $$$O(9! \cdot 9)$$$ And yes I used next_permutation.

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

        you are calculating mean of (9!) permutations in O(1). how?

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

    I tried testing it locally, even with number of queries upto $$$10^6$$$ the probability of this working is only $$$90\%$$$

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

Does anyone have an idea of the average increase in ranking for a person who is ranked approximately 1500 after they review the round and disqualify cheaters and such? I am currently ranked 1514 and I want to sleep at night with an approximation of my chances of being bumped over the edge!

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

I got WA on test 2 in problem B. Changed unsigned int x to long long x and got AC. The only thing I do with variable x is read it from the input. AFAIK overflow with unsigned int in is not UB. Has anyone an idea what is the problem?

My code
  • »
    »
    5 years ago, # ^ |
      Vote: I like it +14 Vote: I do not like it

    You are reading a number from the input that has the length bigger then the accepted length of the maximum value of a unsigned int. I suppose that the reading truncates and some part is left over and this messes up the rest of your reading. I am not sure what the name of this behaviour is, but I dont think it is overflow.

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

      Yea, I tried with small example and this is probably the reason. You can't read arbitrarily big number from the input and compress it to uint. It will mess up memory. Thanks

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it -10 Vote: I do not like it

      This is exactly the reason, why my solution failed Tests 2 & 3 of Problem B.

      No wonder Google got rid of their motto 'Don't be evil!'...

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

Failed this round because I read Q in problem B into int, as in subtask 3 it is always -1. Found out the issue after contest ended :(

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

My solution for B, however, it fails on Test 3 if someone can provide a countercase.

For each test case, I go through all the queries and get the set of distinct alphabets that occurred. Now for a server, I require 10 distinct alphabets if I somehow have less than 10 I add up some characters from my side. Now I go through each permutation of this set and check if it is a valid digit string or not if yes then output it as the answer.

code
»
5 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Posting this here because there was a question based on this today
I read uniformly randomly selected and the next thought that comes to my mind is I won't be able to solve this.
It would be really helpful if someone can provide some resource or anything on how to approach these questions.

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

    High level approach: solve it the same as any other problem, except that when you want to discard any idea because it is not going to solve some of the tests, you should first think about what are the properties of the tests that break your idea, and how likely are they to be generated randomly.

    You may look around and find some problems of this kind for practice, so that you'll learn the most common observations about random tests (e.g. "randomly generated permutation will have relatively short LIS" or "randomly generated tree will have relatively small depth").

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

      Okay, I'll do that. Thank you.

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

      By "randomly generated permutation will have relatively short LIS", did you mean, that if we generate a large number of completely random sequences, the frequency of LIS = 1 will be maximum? As this sounds weird to me.

      Edit: Please ignore this comment, I got your point. I was thinking something strange. Sorry.

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

My solution for B failed only in test case 3. I made a function for each test case separately. For testcase 3 function is solve3. Here is link to my solution. Appreciate any help.

Update: Got it why my answer is wrong.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone explain the solution for B ?

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

    TLDR: If all is really random the frequency of first digit being 1 is bigger than first digit being 2 which is bigger than first digit being 3 ... first digit being 0 has probability of 0 on the other hand.

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

ооо хабиб !!!! когда бой с магкрекором ?

eng edit: ooo habib !!!! whene fight with magcrecor ?

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Any solution for the A? I don't understand why I got WA

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

    My solution for the first problem was as the following:

    Keep track of the xy coordinates of Peppurr's after each move. If the Manhattan distance between the xy coordinates and the origin became less than or equal to the move count then at this moment they will meet. If the distance is always greater than the move count then it's impossible.

    For example if we considered the first sample case:

    4 4 SSSS

    After the first move the distance = |4| + |3| = 7 > 1 (can't meet)

    After the second move the distance = |4| + |2| = 6 > 2 (can't meet)

    After the third move the distance = |4| + |1| = 5 > 3 (can't meet)

    After the fourth move the distance = |4| + |0| = 4 = 4 (can meet)

    Then the solution equals to 4

    Here is my code for this problem. This is the only problem I could solve completely correctly in this round :"D

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I am trying to solve B for Test Set 2 but it failed with WA however it passes Test Set 1. It seems I do the same what is written in the analysis but it keeps failing. Can anybody help me to find mistakes in my solution?

Solution
  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    M and many other things need to be long long

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

      That was the problem! It works now. Thanks a lot!

      I mistakenly thought that the upper bound for M is 2^U but not 10^U. A silly mistake :(

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone explain Problem C, (Oversized Pancakes). If you could attach the source code,it would be great.