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

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

Hi, Codeforces!

1529A - Eshag Loves Big Arrays

problem idea and solution: AmShZ

editorial
official solution

1529B - Sifid and Strange Subsequences

problem idea and solution: Davoth

editorial
official solution

1528A - Parsa's Humongous Tree

problem idea and solution: shokouf, AmShZ

editorial
official solution

1528B - Kavi on Pairing Duty

problem idea and solution: alireza_kaviani

editorial
official solution

1528C - Trees of Tranquillity

problem idea and solution: AliShahali1382

editorial
official solution
better implementation of official solution: AaParsa
python solution: Tet
another O(n.log) solution with seg-tree: N.N_2004

1528D - It's a bird! No, it's a plane! No, it's AaParsa!

problem idea and solution: AmShZ, AliShahali1382

editorial
official solution
a different O(n^3) solution: Atreus

1528E - Mashtali and Hagh Trees

problem idea and solution: AliShahali1382

Thanks to Kininarimasu for the editorial.

editorial
official solution

1528F - AmShZ Farm

problem idea and solution: AmShZ, AliShahali1382

editorial
official solution
Разбор задач Codeforces Round 722 (Div. 1)
Разбор задач Codeforces Round 722 (Div. 2)
  • Проголосовать: нравится
  • +260
  • Проголосовать: не нравится

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

Auto comment: topic has been updated by AliShahali1382 (previous revision, new revision, compare).

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

Woow !! Fast editorial Thanks :)

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

There might be an error in the n log n solution with segtree (div1. problem C).

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

I hope all the future rounds will be as good as this one

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

Nice contest with nice problems! Thanks

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

Genius problems. Nice job.

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

Bad tests in D? The $$$\log n$$$ part in my $$$\mathcal{O}(nm \log n)$$$ code triggers only $$$0.025 N^3$$$ times in large tests: 117257732

EDIT: dorijanlendvaj found a large input where it triggers $$$0.5 N^3$$$ times, that's $$$20$$$ times better than the original test data!

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

    we wanted to fail high-constant O(n^3.log)(like seg-trees) and let easily optimized O(n^3) or O(n^3.log) with heaps to pass.

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

Via 1529B — Sifid and Strange Subsequences

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

Dfs Based Solution of C Parsa's Humongous Tree without DP based on the fact that during path traversal on a tree from root , A particular node is reached only once.

We return a pair {a,b} from every node.

A has the best solution from that node to all nodes below it when left_mini is assigned to that node.

B has the best solution from that node to all nodes below it when right_maxi is assigned to that node.

We store best possible values of the current node from all children node i.e. one with maxi on all possible calls and other with mini and then return it.

In the end when recursive calls end print the maximum of the pair of values returned from the root.

Solution

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

    What you are describing is the same DP on a tree. The official solution also uses DFS.

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

      U don't need to save results in a array for Dp in this DFS

      Both best case possibilities are calculated and return from there.

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

Where is the editorial on F?..

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

    I've added it but it doesn't seem to appear ...

    will probably get fixed soon

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

For Div2 D- "x≤n: In this case, due to the lemma mentioned above all the segments must have the same length, thus their length must be a divisor of n, in this case they can be paired in D(n) ways; where D(n) is the number of divisors of n.".

The equality should not be present I think. It should be only x<n. Correct me if I am wrong!

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

    If 1 is paired with n, each number from n+1 to 2n must belong in a segmemt with length n, where the length of a segment is defined as the number of elements inside it.

    The pairing is unique as well ...

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

I have done Question b in two way 1st submission : 117264950 2nd submission : 117264953 I can't find why my second submission failed I did the same thing but just in another manner, can anyone help me out please

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

    It took me a lot of time but I found out what's wrong with your code. Actually, you are failing edge case when seq is [-1] or [0] because in your second program you'll be having cnt = 1 and since there is only 1 item in the array it will not enter the while loop, and bool will evaluate to true and it will increase it by 1 resulting in ans = 2. Luckily your former code worked for these edge cases.

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

      First thank you very much for your efforts Could you please help me this submission , it is failing on 10000th test case 117240464

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

        I have submitted your code with a minor change, it got ac. You just had to account for the special case when n equals 1.

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

