Блог пользователя Kaleab_Asfaw

Автор Kaleab_Asfaw, история, 4 года назад, По-английски

Hi!

Solution Discussion for HHKB Programming Contest 2020

I created this blog, because I couldn't find an announcement in Codeforces (where usually there would be) and want to have a discussion about the solutions to the problems after the contest ended.

The contest is being held on Atcoder. Link to the problems in the contest: https://atcoder.jp/contests/hhkb2020/tasks.

Hope this will help people who didn't get a solution for problems.

Sorry for the bad English.

  • Проголосовать: нравится
  • +31
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится
»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Summary for participant like noob like me : Solve ABC fast and take rest the remain time !!

How to solve D and E ???

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    A hint for E — For each tile, consider how many configurations will light it up

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Do you mean calculating the number of tiles in each direction that will light up? Or did I made a mistake in understanding.

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        I mean, for a given tidy tile, I want to find out, in how many configurations this is lit up? For instance, in the second sample, there are 12 configurations that light up the top left of the grid (I'll leave it to you to figure out why). If you do that for every single tile on the grid and sum them up, you will get the answer.

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

how to solve E ?

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится +6 Проголосовать: не нравится

    For each tidy cell, find from how many tidy cells it can be lit. It can be lit up by continous line tidy cells in all 4 directions. Lets say the count of such cells is lit and total number of tidy cells is tidy.

    Then that particular tidy cell, can be lit up in2 ^ tidy - 2 ^ (tidy - lit) combinations.

    Submission

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

IDK why I got wrong answer in F. I derived the result using integration. Like for each continuous interval having bounds as discrete points, I do integral of V dP. Any ideas?

V : Value assumed by the maximum P : Probability function for that range for all the overlapping intervals

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    how did you found expectation, i was getting 117 as my answer for second sample. How should one calculate expectations in these type of problems??

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      I just noticed that in a bounded range, say for Value V, my change in probability function is contributed by the value that I just assumed.

»
4 года назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

How to solve D?

»
4 года назад, # |
  Проголосовать: нравится +14 Проголосовать: не нравится

how to solve D?

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

How do you go about doing F? The way I tried is $$$P({x_1,x_2...} < z) = \prod \frac{z-l_i}{r_i-l_i}$$$ and the denominator removes the rightmost term of our answer. But the integration yields a polynomial I can't calculate (and I somehow have to handle that the probability maxes out at 1). If this is the right direction, how do you proceed from here?

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    To overcome this, I thought about fixing Li's but idk if this is right

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Can you tell if this is true,

    Expected value when there are N ranges, all having same range[L,R] is : L + (R-L)*(N/(N+1))

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +8 Проголосовать: не нравится

      I think that's true if all assume same probability function?

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Can we do it like this then ? (just assuming R_min >= L_max for ease)

        sort all the pairs by R[i].

        consider these N ranges now :

        L_max to R[0] R[1] to R[2] R[2] to R[3] ... R[N-2] to R[N-1]

        For each range, we find contribution of that range in the expected value, where we consider (N — i) i.i.d. RVs.

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Yes this is the right direction, I was not getting correct but to multiply this you need to do FFT(n*log^2(n)). This gives you a polynomial of the form

    $$$a_0 + a_1*z+a_2*z^2....+a_n*z^n$$$

    . Then you need to differentiate this, multiply by z and then integrate it in suitable range. I will try to get AC and then share the code. Hoping this helps.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      so what should be the range of integration? min(Li) to max(Ri) ryt??

      • »
        »
        »
        »
        4 года назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        No, consider the ranges [1, 3] and [3, 5]. Note that [1, 3] is of no use.So here we use the range [3, 5] only. But now consider the ranges:[1, 4], [2, 6], [3, 9]. You can't simply consider the range [3, 9] since in [5, 9], the first range has no contribution. So you need to split between points:

        $$$L_{max}, R_1, R_2,....R_{max}$$$

        and delete ranges which are of no use(division of polynomials).

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится

    Sort all L and R to find the "fundamental intervals" [fL, fR]. We'll assume the maximum is on each of these interval and calculate the expected values.

    Fix an interval [fL, fR].

    If fR <= L_i for some i, there's 0 probability that the max lies in the interval [fL, fR], so just skip.

    Otherwise, let A_k be the probability that k of these variables lies in the interval [fL, fR]. The generating function F(X)=A_0 + A_1 X + A_2 X^2... can be found by multiplying following polynomials for each variable i.

    If fL >= R_i, simply multiply the constant polynomial 1 to the polynomial. Otherwise, multiply (fL - L_i) / (R_i - L_i) + (fR - fL) / (R_i - L_i) X to the polynomial.

    After we've obtained the generating function, we can recover the answer using the fact that if we have N random variables with uniform distribution on [L, R], the expected maximum value is (L + R * N) / (N + 1).

    Code

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +6 Проголосовать: не нравится

      Thanks! I have some reading to do to understand how all this works

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +7 Проголосовать: не нравится

      You don't need that fact. Let $$$P(x)$$$ be the probability of $$$x$$$. We need to find

      $$$\displaystyle\int_{0}^{\max(R)} xP(x) dx$$$

      This can be rewritten as

      $$$\displaystyle\int_{0}^{\max(R)} P(>x)dx = \int_{0}^{\max(R)} 1-P(<x)dx $$$

      $P(<x)$ is the same. We have $$$\displaystyle\prod_{x \le R_i} \dfrac{x - L_i}{R_i - L_i}$$$. We know that the polynomial changes when $$$x = R_i$$$. Now we iterate in decreasing order of $$$R$$$, as in sort the ranges in decreasing order of $$$R$$$. Then we get

      $$$\displaystyle\sum_{R_{i+1} \ge max(L_i)}\int_{R_{i+1}}^{R_i} 1 - \prod_{j \le i} \dfrac{x - L_j}{R_j - L_j}$$$. There is still some part missing, which is till $$$max(L)$$$. That can be added seperately, and we get $$$O(n^2)$$$.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      I'm confused about the time complexity. It seems that it's $$$O(N^3)$$$?

»
4 года назад, # |
  Проголосовать: нравится +12 Проголосовать: не нравится

How to solve D? I tried to solving it by finding in how many cases squares will overlap and subtract that from (n - a + 1) * (n - a + 1) * (n - b + 1) * (n - b + 1). But finding formula was too hard for me. :(

»
4 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

when you get AC on E 5 sec after contest because your hands were shaking too much while submitting lol.

»
4 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

Can anyone share the approach for solving D and E.

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

for C, we can use DSU

source code

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Ok, but a simple idea is to insert first 200005 numbers in a set and for each 'i', remove the element from the set and output the smallest element.

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 2   Проголосовать: нравится +4 Проголосовать: не нравится

      Or you can mark visited elements in a vector and increment the index until the next unvisited using the fact that the answer is always a non-decreasing sequence

»
4 года назад, # |
Rev. 2   Проголосовать: нравится -8 Проголосовать: не нравится

for D, we use exclusive.

the total number can be easily given by sqr(n-a+1) * sqr(n-b+1), then we subtract the number in three different overlapping cases.

source code

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

for E, it's also exclusive

source code

»
4 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

m(at)ths(coder)

»
4 года назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

Can Anyone Share the Logic/Hint (Not the Code) to Approach D and E?

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +11 Проголосовать: не нравится

    E was easier than D, at least to me. A quick explaination Let's try to find for each '.', in how many cases it will contribute.

    Spoiler

    We can also say, left+right and up+down information is also good enough, we don't want seperate left, right and so on. How do we find this information for each '.'

    Spoiler

    For right most elements or element left to '#' we have correct information, but for rest, we need to do something.

    Spoiler

    Repeat same for top-bottom. How to use the information we have stored ?

    Spoiler
    Spoiler

    Take sum over all '.' . That's your answer

»
4 года назад, # |
  Проголосовать: нравится +35 Проголосовать: не нравится

Here's one of the solution to D.

Let us find the number of ways such that there is no overlap in x-axis. Let us put A to the left and B to the right, such that on x-axis, now we have N-(A+B) blank space. Can you guess the number of ways to distribute these blank spaces between A and B ?

Spoiler

Now that x-axis is covered, what about y-axis ?

Spoiler

We fixed left and right for A and B, but it can be other way too. So multiply above calculation with 2.

We can do a similar thing in Y-axis, and since everything remains same, we can multiply the above calculation with 2

Are we repeating any cases ?

Spoiler
Spoiler

We have actually done the hard work already, the cases are

Spoiler

Last up is, how to find number of integral solutions to a+b+c = n

Spoiler

To wrap up

 p = N - (A + B)
 res = C(p+2,2) * (N-A+1) * (N-B+1)
 res = res*2 
 res = res*2 
 
 res2 = C(p+2,2)*C(p+2,2)*4

 answer = res - res2
  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Where there is no overlap in x-axis and y-axis. Can you please elaborate ? Thank you.

    Edit: I figured it out.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      Could you please tell me how to sovle your problem ? thanks.

      • »
        »
        »
        »
        4 года назад, # ^ |
        Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

        This might help...

        Now we multiply by 2 because we can flip the image and exchange the positions of A and B.

        Now again multiply by 2 because we can rotate the image by 90 degrees for a new configuration.

        Now there are cases when we are overcounting. Example: when A is on top right of B, now this is already counted again in a case when the image is rotated. So we need to subtract these kind of cases.

        How to find those cases ? It is explained in the OP's comment.

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        go out to practice

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +11 Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

Can someone please explain how to solve D?

Seems like a straight forward solution in which we subtract the overlapping cases. But how does it work, I am not able to wrap my head around it? Can someone please explain?

Submission link : https://atcoder.jp/contests/hhkb2020/submissions/17295052

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Okay, I think I can explain... One thing to notice that if two square overlaps then there length and breadth of one rectangle must cross the breadth and length of the other rectangle so we need to discard those and we should count only the where the dimension don't overlap as mentioned i.e solution of the integral equation above which is shows the conditions of non-overlapping squares and subtract those which were counted twice due to rotations if u need further explanation u can ask

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

      Thank you. I understand the integral equation above which calculates the conditions of non-overlapping squares. Can you please elaborate how do we calculate cases that were double counted (as in what to subtract) and how it works? (As in how do we account for repeating cases?)