Sol1's blog

By Sol1, 23 months ago, In English

Seems like the official editorial has not been released for a long time, I'll try to provide an unofficial one for those who are tired of waiting. But I'm too weak to understand the solution to H by reading others' code :(. If you know the solution, I'll appreciate it if you can explain it to me.

Implementation can be seen in my submissions.

This is the first time I write an English editorial like this. If there's anything inappropriate, kindly point out.

A

Note that you have to move from the bottom to the top no matter what, so we can consider the following problem: Given 2 points $$$A,B$$$ in a $$$w\times d$$$ rectangle, find one path from $$$A$$$ to $$$B$$$ that moves parellel to the edges of the rectangle and touch the edges at least once. After we get the answer to this problem, we can add $$$h$$$ to it to get the answer of the original.

This problem is easy to solve: iterate over which edge to touch, and the distance can be easily calculated.

B

The problem considers sets, thus we can sort the original array.

Observe that: if $$$a_i \geq a_j$$$, and $$$i$$$ goes but $$$j$$$ does not go, then one of $$$i,j$$$ must be sad.

Proof

And we are done: just iterate over all prefixes, check if everyone is happy. Note that we can check the one with the maximum $$$a$$$ among the selected and the one with the minimum $$$a$$$ among the unselected, thus we can do this in $$$O(n)$$$.

C

Note that the character set consists of only 26 characters. We iterate over $$$i$$$ from $$$1$$$ to $$$26$$$, check if $$$n/i$$$ is an integer. If it is, then it is possible to modify the string so it consists of $$$i$$$ distinct characters, each appearing $$$n/i$$$ times.

Now consider the following problem: we have an array $$$s_1,s_2,\cdots,s_n$$$. In one operation, one can arbitrarily choose $$$i,j$$$, and decrease $$$s_i$$$ by 1 and increase $$$s_j$$$ by 1. we want to change it to $$$t_1,t_2,\cdots,t_n$$$.

Solution

Back to the original one: we count the occurrences for all 26 characters $$$c_1,c_2,\cdots,c_{26}$$$. We want to rearrange them in arbitrary order and minimize $$$\sum_{j=1}^{i}|c_j-\frac{n}{i}|+\sum_{j=i+1}^{26}|c_j|$$$. Obviously sorting $$$c$$$ in decreased order will do.

After we iterate over all $$$i$$$ and find the one that gives minimal answer, use the method in the spoiler above to produce a construction. Total complexity is $$$O(n\log n+26n)$$$ for one test case.

D

Obviously the answer can always be $$$1$$$. Thus let's check if it can be $$$\geq 2$$$.

Iterate over $$$i,j$$$ such that $$$a_i<a_j$$$. If we want $$$a_i+X=n^2$$$ and $$$a_j+X=m^2$$$, then we have $$$a_j-a_i=m^2-n^2=(m+n)(m-n)$$$. So that we factorize $$$a_j-a_i=p\times q$$$, if $$$p,q$$$ is of same parity, we have $$$m=(p+q)/2,n=|p-q|/2$$$, thus yielding a possible $$$X=m^2-a_j$$$ (if it is non-negative).

In total, the above process gives $$$O(n^2d(10^9))$$$ possible $$$X$$$s, in which $$$d(10^9)$$$ denotes the the maximum number of divisors one number $$$\leq 10^9$$$ has, and equals to $$$1344$$$. So we can simply calculate the answer for all of them, resulting in the $$$O(n^3d(10^9))$$$ solution which can pass.

E

Observe that the total size of cover will not decrease. We will prove this by construction.

Divide the process into 4 steps:

Step 1

Remove all rectangles that are contained by some other rectangle. This can be done by a trivial greedy.

Step 2

For rectangle $$$i,j$$$, if $$$u_i\neq d_i, u_j=d_j$$$, and $$$l_j<l_i\leq r_i<r_j$$$, shrink the width of $$$i$$$ such that $$$u_i=d_i\neq u_j=d_j$$$. Sort everything by $$$l$$$ and greedy will do.

Step 3

For rectangle $$$i,j$$$, if $$$u_i\neq d_i, u_j=d_j$$$ and they intersect, shrink $$$j$$$ so that they does not intersect but only touch on the edge. Still greedy and sortings.

Step 4

Segment cover for all rectangles.