i am sorry for every one get wrong answer in problem B test 15 i put it in hacks :( can you give contriubution for it

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

Can anyone explain the DP applied in Kavi on the Pairing Duty problem in simple words?

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

In Div 1 Problem D:

"To handle the "waiting" concept, we can add n fake edges, i-th of which is from the i-th vertex to the (i+1 mod n) -th vertex with weight equal to one."

What is a vertex here? Does this sentence mean the cities? But waiting for 1 second doesn't move you to the next city, so that can't be. Or can it? Is the assumption, that "if we can reach city X from some other city Y, then we could've waited for 1s in Y and then we would've reached X+1"? That seems also to be the explanation for "It can be proved that doing dijkstra in the new graph is sufficient if we guarantee that the first used edge is not fake."

Well, It seems I answered my own question here while writing it down. Debugging-Rubber-Ducky for the win. But since I really was confused by this and I don't feel that this step is trivial, I'm gonna leave this comment here.

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

    "It can be proved that doing dijkstra in the new graph is sufficient if we guarantee that the first used edge is not fake.

    We can map waiting for x seconds and then using an edge to go to u from v to using a cannon and then using x fake edges to go to u from v."

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

      Oh, yes you are right. I'm just thick as a brick and didn't get that sentence before. Thanks for the heads up! :)

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

Can someone explain how to find Stirling numbers of the 2nd kind in O(k log k) (for F)?

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

    From wikipedia page \begin{gather*} S(n, k) = \frac{1}{k!}\sum_{i=0}^k (-1)^i \binom{k}{i} (k-i)^n \end{gather*}

    You can rewrite it as \begin{gather*} S(n, k) = \sum_{i=0}^k \underbrace{\left((-1)^i\cdot \frac{1}{i!}\right)}_{a_i} \cdot \underbrace{\left(\frac{1}{(k-i)!}\cdot(k-i)^n\right)}_{b_{k-i}} \end{gather*}

    So you can calculate arrays $$$a$$$ and $$$b$$$, and stirling numbers are just convolution of these arrays:

    \begin{gather*} S(n, k) = \sum_{i=0}^k a_i \cdot b_{k-i} \end{gather*}

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

I am getting TLE for Div 2 C Mysolution. I have done almost the same as Editorial (using memorization of recursion). Please anyone help me with debugging. Thank you

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

Since E editorial is not out yet, here is how I solved. Let $$$dp_i$$$ be the answer for all trees such that there exists a root and all edges are directed in the same direction from root and the root has at most $$$2$$$ children. We transition

$$$dp_i$$$ = $$$dp_{i-1} + dp_{i-1}*pdp_{i-2} + dp_{i-1}*(dp_{i-1}+1)/2$$$ where $$$pdp_i = \sum_{j=0}^i dp_j$$$

Then let $$$dp2_i$$$ be the same as $$$dp_i$$$ except the tree must have $$$2$$$ children. So $$$dp2_i = dp_i - dp_{i-1}$$$.

The answer for these cases is

$$$2*(dp_n + dp_{n-1}*pdp_{n-2}*(pdp_{n-2}+1)/2 + pdp_{n-2}*dp_{n-1}*(dp_{n-1}+1)/2 + dp_{n-1}*(dp_{n-1}+1)*(dp_{n-1}+2)/6) - 1$$$

This is because $$$dp_n$$$ holds the answer for at most $$$2$$$ children and the other section accounts for the rest. We multiply by $$$2$$$ to account for both edges directions, and subtract $$$1$$$ because a single path is isomorphic.

This obviously doesn't handle all cases, but all other cases can be found in the following form. Let $$$t_{up,k}$$$ be a tree where the root has $$$2$$$ children and the edges are directed up and the longest path is $$$k$$$, and let $$$t_{down,k}$$$ be a tree where the root has $$$2$$$ children and the edges are directed down and the longest path is $$$k$$$. Then all other cases are $$$t_{up,k}$$$ which exists on some path of length $$$l$$$ to and connects to $$$t_{down, n-k-l}$$$

We can count every other case as $$$\sum_{i=0}^{n-1} (dp_i-1)*dp2_{n-1-i}$$$

This works because we pretend the path is always length $$$1$$$, then if we do $$$dp_i*dp2_{n-1-i}$$$ we handle all cases except for when the $$$t_{up, k}$$$ is empty, and that only happens one time.

Here is my submission: 117272002

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

