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

Автор Serval, 7 лет назад, По-английски

1153A - Serval and Bus

Author: Serval, preparation: bzh

Editorial

1153B - Serval and Toy Bricks

Author: Serval, Preparation: bzh

Editorial

1153C - Serval and Parenthesis Sequence

Author & preparation: Serval

Editorial

1153D - Serval and Rooted Tree

Author & preparation: bzh

Editorial
Code
Another Solution
Code for Another Solution

1153E - Serval and Snake

Authors & preparation: Serval, bzh

Editorial
Bonus: How to save more queries?

1153F - Serval and Bonus Problem

Author: Serval, preparation: Serval, bzh

Editorial
Code

UPD: We fixed some mistakes and added another solution for D.

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

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

Aim of Q.C is just to make subset x = s[1...[s]-2] a perfect parenthesis sequence, with s[0] = '(' and s[|s|-1] = ')'. This is because if x is balanced, then all strict prefixes are gauranteed unbalanced, but still s[0]+x+s[n-1] will become balanced.

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

Thanks for the fast editorial!

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

Can someone explain D — Serval and Rooted Tree more clear, sorry for my stupidness

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

    For each non leaf node, we either have a max node or a min node. If we are at a max node, then we can assume our answer will come from the child with the highest upper bound. But if we are at a min node, then we need to add up all the upper bound. The upper bound at the leaves are 1 (meaning we take the 1st highest node of its subtree which is itself). For a min node with 2 leaf child, the upper bound is 2 (meaning we need 2 high value K's to maximize that node)

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

    for each vertex, try to minimize the number of numbers which the number on this vertex must be less than (I mean '<'). Try to find this answer for all the vertices and the answer for the no.1 vertex will be used to make the final answer for this task, final answer is (number of leaves)-(answer for question I mentioned above for no.1 vertex). Hope you get it. how to get this idea: by seeing that the number on each vertex must be less than some numbers on other vertices, it depends on what type of restriction (min or max), try to minimize this number for all vertices to get better answer (bigger number for vertex no.1)

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

In pronlem E , we can check all rows and columns in a random order . In worst case , the expect number of queries is about 1020.

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

Thanks for the editorial. I was trying to solve C by trying to maintain difference of 1 between number of open and closed brackets and greedily marking a '?' as '(' or ')'. Kept failing on pretest 6 and could see on Discord after contest that a few more were failing on that pretest.

I guess another intuition of why we can make as many '?' = '(' in the beginning is that there is no way it will make the sequence unbalanced as we can always have ')' at the later half of the sequence to balance it out. My solution, in contrast would have failed for case like "((?)))" as it would have changed the first '?' to ')'.

BTW Then I think there can't be an online solution to the problem ? Am I right ?

Thanks again !

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

Problem D and E are really nice! We need more div2 contests like this ;)

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

In the editorial for 1153E — Serval and Snake,

"If the head and tail are in different columns, we can find two columns with odd answer and get them. Then we can do binary search for each of those two columns separately and get the answer in no more than 998+10+10=1018 queries totally."

Where does the 998 comes from? Isn't it that in the worst case you will have to go through 999 columns?

»
7 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +25 Проголосовать: не нравится

I hardly understand anything from D's editorial :// ,

Can anyone explain : ,

1, What dp[i] means ? ,

2, What does "the number on i is one" mean when we dont know the specific 'x' ? ,

3, Why the answer is k + 1 — dp[1] ?

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

In Q C test case : 14 ((?)))(((??))) in this example we need to replace 2 ?'s with '(' and 1? with ')' with reference to the editorial, we should first add 2'(' to the string and then 1')' so ans =((()))(((()))) but in this case sequence s1...s6 is correct parenthesis.? Can anybody explain the editorial more clearly

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