Total complexity is $$$O(n\log n)$$$.

F

Convert the problem onto the prefix sum array of the bracket sequence as follows:

Problem

Define $$$f_{i,j}$$$ as the probability of the sequence $$$a=[j]$$$ getting operated $$$i$$$ times without blowing up.

After one operation, $$$[j]$$$ changes to $$$[j,j+1,j]$$$ or $$$[j,j-1,j]$$$. Using this to directly calculate $$$f$$$ will result in $$$O(n^4)$$$ solution which can not pass.

To optimize, we try to convolve the 3 arrays one by one. Define $$$g_{i,j}$$$ as the probability of the sequence $$$a=[j,j]$$$ getting operated $$$i$$$ times without blowing up.

Transition as follows:

$$$ g_{i,j}=\sum_{k=0}^i s_{k,i-k}f_{k,j}f_{i-k,j} $$$
$$$ f_{i,j}=\sum_{k=0}^{i-1} t_{k,i-k-1}g_{k,j}(pf_{i-k-1,j+1}+(1-p)f_{i-k-1,j-1}) $$$

where $$$s_{k,i-k}$$$ denotes the probability of operating sequence $$$a=[x,y]$$$ for $$$i$$$ times, $$$k$$$ operations fall to $$$x$$$ side and $$$i-k$$$ operations fall to $$$y$$$ side; $$$t_{k,i-k}$$$ similiarly denotes the probability of operating sequence $$$a=[x_1,x_2,y]$$$ for $$$i$$$ times, $$$k$$$ operations fall to $$$x$$$ side and $$$i-k$$$ operations fall to $$$y$$$ side;

$$$s,t$$$ can be calculated by transitions:

$$$ s_{i,j}=\dfrac{2j-1}{2i+2j}s_{i,j-1}+\dfrac{2i-1}{2i+2j}s_{i-1,j} $$$
$$$ t_{i,j}=\dfrac{2j-1}{2i+2j+1}t_{i,j-1}+\dfrac{2i}{2i+2j+1}t_{i-1,j} $$$

And we have solved it in $$$O(n^3)$$$.

G

Observe that, except for a star with $$$3$$$ leaves, the answer to any tree is less than $$$1$$$. Still, proof by construction.

And thus we are done with the first part, now everything is about constructing the answer.

We will do this in a recursive manner: let white be $$$+1$$$, blue be $$$-1$$$, and $$$F(u,s)$$$ denote the process of coloring the subtree of $$$u$$$ such that its sum is $$$s$$$.

Then it is a lot of casework:

  1. If $$$u$$$ is a leaf, color $$$u$$$ with $$$s$$$.
  2. If the minimum imbalance for the subtree is $$$2$$$, just color it that way.
  3. If $$$u$$$ has two children, consider the minimum imbalance for each subtree.
    1. the two imbalances are $$$(2,2)$$$: Call $$$F(l,-2)$$$ and $$$F(r,2)$$$, and color $$$u$$$ with $$$s$$$.
    2. the two imbalances are $$$(2,1)$$$: Call $$$F(l,2)$$$ and $$$F(r,1)$$$, recolor $$$l$$$ to $$$-1$$$, and if the color of $$$r$$$ is $$$-1$$$, invert all colors in the subtree of $$$r$$$ and color $$$u$$$ with $$$1$$$; otherwise just color $$$u$$$ with $$$-1$$$.
    3. $$$(2,0)$$$: Call $$$F(l,2)$$$ and $$$F(r,0)$$$, if color of $$$r$$$ is $$$1$$$ then invert its subtree, and color $$$u$$$ with $$$s$$$.
    4. $$$(1,1)$$$: Call $$$F(l,s)$$$ and $$$F(r,s)$$$, if color of $$$l$$$ and $$$r$$$ are both $$$-s$$$, then invert the subtree of $$$r$$$ and color $$$u$$$ with $$$s$$$; otherwise just color $$$u$$$ with $$$-s$$$.
    5. $$$(1,0)$$$: Call $$$F(l,1)$$$ and $$$F(r,0)$$$, if color of $$$r$$$ is $$$-1$$$ then invert its subtree, and color $$$u$$$ with $$$-1$$$.
    6. $$$(0,0)$$$: Call $$$F(l,0)$$$ and $$$F(r,0)$$$, if both $$$l$$$ and $$$r$$$ are of color $$$s$$$, invert one of them, and color $$$u$$$ with $$$s$$$.
  4. If $$$u$$$ has one children, denote it as $$$v$$$, and consider the degrees of $$$v$$$.
    1. $$$v$$$ is a leaf: color $$$u$$$ with $$$1$$$ and $$$v$$$ with $$$-1$$$.
    2. $$$v$$$ has one child (denote as $$$w$$$): If the minimum imbalance of $$$w$$$ is 2 then call $$$F(w,2)$$$ and recolor $$$w$$$ to $$$-1$$$; otherwise just call $$$F(w,s)$$$, and color $$$v$$$ the opposite of $$$w$$$ and $$$u$$$ the opposite of $$$v$$$.
    3. $$$v$$$ has two children (denote as $$$w_1$$$ and $$$w_2$$$): more casework, similiar to when we deal with $$$u$$$ having two children. You may refer to my code for this.