In problem B: my solution passed the pretests but giving time limit exceeded in test 4. It runs in O(nlogn) time l. Here is my submission Can someone help me finding what's wrong in my code?

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

    In your code's solve, second range must be time limit exceeded.

    the range is $$$O(n)$$$.And the min_element also is $$$O(n)$$$.so it's $$$O(n^2)$$$.

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

can anybody please tell in B why its optimal to always choose negative elements

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

    the time complexity of min_element() is O(n), you are doing it (n-1) times in the worst case. you should think of propagating from the start only.

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

    because their maximum element is negative and absolute of all differences is greater than or equals to zero that is greater than negative maximum element.

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

    Suppose we choose more than 1 positive numbers a, b..., |a — b| < max(a, b), that means we can't choose more 1 positive numbers. Suppose all the numbers we choose is non-positive numbers, |ai — aj| >= max(ai, aj) is always true. So we just firstly choose all the non-positive numbers from the array, then calculate the minimum distance between these chosen numbers, we call it mn. And then check the original array if there is a positive number that is no greater than mn.

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

Problem B Div-2It's easy to prove that a strange subsequence can't contain more than one positive element.

Can someone please prove this ?

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

    Assume that it contains two positive elements — say 2 and 5 . Max is 5. Difference is 3 . Similarly, it is easy to see that difference of two positive numbers will always be less than the greater element of those two numbers. Hence taking more than one positive number is not possible

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

    For two positive elements, we have the property that their absolute difference will be strictly smaller than the larger element. So, you can clearly see that this violates the strange condition

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

can anyone find error on my code for Div2 C failing on test case 2. I can't find any error ,i checked it more than 10 times ,any help will be really appreciated

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

In div1 C / div2 E , Can someone explain the part 3 , i.e. "Among the vertices inside it, find the maximum number of vertices such that none of them is the ancestor of the other in Keshi's tree." . How are we doing it .

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

    When you give a dfs order of a tree, there will be no pair satisfies

    $$$in[u] < in[v], out[u] < out[v]$$$

    and u is a ancestor of v if and only if $$$in[u] < in[v] \leq out[v] \leq out[u]$$$. so,

    if neither of u or v is the ancestor of the other, then $$$[in[u], out[u]), [in[v], out[v])$$$ will be disjoint interval. and std::set is enough to maintain it.

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

      which also means euler tour generates a set of laminar ranges

      -PurpleCrayon

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

      So, when you are at a leaf you have a set of intervals with you corresponding to the path from $$$1$$$ to the that leaf. And you want to know maximum number of non-overlapping segments? Could you explain how you implemented it using std::set?

      UPD: Never mind. I got the idea. We can maintain only the disjoint intervals

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

        Do you mean to say that in that set we only keep those intervals which will guarantee the max ans? If you do then won't it cause a problem while erasing a certain node? If not then what do you mean?

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

          Yes, we are maintaining the intervals that would cause maximum answer... The idea is that whenever you add a new segment in the set, then there are only three cases —
          (1) The new segment doesn't intersect with any (in this case we simply add it)
          (2) It comes inside some existing segment (so we remove the existing segment and add this one)
          (3) If this segment covers some additional segments within it (we don't add it)..

          So, whenever we add a segment it removes $$$\textbf{atmost 1}$$$ segment from the set. We can keep track of which segment removes which one, and any change can be rolled back easily.

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

Bye Bye Specialist!!
Hello Pupil!!

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

In Div 2 C, I think the condition should be $$$a_u \geq a_v$$$ for the first case and similarly $$$a_u \leq a_v$$$ for the second case. AliShahali1382

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

    No, the case $$$a_u = a_v$$$ is handled in the third (otherwise) part.

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

      No it's not handled in the third part. Because beauty of tree will still depend on $$$a_v$$$.

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

        Ah, I see what you mean. I should rephrase that part.

        It's better to compare the number of vertices $$$p$$$ such that $$$a_p > a_v$$$ and the number of vertices $$$q$$$ such that $$$a_q < a_v$$$. Thanks for noticing!

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

Very good round. ! Thanks for this round ! Graph and DP is always fun to solve in a contest. !

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

In Div-1 C [Editorial]: What are efficient ways to compute below?

Among the vertices inside it, find the maximum number of vertices such that none of them is the ancestor of the other in Keshi's tree.

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

This one of the best contest I have ever given on codeforces.

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

Can anyone explan in 2D,when $$$x\le n$$$,why each length must be a divisor of n?

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

    1623152887770

    Here, $$$1$$$ pairs up with $$$x$$$ and $$$x <= n$$$. Let say pattern repeats $$$p + 1$$$ times, the bottom right equation in the image is for total length which is $$$2n-1$$$. (I have numbered from $$$1$$$ to $$$2n$$$).
    $$$l(p+1) = n$$$ tells us that $$$l$$$ must be divisor of $$$n$$$.

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

Did anyone try solving Div2-D by solving the complementary problem??

The complementary problem was to find the total number of bad pairings, where a pairing is considered to be bad if, for at least two segments $$$A$$$ and $$$B$$$ among those, the following condition holds:
$$$1)$$$ $$$A$$$ and $$$B$$$ don't lie completely inside each other.
$$$2)$$$ $$$A$$$ and $$$B$$$ don't have the same length.