Can someone tell me how to find the bus route in O(1)? (for Problem A)

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

    time = s + ( ( (t-s+d-1)/d ) * d );

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

      Yeah, I saw it in some sources but I don't understand how it works.

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

        t-s is the duration between arrival time and initial time. But the bus need not come at the arrival time exactly. So, we add d to this time in order to get any time after arrival. Now we need to find the number of trips within the duration t-s+d.

        In order to find it, we subtract 1 form t-s+d because we don't need any trips in the last minute. So, we get number of trips=(t-s+d-1)/d. Each trip takes d time. Finally, time will be = s + ( ( (t-s+d-1)/d ) * d )

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

          For problem A, can someone explain , how to come up with this? time = s + ( ( (t-s+d-1)/d ) * d );

          Also, is it not possible to come up to this using aritmetic progression??
          Because I tried to do this:

          =>   s-(n-1)d >= t
          =>   (n-1)d >= t-s
          =>   (n-1)  >= (t-s)/d
          

          but this gives wrong ans..

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

            Let $$$time$$$ be the minimal time you need to wait for a bus.

            If $$$s\geqslant t$$$, then $$$time=s$$$. Else, you need to wait $$$x$$$ more seconds until the next bus comes. So basically, you have $$$time=s+x$$$.

            Of course $$$time\geqslant t$$$ so $$$s+x\geqslant t$$$. Moreover, you get on the bus right when the bus comes, so $$$x$$$ is divided by $$$d$$$. The problem become: Find the smallest $$$x$$$ satisfies that $$$s+x\geqslant t$$$ and $$$x$$$ is divided by $$$d$$$.

            Now we can write $$$x$$$ as $$$y\times d$$$ where $$$y$$$ is an integer. Condition $$$s+x\geqslant t$$$ can be written as $$$s+y\times d\geqslant t$$$, or $$$y\geqslant \dfrac{t-s}{d}$$$. We need to find the minimum $$$y$$$ that satisfies that condition. Here, $$$|\dfrac{t-s+d-1}{d}|$$$ is the answer (let $$$a$$$ be a real number, $$$|a|$$$ is the maximum integer that smaller than $$$a$$$), and when you save the result of the operation (t-s+d-1)/d to an integer variable, it gets the same result.

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

Got my mistake.

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

    The string (()(())()) is different from the one given in the test case, and can't be formed from (???(???(? as the second last element (s[n-2]) is different in your solution and we can only change "?" and not the "(" and ")".

»
7 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

Can someone explain with example the approach for D ?

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

Is ques D is related to some standard problem which I should solve first? because I feel many people did solve this ques in very less time and almost all have applied same approach.

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

    I feel the same way.

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

    I solved it in contest and don't remember seeing any similar problem. I started brainstorming and analyzing random trees I drew on paper and eventually saw the pattern.

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

      Could you help me? how you come to the recognize the pattern? Like not the solution I am asking, but how did you able to connect the dots in order to get the pattern?

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

        Okay, I will try to explain my thought process. The maximum query looked like it would be pretty straightforward, so I only thought about the minimum query. Idea one (Wrong): If I want the minimum of 2 children, then the max should be in one and max-1 should be in another. I drew a sample where this failed. Idea two (Failed): Make some sort of dp to find out which of the children is the one I keep. I couldn't think of anything with this approach. Idea three (Success): Look carefully at sample 3, and try drawing a few similar ones, but with more levels. Say N is the number of leaves. I saw that when a node keeps the minimum value of its children, it looses the values of all other children. Say you have 3 children, the value on this node can not be N or N-1, but it can be N-2. This led to the idea: If a node keeps the minimum, it looses all the values of its children, except the smallest one (loosing K values means the answer can't be higher than N-K). So you keep track of which values the children lose, and keep the smallest one (Number of lost numbers — 1). Now for the max query: You want to lose the minimum possible (to get the highest possible result), so you keep the child which loses the minimum. Link to my submission: https://codeforces.me/contest/1153/submission/52704203

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

What is the intuition behind problem D?

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

Am I the only one who thinks that the editorial needs more explanation?

»
7 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится +14 Проголосовать: не нравится

**Problem D : Serval and Rooted Tree **.
Can also be done in this way . for each node X keep a pair (maximum possible order of X,no of leaves subtree rooted at X has).
Order of X means if we sort values at leaves in the subtree rooted at X then let that sorted sequnce be it be L1,L2 ..,Li ...Lk then Order of x is maximum possible index .
Lets call this tuple (maximum possible order of X,no of leaves subtree rooted at X has) as (ff,ss).
if node X is of **max ** type maximum then X.ss is total leaves in subtree rooted at X i.e. X.ss=($$$\sum\limits_{}^{all child} C.ss$$$ ) where C represent child.
Ans X.ff is maximum possible order that will be X.ff=max(X.ss-(C.ss-C.ff)) i.e. X.ff=X.ss+max(C.ff-C.ss)) for all C that are child of X.

if node X is of **min ** type maximum then X.ss is total leaves in subtree rooted at X i.e. X.ss=($$$\sum\limits_{}^{all child} C.ss$$$ ) where C represent child.
Ans X.ff is maximum possible order that will be X.ff=($$$\sum\limits_{}^{all child} (C.ff -1)$$$ ) + 1.

code :
code_link

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

There is a place in question C that I don't want to understand. According to the meaning of the problem, first fill in the required'(', then fill in the required')'. In this case, there is only one filling method. I don't know how to prove that only this filling method is available.I hope someone can help me.

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

What is test case 5 in problem D ?

»
7 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +19 Проголосовать: не нравится

silly bugs T^T

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

Just when you thought asking EXACTLY 2019 queries for E was fuckin' genius...

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