Inverting a subtree can be done with a tag, thus the problem is done in $$$O(n)$$$.

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

| Write comment?
»
23 months ago, # |
  Vote: I like it +2 Vote: I do not like it

can anyone explain o(n4) solution for problem F.I don't think I can understand optimized anyway. will try to understand o(n4) or simpler one.

»
23 months ago, # |
  Vote: I like it +9 Vote: I do not like it

It would be better if you've put https://codeforces.net/submissions/Sol1/contest/1782 as the link for submissions.

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

    Thanks, I've updated the blog.

»
23 months ago, # |
  Vote: I like it 0 Vote: I do not like it
what is skipped verdict in codeforces ?
  • »
    »
    23 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    If you submitted multiple solutions for one problem during the contest and passed the pretest, in the system testing, only the last submission will be considered valid, so other submissions will be "skipped". Also if your rating was cancelled since cheating, you submissions will also be slipped.

»
23 months ago, # |
  Vote: I like it +11 Vote: I do not like it

Is there any other simpler solution for F ?.I have seen some members using ncr or any other approach ?

»
23 months ago, # |
  Vote: I like it -56 Vote: I do not like it

down vote me

»
23 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Amazing try ! I Think adding some source codes could help more people out.

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

can someone please suggest me similar questions to Problem C. It would be really helpful . Thanx :)

»
23 months ago, # |
  Vote: I like it +8 Vote: I do not like it

For B, the assumption that for ai <= aj, and i goes, one of i or j will be unhappy is not holding. Suppose ai = 0 and aj = 2. This means ai < aj and since ai went, both of them are happy.

Is there any problem in the above scenario that I am overlooking?

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

    Whoops, my mistake. Fixed it now.

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

plz explain "obviuosly sorting c in deceased order will do" in problem C.

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

    If you are processing the following case:

    The string $$$t$$$ has $$$k$$$ different characters, each having a frequency equal to $$$n/k$$$.

    What are these characters should be?

    To achieve minimum cost we choose the $$$k$$$ characters from $$$s$$$ that have maximum frequencies.

    So for those $$$k$$$ characters, we need their frequency in $$$t$$$ to be $$$n/k$$$ and any other characters should be erased from the string, meaning their frequency must be $$$0$$$.

»
23 months ago, # |
  Vote: I like it +24 Vote: I do not like it

This unofficial tutorial is linked on the mainpage of the contest. lol

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

Orz

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

I haven't understood the proof of problem D why the complexity of problem O(N^2*1444) only if we traverse all the divisior of a[j]-a[i] then it would be O(N^2*sqrt(diff)).

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

    yeah , It is true that you have to run a loop for sqrt(diff), but inside that loop you will only find divisors around 1344.

»
23 months ago, # |
  Vote: I like it +9 Vote: I do not like it

What? An unofficial tutorial is linked by the official page? How lazy they are!

»
23 months ago, # |
  Vote: I like it +8 Vote: I do not like it

A simple solution to problem E:

The solution for height = 1 is trivial. I'll discuss about a similar solution for height = 2.

Step 1 : Solve the trivial problem for the rectangle covers both rows only.

Step 2 : Let's detach the rectangles we chose in step 1 into 2 rectangles in the first and second row.

Step 3 : Solve the trivial problem for each rows.

Step 4 : Merge the rectangle in step 2.

My submission: 189882008