AliShahali1382's blog

By AliShahali1382, history, 4 years ago, In English

Hi, Codeforces!

1529A - Eshag любит большие массивы

problem idea and solution: AmShZ

editorial
official solution

1529B - Sifid и странные подпоследовательности

problem idea and solution: Davoth

editorial
official solution

1528A - Огромное дерево Parsa

problem idea and solution: shokouf, AmShZ

editorial
official solution

1528B - Kavi разбивает на пары

problem idea and solution: alireza_kaviani

editorial
official solution

1528C - Деревья примирения

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 - Это птица! Нет, это самолет! Нет, это AaParsa!

problem idea and solution: AmShZ, AliShahali1382

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

1528E - Mashtali и деревья Hagh

problem idea and solution: AliShahali1382

Thanks to Kininarimasu for the editorial.

editorial
official solution

1528F - Ферма AmShZ

problem idea and solution: AmShZ, AliShahali1382

editorial
official solution
  • Vote: I like it
  • +260
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +38 Vote: I do not like it

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

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

Woow !! Fast editorial Thanks :)

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

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

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

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

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

Nice contest with nice problems! Thanks

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

Genius problems. Nice job.

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

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!

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

    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.

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

Via 1529B — Sifid and Strange Subsequences

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

    This is what happens when you don't help in making statements. :(

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

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

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

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

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

      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.

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

Where is the editorial on F?..

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

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

    will probably get fixed soon

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

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!

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

    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 ...

»
4 years ago, # |
  Vote: I like it +106 Vote: I do not like it
Spoiler
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

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

    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.

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

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

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

        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.

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

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

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

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

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

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.

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

    "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."

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

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

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

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

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

    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*}

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

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

»
4 years ago, # |
Rev. 11   Vote: I like it +134 Vote: I do not like it

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

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

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?

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

    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)$$$.

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

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

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

    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.

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

    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.

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

    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.

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

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 ?

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

    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

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

    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

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

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

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

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 .

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

    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.

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

      which also means euler tour generates a set of laminar ranges

      -PurpleCrayon

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

        Sounds like something important. What does it mean tho'?

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

          all ranges of tin and tout are either completely disjoint or contained within each other

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

      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

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

        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?

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

          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.

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

            Oh that's brilliant. Thanks

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

Bye Bye Specialist!!
Hello Pupil!!

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

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

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

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

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

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

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

        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!

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

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

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

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.

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

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

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

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

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

    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$$$.

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

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??

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

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

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

    You forgot to call dfs.

    Adding dfs(1 , 0) should fix your code...

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

    Clear the global arrays before every test case as well, apart from calling the dfs function.

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

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

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

    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.

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

dpforces

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

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

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

    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.

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

      Thanks

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

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

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

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

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

          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?

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

            "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.

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

              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?

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

                Then we just ignore it and won't insert it.

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

                  Thanks, I will try to implement it with this thing in mind.

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

      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?

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

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?

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

    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.

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

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

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

        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.

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

          resolved

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

            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.

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

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

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

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

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

                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.

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

                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

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

I really loved the problems!

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

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!

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

    Fixed, thanks for noticing.

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

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

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

    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 :(

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

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

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

        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

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

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?

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

    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.

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

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

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

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

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

      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?

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

        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.

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

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

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

            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.

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

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.net/contest/1528/submission/117327246

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

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

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

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

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

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

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

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

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

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

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

    If $$$a_i \neq mn$$$: $$$ans += 1$$$

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

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

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

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

    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);.

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

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$$$.

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

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

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

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.

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

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?

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

    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.

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

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

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

    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.

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

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$$$".

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

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.

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

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

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

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?

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

    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$$$.

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

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

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

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

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

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

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

    What are you talking about?

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

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

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

        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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

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

Deleted

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

    It TLEs because you are an illiterate moron who refuses to read the other comments.

    It's been asked here, here, here and here.

    Also your fixed code: 146310603

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

      My fault, I read the other comments, but forgot to delete mine,

»
7 months ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

can anyone help me in debugging first question? I am getting time limit exceeded on test case 4?261641603 thanks in advance:)