For div 2 c the second example test case given- 10 (???(???(?

is ()()()()()->(1)+(1)+(1)+(1)+(1) not a solution? why?

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

I have seen problems similar to C before with parentheses checking. I would like to practice more of those .. can anyone link me to similar problems please ?

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

In problem D 1. why are we taking sum of dp values in case of min operation. 2. why are we taking min values in case of max operation.

»
7 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится +70 Проголосовать: не нравится

I think Problem F can be solved directly by calculus in $$$O(n\log n)$$$.

Let

$$$ p(x) = {x(l - x) \over ({l^2 \over 2})} $$$

(Which means the probability that a random segment cross point x.)

Then

$$$ f(x) = \sum\limits_{i = k}^n {n \choose i}p(x)^i (1 - p(x))^{n - i} $$$

(Which means the probability that a at least k random segment in n cross point x)

We just need to calculate:

$$$ \int_0^l f(x)dx $$$

We can solve it in $$$O(n\log n)$$$.

I implement it in $$$O(n^2)$$$ though...

UPD: I can't solve it in $$$O(n\log n)$$$ but $$$O(n\log^2 n)$$$. It's about polynomial interpolation so very brute.

UPD2: It can be solved in $$$O(n\log n)$$$ now. See at here.

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

Can someone explain the problem C, I have tried understanding it, but didnt worked out.

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

    So the problem is the same as asking to recover (if possible) a sequence of parentheses that is balance but any proper prefix is not balanced. The sequence of parenthesis is balanced if and only if: there's equal ( as ), and for any prefix there's no more ( than ). Saying that the sequence of parentheses is balanced but any proper prefix it's not actually implies that the number of ( is more than the number of ) for any proper prefix... that's the main idea :)

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

Problem B has another cute solution (at least I had it accepted, break me if you dare!) -- if t[i][j] = 1 (we don't care about the 0 case obviously), then the number of bricks we put is min(b[i], a[j]) (basically, the minimum of the front and left view corresponding to the brick).

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

[please ignore earlier posted message, I confused node id and value filled in]

»
7 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

I have looked at all of the solutions of D but all of them are too cryptic. I can't understand none of them at all. All these subindicies and sums are very, very confusing and I can't understand why do they pop up. Can someone please instead of saying "Do x to solve" try to explain why do x to solve? What is the intuition behind those indicies? Why this and not that?

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

Can someone explain how to do a binary search in problem E ,for example, after finding a row how to choose the two points ?

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

Can you please explain what is wrong with this submission: https://codeforces.me/contest/1153/submission/52760960 When I run it locally I get the correct answer for the second test case.

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

Why in problem F we add both f[i-1][j][1] and f[i-1][j][0] to f[i][j][1] (the first case in solution placing P at the ith position)

»
7 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится -15 Проголосовать: не нравится

Can I get Some upvotes ;)

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

Are there any problems similar to problem D for practice ?

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

Can anyone explain how this solution of D by Max.D. works ?

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

It would be nice if the problem statement explicitly says if the interacter is adaptive, just saying.

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

Can anyone explain to me what's wrong with my submission for the interactive problem E. The checker seems to be giving me an invalid answer given the mentioned input. Can anyone take a look if there's some issue? https://codeforces.me/contest/1153/submission/53116004 (check the diagnostic message for Test Case #3)

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

Why is it so hard to explain D?

I rember participating in that contest, and tried today again to solve it. With ablsolutly no success. Then did read the tutorial...still understand like nothing. Did read again all comments in this thread...but have still no clue else that is dp ...somehow.

What I especially don't understand: How can we recursively make a statement about a subtree when the value clearly depends on which numbers are used in the siblings' subtrees?

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

For Problem D Serval and Rooted Tree.

Finding the number of numbers for a vertex from which the number on the vertex must be less than. Vertices are two types max and min. 1)if the vertex is a min vertex then it has to be less than all its child except one child whose value it would take. Here if the vertex has x children then it has to be smaller than dpv+dp[c1]+dp[c2]+dp[c3]+...+dp[cx]; X-1 because it would take the value of one of it's child. 2)If the vertex is a max vertex then it has to be the maximum values among all its child. Here if the vertex has x children then it would give priority to the child which is smaller than minimum number of values dp[v]=min(dp[c1],dp[c2],dp[c3],dp[c4],....,dp[cx]); here min operation in max vertex because it would select the most optimal child and since this child is having a value greater than other children so the value of the vertex would also be greater than other children.

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

I have a solution to problem E without any binary search!

First, for columns:

First, we do sliding window query: we query a rectangle of size $$$n \times n/2$$$, and slide it from left to right. If the head and tail are at different columns, then we can find those columns when the sliding window experiences a change in parity. This takes $$$n-n/2=n/2$$$ queries for column. Similarly for rows, so it takes $$$n$$$ queries in total.

Now if they are in the same row/column, they must be in different column/row. So just query each cell of that column/row: $$$n$$$ more queries. Hence we find the common row/column.

Now, in $$$2n$$$ queries, we have found out the rows and columns. Now all that remains is to try all possible $$$2^{2}$$$ operations, so that we complete the entire interaction in $$$2n+4$$$ queries! A new record!!

You can see that my program doesn't cross 2004 queries 103719211

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

    Thanks a lot for your solution.

    I doubt that if one of the points is in column $$$1$$$ while the other is in column $$$n/2+1$$$, you solution can't find the column (the parity is always odd).

    However, following your hint, I found a solution with an upper bound in about $$$n+2\log(n)$$$. If the two points are in different columns, there will always be an odd reply in your sliding. Once we find an odd reply, we know there is exactly one point within the rectangle, and binary search works.

    So we can implement your method first for columns, if no odd reply found, then they are in the same column. Do it for rows, and we must receive an odd reply.

    To find the second point, for example, if we know they are in different columns, and we can do binary search in either column $$$1$$$ to $$$c_1-1$$$, or $$$c_1+1$$$ to $$$n$$$, whichever has odd reply.

    To estimate the upper bound, the worst case is that we didn't get anything from column queries. So they are in the same column. The binary search for the first point takes $$$2\log(n)$$$. In principle, the second one requires one $$$\log(n)$$$ for rows, but if the two points are far apart, the row queries end a lot earlier; and if they lie close together, and at the last rows, the last binary search is almost nothing.

    Here is my submission 103914149

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

I was trying to solve problem D, and not being able to solve it, I came to search for the solution here. But I found no intuitive or clear solution either in the editorial, or in the comments for people with unicellular brains like me. I looked at a few solution codes, and finally solved the problem. So I decided to attempt to write a simpler explanation than what's already here for problem D.

Let us define $$$dp_i$$$ as the minimum number of leaves in the subtree of node $$$i$$$ that we have to consider to get the maximum possible value at node $$$i$$$. In other words, say, if $$$dp_i = 2$$$, then we have to consider at least $$$2$$$ leaves as the possibility of containing the number which will be stored in node $$$i$$$. Which means, we can narrow down the number of necessary leaves to consider as the answer for node $$$i$$$ as $$$2$$$. So, if $$$dp_1=p$$$, then our final answer will be, (choosing greedily), the $$$p$$$-th largest possible number to be assigned to a leaf in the tree. So, say, there are $$$k$$$ leaves in the tree, our final answer will be $$$k-p+1$$$.

Now that we know what exactly we have to compute, let us move on to how we will compute it.

Let $$$choice_i=1$$$ if node $$$i$$$ has max written on it, and $$$0$$$ if node $$$i$$$ has min written on it.
Case 1 (MAX): If $$$choice_i=1$$$, then we can choose the child of node $$$i$$$ which has the least number of necessary leaves in its subtree. We can, thus, narrow down the answer as much as is possible to be done by node $$$i$$$. Thus, if $$$choice_i=1$$$, $$$dp_i=min(dp_{child_i})$$$.
Case 2 (MIN): If $$$choice_i=0$$$, then we do not have an option of narrowing down the number of necessary leaves at node $$$i$$$. We must consider all the necessary leaves of all children of node $$$i$$$ to compute the number of necessary leaves for node $$$i$$$. Thus, if $$$choice_i=0$$$, $$$dp_i=\sum dp_{child_i}$$$.
If node $$$i$$$ is a leaf, we do not have to consider $$$choice_i$$$ as it does not have any children. Thus, if node $$$i$$$ is a leaf, $$$dp_i=1$$$.

Thus, we can summarize the solution as:
- if node $$$i$$$ is a leaf, $$$dp_i=1$$$.
- if $$$choice_i=0$$$, $$$dp_i=\sum dp_{child_i}$$$.
- if $$$choice_i=1$$$, $$$dp_i=min(dp_{child_i})$$$.
$$$answer=k-dp_1+1$$$.

C++ solution

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

Problem D is a simpler version of this question.

Problem E is just a simpler version of this question.

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

Problem F can be solved in $$$O(n)$$$. First, notice that for segment $$$[0,1]$$$, the probability that point $$$x$$$ is covered by at least $$$k$$$ subsegments is

$$$\sum_{s=k}^n(-1)^{s-k}C_{s-1}^{k-1}C_n^s2^sx^s(1-x)^s.$$$

Second, notice that

$$$\int_0^1x^s(1-x)^sdx=\frac{s!s!}{(2s+1)!}.$$$

So the final answer is

$$$l\sum_{s=k}^n(-1)^{s-k}2^sC_{s-1}^{k-1}C_n^ss!s!/(2s+1)!.$$$