awoo's blog

By awoo, history, 3 years ago, translation, In English

1606A - AB Balance

Idea: BledDest

Tutorial
Solution (adedalic)

1606B - Update Files

Tutorial
Solution (Neon)

1606C - Banknotes

Idea: BledDest

Tutorial
Solution (Neon)

1606D - Red-Blue Matrix

Idea: BledDest

Tutorial
Solution (awoo)

1606E - Arena

Idea: BledDest

Tutorial
Solution (Neon)

1606F - Tree Queries

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +99
  • Vote: I do not like it

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

I didn't sort the array in problem D, but the rest matches with the editorial. No. No. No. NOOOOOOOOOOOOO my ratings :'(

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

For problem E Total permutations possible are x^n. To get a winner I set one maximum value(suppose a) and set rest values less than a so they have a-1 options. So its total permutations will be n.(a-1)^(n-1). Now minimum value of a can be n otherwise all players will be eleminated in the first round. So I get the formula x^n — n.( (n-1)^(n-1) + (n)^(n-1) + (n+1)^(n-1) + ..... + (x-1)^(n-1)) Can anyone tell me what is wrong in this approach it is failing on sample 4.

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

    It isn't always the case that the losing players can have any health that's less than the winning player's health. Take the case n=3, health values are 4,3,3.

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

In editorial of problem E it should be <2 or <=1, when talking about fights which ended.

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

Hi in Problem B, if k is larger than n/2 we can use ceil(log2(n)) to get the answer as at each step we can go up by power of ^2 so I am pretty confident in my solution.

But it fails at test case: 576460752303423489 576460752303423489 (https://codeforces.me/contest/1606/submission/133708639) I just want to know if this is because the precision points are too small to consider? and this is a language issue? but mathematically it is correct. Can you please help me?

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

    Precision error, definitely. In this case 57646075230342348 9 (as long long int) is implicitly converted to 57646075230342348 8 .0000 (as double)

    Codeforces (along with most computers/compilers) uses 64-bit long long int and 64-bit IEEE-754 double. However, IEEE-754 double has only 53-bit significand precision, according to Wikipedia.

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

    I have used a similar approach 133723445
    The constraints of n and k are probably too big for log2() function, Therefore I've used log2l() for extra precision. You can read the documentation about these functions here https://www.cplusplus.com/reference/cmath/log2/

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

      I used the log2l() version and it worked, also there is a similar version for the pow() function powl(). to use the long double. Thanks for the help :)

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

My solution to D was much longer and more complicated, but here is at least a neat-ish observation that it used:

Just looking at the first column, we can be sure that if a solution exists, the row with the max element is red and the row with the min element is blue. This is enough to uniquely identify where the cut should be: going left to right, it is whenever we switch from red > blue to blue > red. Now that we know where the split is, we can simplify the matrix by only keeping the min and max element for each row, on each side of the matrix.

My solution did this, then treated each row as an interval [min, max]. On the left side, any overlapping/touching intervals need to be merged, and in the end we have a list of disjoint ranges, after which, in similar spirit to the editorial solution, we sort then brute force on the number of blue ranges. I then used BSTs on the right side to keep track of whether blue > red, which are quick to update since I've condensed each row into only 2 values.

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

UPDATING FILES :- doubt if (cur < n) ans += (n — cur +k-1) / k; can anyone explain why k-1 is being added . It seems to be confusing . Pls explain

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

    (a+b-1)/b is the same as ceil(a/b) if b is positive

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

Another solution for F with liangjiawen2007.

Considering the value of $$$k$$$, obviously we do at most $$$\frac n k$$$ operations. Thus, we can have a $$$O(n\sqrt n)$$$ solution based on the observation.

When $$$k\le \sqrt n$$$, there are at most $$$\sqrt n$$$ different values of $$$k$$$. We can have such dp as follow: $$$f_{i,j}$$$ for the maximum value you can get for $$$k=j$$$. Transforming is easy, $$$f_{u,i}=\sum \max(1, f_{v,i}-i)$$$ and we can do it in $$$O(n)$$$ for every $$$j$$$.

When $$$k>\sqrt n$$$, we do at most $$$\sqrt n$$$ operations, we can have dp as follow: $$$g_{i,j}$$$ for the maximum sons you can get when you do $$$j$$$ operations. When we merge subtree $$$u$$$ and $$$v$$$, we get $$$g_{u,i+j+1}\leftarrow g_{u,i}+g_{v,j}$$$.

This is a knapsack problem on tree, as $$$j \le \sqrt n$$$, we can use the trick that we only do $$$i\le \min(k,siz_u)$$$ and $$$j\le \min(k,siz_v)$$$ while transforming, then the time complexity will be $$$O(nk)$$$ while $$$k=\sqrt n$$$

The total time and space complexity is $$$O(n\sqrt n)$$$

Here is the submission 134047234

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

    Could you elaborate on why the complexity is $$$ O(n \sqrt{n}) $$$? I find it a bit intuitive but I couldn't prove it

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

      Let me try to explain what I've got from the source code:

        for (auto t : adj[now]) {
          if (t == fa) continue;
          dfs2(t, now);
          for (int i = 0; i <= sz[now] && i <= B; i++) {
            for (int j = 0; j <= sz[t] && j + i <= B; j++) {
              g[i + j] = max(g[i + j], f[now][i] + 1);
              g[i + j + 1] = max(g[i + j + 1], f[now][i] + f[t][j]);
            }
          }
          sz[now] += sz[t];
          for (int i = 0; i <= sz[now] && i <= B; i++) {
            f[now][i] = g[i];
            g[i] = -0x3f3f3f3f;
          }
        }
      

      Two nested loops inside the DFS is what seems to be dangerous here. Let B = sqrt(n).

      There are up to B subtrees for which sz[x] > B, so even if sz[t] and sz[now] are greater than B there are up to B such occasions. So if sz[t] > B then we spend up to $$$O(B) * O(B * B)$$$ for such cases.

      On the other hand, there could be up to n "small subtrees" for which sz[t] < B. For such a subtree one can check that the complexity of the whole run $$$Dfs(t)$$$ is indeed $$$O(size^2)$$$. Let us denote by $$$n_b$$$ the number of subtrees of size $$$b < B$$$. Then $$$\sum_{b = 1}^{B} n_b b \leq n$$$ so $$$O(\sum_{b = 1}^{B} n_b b^2) = O(B \sum_{b = 1}^{B} n_b b) = O(Bn)$$$.

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

can someone explain the output for n = 3 and x = 3 of problem E ?

  • »
    »
    22 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The different ways are: {1, 3, 3}, {3, 3, 1}, {3, 1, 3}, {2, 3, 3}, {3, 3, 2}, {3, 2, 3}, {1, 2, 2}, {2, 2, 1}, {2, 1, 2}, {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {1, 1, 2}, {1, 2, 1}, and {2, 1, 1}.

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

UPDATE It's my fault, I misunderstand that it will compare only the column. Please give me an apologize.

In problem D, I found that there is a test case ~~~~~ 1 3 3 7 9 8 4 8 14 15 9 13 ~~~~~

The solution gives the answer as NO.

But I think it is possible for YES by painting to be RBR with k = 2.

Please correct me if I'm wrong. Thank you :)

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

F has a much simpler solution based on the fact that we don't need to store DP values for many different pairs of $$$(x, k)$$$, for at most $$$O(n \log n)$$$ of them it will be greater than $$$|children(x)|$$$, leading to simple $$$O(n \log n)$$$ DP.

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

    Could you elaborate on that? I can't understand two things:

    • why it is the case that only $$$O(n \log n)$$$ vertices will have more than $$$|children(x)|$$$

    • let $$$v_{1}, v_{2}, ... , v_{n \log n}$$$ be those vertices. Why can you iterate through $$$\sum dp[v_{i}].size() $$$ without exceding the time limit?

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

      There are just $$$n$$$ vertices, not $$$n \log n$$$. Let $$$dp[u][k]$$$ be the answer of query $$$(u, k)$$$, there are only $$$O(n \log n)$$$ pairs of $$$(u, k)$$$ that $$$dp[u][k] \gt |children(u)|$$$.

      To prove it, for a fixed $$$k$$$, let's construct a tree that maximize the number of $$$u$$$.

      For a certain tree, let the lowest vertice that statisfies the condition above be $$$v$$$. $$$v$$$ must have at least one child $$$w$$$ that $$$|children(w)| \gt k + 1$$$. To maximize the number of $$$u$$$, there should be only one $$$w$$$. (If there are more than one child meet the requirement(call them $$$w_i$$$), we can leave only one child $$$w_1$$$, add other $$$w_i$$$ and $$$children(w_i)$$$ to $$$children(w_1)$$$. After that $$$dp[v][k]$$$ will only increase, which leads to number of $$$u$$$ increase.) Also, obviously all the children $$$w$$$ have should be leaves.

      Then we consider $$$p$$$, the parent of $$$v$$$. We can also take all the children of $$$p$$$ except $$$v$$$, insert them to $$$children(w)$$$. It will not decrease the result. Repeat this process, we can see this tree is a chain with all leaves linked to one end.

      For this tree, we can easily caculate that $$$dp[v][k] = |leaves|-k$$$, $$$dp[parent(v)][k] = |leaves|-2k, ...$$$ So the number of $$$u$$$ that $$$dp[u][k] \gt |children(u)| = 1$$$ will not exceed $$$\frac{n}{k}$$$. Use the sum of harmonic series we can prove there are only $$$O(n \log n)$$$ pairs.

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

can someone explain problem E ? For n = 3 and x = 3 I can only think of 12 cases and not 15 -

  1. 1 1 1

  2. 2 2 1

  3. 2 1 2

  4. 1 2 2

  5. 2 2 2

  6. 3 3 1

  7. 3 3 2

  8. 3 1 3

  9. 3 2 3

  10. 1 3 3

  11. 2 3 3

  12. 3 3 3

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

    2 1 1

    1 2 1

    1 1 2

    In these scenarios, everyone dies after first the round.

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

Can anyone please tell what's the intuition behind taking such states in E-Arena.

Spoiler
»
18 months ago, # |
  Vote: I like it 0 Vote: I do not like it

In the solution for E-Arena given in the editorial, I didn't understand why dp[n][0] = 1;

which is that case where all n heroes are alive and each of them has dealt 0 damage?

  • »
    »
    16 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    dp[i][j] actually stores the number of ways to choose the initial health and positions of the n-i heroes who have died by now.

    Why is dp[n][0] = 1? before the first round, there are n heroes alive, 0 damage has been dealt and there are 0 heroes dead. Number of ways to chose the health and positions of 0 dead heroes is 1.