I was able to find exactly two segments $$$A$$$ and $$$B$$$, which will follow the above condition(if I am not wrong in computing that xD), but could not reduce it further. Is it even possible this way or is there any other approach to solve it??

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

I referred the editorial for Div2(C)/Div1(A)... But my solution failed after multiple attempts. Can someone please help me out? My solution 117289608

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

"To be continued ..." in Div. 1 C's editorial?

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

    At the time of posting, the editorial was incomplete and said "To be continued ...", now it has been fixed.

    I really don't deserve the downvotes.

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

dpforces

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

Can someone describe the data strcuture used for Div1 C? It just displays "to be continued".

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

    I used a segment tree.

    tin[u] is the entry time of the vertex u and tout[u] is the exit time of the vertex u during dfs.

    1) To insert a node: First check if any of its ancestor is already inserted. If yes, remove the ancestor. Now, to insert the node, update the value in segment tree at the index tin[u] as tout[u].

    2) To delete a node: Update the value in segment tree at the index tin[u] as 0.

    3) To check if any ancestor is present: If current node is u, any ancestor v will satisfy the property tin[v] < tin[u] && tout[v] > tout[u], so to check if any ancestor is already inserted, the range max query on this segment tree will be max_query(1, tin[u] - 1) > tout[u] if an ancestor in inserted.

    For reference you can look at 117219173.

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

      Thanks

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

      Won't there be a situation when it is better to remove the current node instead of the ancestor?

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

        Nope, all the subsets which are satisfied by any ancestor will also be satisfied by this vertex, but not vice-versa.

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

          Wait, so if the current node is itself an ancestor of some of the previous nodes in the path, then does that mean it is the current node that will be removed?

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

            "It's easy to see that the vertices in the data structure always form a path from root to some vertex v in Soroush's tree." so v (the current node) will never be an ancestor of any node currently in the data structure.

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

              No, I meant what happens if the current node (v) is an ancestor of some node in the data structure according to Keshi's tree?

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

      A bit of a late reply, I am currently trying to wrap my head around this segtree solution implementation, but I have a question: how does the code actually take into consideration the case when u is an ancestor of a previously visited node v (by "ancestor" I mean an ancestor in Keshi's tree)? What I mean is that, in the DFS function in Soroush's tree, how does the code take into consideration the case when the current node u is an ancestor (in Keshi's tree) of a previously visited node v by the DFS?

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

In div2 D, lets say n=4(8points) And lets say we pair 1 with 4 What are the rest of the pairs? Can anyone list them?

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

    You can't pair 1 with 4 that's invalid. If u draw on paper u can see why. You can pair 1 with 3 and 2 with 4 followed by 5 with 7 and 6 with 8.

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

      But when we calculate the divisors of n, we get 1,2 and 4, right?

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

        You don't include n amongst divisors. (I think maybe this wasn't mentioned in editorial)

        for n = 4 these are the valid equal length pairings.

        1. (1,2) (3,4) (5,6) (7,8) for divisor = 1
        2. (1,3) (2,4) (5,7) (6,8) for divisor = 2

        which is 3 — 1 = 2.

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

          resolved

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

            One minute it is correct my bad.. actually the way I have implemented is a bit different

            there is one more pair (1,5) (2,6) (3,7) (4,8)

            You may want to edit your comment. I am sorry for the mistake.

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

              these are actually those equal length pairings which do not have anything lying inside them

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

              But this is when x>n In the other case, when x<=n, we still cannot take x=n right?

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

                According to the tutorial's method, author discuss the problem for 2 cases x <= n and x > n, and as we can see, in the former, we can only take length at most n — 1,also in the solution, d(n) is proper divisor of n. But from my point of view, talk in x <= n + 1 and x > n + 1 is more natural, from then on, length is <= n now, d(n) is all divisors of n, including itself, the formula has finally become $$$dp_n = \sum_{i = 1} ^ {n - 1} dp_i + d(n)$$$. also it is no need to initialize $$$dp_0 = 1$$$ (which has no useful purpose) anymore.

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

                yes we can't take when x = n.

                the one case which is contributing to total number of divisors is from the x > n case and nothing lying within it.

                If you see my code I have added + 1 separately in dp[i] = dp[i-1] + 1 + count[even divisors of 2*n]

                and from the number of even divisors I have subtracted -1;

                strangely the net sum ends up being dp[i] = dp[i-1] + divisors.

                even divisors of 2 * n is same as odd divisors of n

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

I really loved the problems!

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

Sorry to correct one possible little mistakes about the editorial of problem 1528A. In case 3(p=q),to maximize the beauty of the tree, au must be rv or uv.For example, if au=3,lv=0,rv=7, p=q=4, av=4,5,9,5,1,2,0,-1, the original beauty is 21.If change au into rv, the beauty will be 35.(In a word,I mean " The beauty of the tree doesn't depend on av" is not very rigorous.) However,thanks for your editorials again! They have helped me a lot!

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

Can someone tell, how to solve Div2C/Div1A if we need to minimise the sum instead of maximising?

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

    Just a thought: We can define $$$dp_{v , j}$$$ as the minimum beauty of $$$v$$$'s subtree if $$$a_v$$$ is equal to $$$j$$$. This is too slow though, because I don't see any fact like the one in the maximizing version in here :(

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

      I think that would work, if we have small ranges, but how to do if we have large ranges as in the problem?

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

        That's why I said it's too slow. Maybe we can prove that $$$dp_{v , j}$$$ is convex or maybe not ...

        But it should be a fun problem if it turns out to be convex :D

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

how would we solve the div2 problem A if we were asked to find the max no of operations instead of max no of elements to be deleted?

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

    The algorithm I provided deletes $$$1$$$ element at a time, thus does the maximum number of operations. It's worth noting that the answer to both of the problems is the same.

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

Getting TLE on test case 4 of Div2 C. It follows the editorial explanation. Any help would be appreciated. Here's my submission.

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

    Add cin.tie(0) , ios::sync_with_stdio(0); to your main function and it should be fine.

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

      Thanks a lot. It works now. But why does it not work within the time constraints? Most of the n <= 1e5 cases work fine without fast I/O in O(nlogn) sorting time. Why does this solution, which is supposed to be O(n), fail to run in the required time?

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

        It's not as simple as that, the sum of $$$n$$$ over all test cases can be as large as $$$2 \cdot 10^5$$$, there can be approximately $$$2 \cdot 10^5 \cdot (5 + 9 + 9)$$$ characters in the input which is quite a lot.

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

          I'm sorry I did not understand, could you elaborate?

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

            I said that you might need to read more than 4600000 characters from the input (4.5e6). cin itself is slow so you can't read that many characters in time. So you have to add that magical line to your code to make it work faster.

            This is because by default, before you call the function cin, the call cout << flush is done. Adding cin.tie(0) unties cin from cout and stops this.

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

Can anybody please help me what is wrong with this code it is showing TLE test set 4 of question 3 Please help Thank you in advance

Here is my code: https://codeforces.com/contest/1528/submission/117327246

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

    You forgot to add cin.tie(0) , ios::sync_with_stdio(0); to your main function.

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

First time to solve C without fst though I used O(n(logn)^2) algorithm.

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

Can someone explain to me for 1528A — Parsa's Humongous Tree why every vertex needs to be either lu or ru. I'm finding it difficult to understand. Thanks

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

    It doesn't, but we proved that an optimal assignment exists that each $$$a_v$$$ is either $$$l_v$$$ or $$$r_v$$$.

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

what does it mean by ans += a[i] != mn; in the 2nd for loop?

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

Can someone tell me why this code times out? (https://codeforces.com/contest/1529/submission/117339337)

The problem is 1528A — Parsa's Humongous Tree. Thanks!

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

    Please read the comments above ... at least two people have already asked this.

    You forgot to use ios::sync_with_stdio(0), cin.tie(0);.

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

In problem Div1C:

"Now to check whether any vertex u in the subtree of vertex v exists in S,..."

You don't need this check at all to AC. This is because of the condition that $$$p[i] < i$$$ for all $$$i$$$ in both trees. Assume that $$$x$$$ is a parent of $$$y$$$ in any tree, then that ensures that $$$x<y$$$. You can see that it can't be true that $$$x$$$ is a parent of $$$y$$$ in Soroush's tree (and is therefore in the stack of nodes beforehand when we start DFS at $$$y$$$), and $$$y$$$ is a parent of $$$x$$$ in the other tree, since that would require $$$y<x$$$ and $$$x<y$$$.

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

In 1528A — Parsa's Humongous Tree, i used a bit different approach in dp which seems quite similar to me with the one provided in editorial . But it is not passing the pretests and i can't figure out what is wrong with my approach. I am new to dp. Can someone help me out?
Code using my own approach: my approach Code using editorial's approach: editorial approach

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

In problem C official solution, because of the way the trees are given in the input (each vertex in both trees has higher label than its ancestors), there's actually no need to check the case where $$$res = -2$$$. For any pair of vertices $$$u, v$$$, if $$$u$$$ is ancestor of $$$v$$$ in the first tree $$$(u < v)$$$, $$$v$$$ can't be ancestor of $$$u$$$ in the second tree as it would imply $$$u > v$$$. So, you always insert the current vertex, you just need to check if there's any ancestor of it and if there is, just remove it :)

Here's the submission 117369367 with the corresponding assert.

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

In the editorial solution of 1528A, the proof claims we can get a better result by changing av to lv or rv because the contribution from its children increase in this process. But at the same time it can reduce the contribution from its parent right? How do we say that the net result of changing av is an increase in beauty?

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

    In the editorial solution of 1528A, the proof claims we can get a better result by changing av to lv or rv because the contribution from its children increase in this process.

    No, not $$$v$$$'s children but the vertices adjacent to $$$v$$$, this includes $$$v$$$'s parent as well.

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

https://codeforces.com/contest/1529/submission/117382997 This is my solution for problem C. It is showing TLE. Can anyone debug my solution,Pease.

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

    There's nothing wrong with your code. The input is quite large so you have to add cin.tie(0) , ios::sync_with_stdio(0); before reading input to do it faster.

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

Well, the editorial of the 1528C - Trees of Tranquillity has some mistakes.

In the sixth to last line, $$$p\geq st_v0$$$ should be changed to $$$p\geq st_v$$$.

"is less than $$$st_v$$$" should be changed to "is less than $$$ft_v$$$".

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

Q2 could've been solved using binary search as well which would reduce the time complexity. https://del.dog/binarysearchsolution.txt here is my solution using binary seach.

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

Here is a stupid solution for Div.1 C:

The problem is as such: choose a maximum subset of a path in Soroush's tree such that if $$$u$$$ and $$$v$$$ are in that subset neither $$$u$$$ nor $$$v$$$ is an ancestor of the other in Keshi's tree.

The problem can be solved as such: dfs on Soroush's tree, take node $$$u$$$ if none of its subtree in Keshi's tree is taken. If $$$u$$$ has ancestor $$$p$$$ in Keshi's tree that was already taken, greedily take $$$u$$$ in place of $$$v$$$, this works because a lower node is an ancestor of fewer nodes, in another word: picking $$$u$$$ instead of $$$p$$$ offers the chance to pick $$$v$$$ where $$$v$$$ is a child of $$$p$$$ and a sibling of $$$u$$$.

To solve this we'll need two kinds of queries: 1. on a path from root to $$$u$$$ find if a node is already taken, 2. in the subtree of $$$u$$$ find if a node is already taken. Both queries can be done by using a an approach similar to here.

Similar to the editorial we store $$$st$$$ and $$$ft$$$ for each node, notice that for the range $$$[0, st_u]$$$ a node exists exactly once if it is on the path from root to $$$u$$$ and may exist twice otherwise, thus we just need to focus on nodes that occurred a single time. To add a node we update the segment tree at $$$st_u$$$ and $$$ft_u$$$ to be $$$u$$$, to remove a node we set them to $$$0$$$.

Since at most one ancestor is taken at a time, we can implement an xor segment tree on the dfs order and query the range $$$[0, st_u]$$$; undesirable nodes cancel themselves out, all other nodes will be $$$0$$$ except for (potentially) one node: $$$p$$$ which we will then remove and take $$$u$$$ instead. To solve 2, I just implemented an or segment tree (max would work as well) to find if a node exists in subtree.

my solution, not pretty

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

For problem F, how do you go from the sum with binomial coefficients to the sum with Stirling numbers? Also, should the sum be from CNT=1 to n?

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

    I got it.

    We can interpret $$$\sum_{r=1}^n \binom{n}{r} \cdot n^{n-r} \cdot r^k$$$ as the number of functional graphs whose vertex set is $$$V = A\cup B\cup Z$$$ such that

    1. $$$|A|=n$$$, $$$|B|=k$$$, and $$$Z=\{z\}$$$ with $$$f(z)=z$$$,

    2. $$$f(V)\subset A\cup Z$$$ (no element of $$$B$$$ has any incoming edges),

    3. Let $$$C=\{a\in A:f(a)=z\}$$$ be the elements in $$$A$$$ that map to $$$z$$$. Then, $$$|C|=r$$$ and $$$f(A\setminus C)\subset A$$$ (the elements in $$$A$$$ that don't map to $$$z$$$ all map to some element in $$$A$$$),

    4. $$$f(B)\subset C$$$.

    This interpretation is correct because there are $$$\binom{n}{r}$$$ ways to choose the subset $$$C$$$, then $$$n^{n-r}$$$ ways to choose how $$$A\setminus C$$$ maps to $$$A$$$, and $$$r^k$$$ ways to choose how $$$B$$$ maps to $$$C$$$.

    We can count these functional graphs in another way. First, let $$$i=|f(B)|$$$.

    1. Choose how to map $$$B$$$ to $$$f(B)$$$ by first partitioning $$$B$$$ into $$$i$$$ sets. There are $$$\begin{Bmatrix}k\\i\end{Bmatrix}$$$ ways to partition $$$B$$$ into $$$i$$$ sets and $$$i!$$$ ways to order these sets.

    2. There are $$$\binom{n}{i}$$$ ways to choose a subset of $$$A$$$ to be $$$f(B)$$$.

    3. There are $$$(n+1)^{n-i}$$$ ways to choose how $$$A\setminus f(B)$$$ maps to $$$A\cup Z$$$.

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

Can anyone explain why pairing is unique for x <= n in Div2 D/ Div1 B.

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

There's an error in tutorial of Div1B.

$$$dp[i]$$$ is not equal to $$$\sum_{0}^{n-1} dp_i+D(n)$$$, but equals to $$$\sum_{0}^{n-1} dp_i+D(n)-1$$$ instead.

That's because length must be a divisor of $$$N$$$ and less than $$$N$$$, otherwise $$$x>N$$$.

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

    Oops! Sorry, it's fixed now; Although it might take some time for the changes to appear. Thanks for noticing!

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

Can somebody explain magical number 63 in div.1-D solution? From the first glance it implies that answers can't exceed 63...

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

    What are you talking about?

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

      1528D -> official solution (not editorial but code)

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

        I'm assuming that you are talking about memset(a, 63, sizeof a).

        Memset works by writing values byte by byte based on the given value. So if you want to set an array to large values, use memset with 63, this will set all values to ‭1,061,109,567‬.

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

B. Sifid and Strange Subsequences

Considering The Test Case

14 9276 -5771 -783 -6209 2041 -9286 -8065 3975 -2961 2689 -1680 9924 1483 3067

The -ve numbers are -9286,-8065,-6209,-5771,-2961,-1680,-783

And the minimum +ve number is 1483

I am confused about how can just adjacent checking ensure that the difference of pairs in the subarray satisfy the condition of strange sequence. For example abs(-9286-(-8065)) is not > 1483 similarly it holds true for -1680 and -783. Can someone please explain me why is this intuition incorrect .

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

    Consider this task: Given a non-decreasing array $$$a$$$, you must find a pair of elements $$$a_i$$$ and $$$a_j$$$ such that $$$i < j$$$ and $$$|a_i - a_j|$$$ is minimum. Obviously, there exists an answer such that $$$j-i = 1$$$, because otherwise we could have chosen $$$a_{i+1}$$$ without getting a worse result.

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

Deleted