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

Автор rotavirus, история, 5 лет назад, По-английски
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Разбор задач Codeforces Round 581 (Div. 2)
  • Проголосовать: нравится
  • +95
  • Проголосовать: не нравится

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

Выложите, пожалуйста, разбор на русском.

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

    давайте завтра

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

    перевёл

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

      В русской версии разбора задачи D2 есть ошибка. Строка будет состоять из нулей на префиксе и единиц на суффиксе, а не наоборот, иначе, очевидно, если в ней есть хотя бы одна единица, в ней есть фиксированная подстрока.

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

$$$k[x][y] = \binom{x+y}{y} - \binom{x+y}{y+1}$$$ when $$$x \leq y$$$

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

Thanks for the fast editorial!

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

For D I found a crazy solution: Because substring '10' always contributes to the length of the longest subseq 1, so erase them doesn't matter. Thinking of doubly linked list. Improve the effiency with a queue.

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

Can somebody explain why SYSTESTS in C were so weak? 59156761 passed all tests, though it's wrong and stupid(don't look at dfs, it's not used here) Maybe rotavirus didn't expect that there will be another solutions?

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

Can someone explain why this statement is true? 1204D2 — Kirk and a Binary String (hard version) "If we change any 1 to 0 and the longest nondecreasing sequence of the whole string remains the same, then we are able to change it to 0."

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

    It follows from the first solution

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

    (this answer was logically wrong and I am not sure how to delete it)

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

    You could also think of solution 2 as alternative implementation of solution 1:

    Let's call a string p fixed if there isn't another string t of the same length which satisfies the first condition of the statement

    So when you change some one to zero:

    If one belongs to fixed substring:

    Spoiler

    If it does not:

    Spoiler

    So the we can check if this one is a part of fixed substring by checking if the LIS of the whole string changes.

    So it's essentially just another way to find all ones which don't belong to any fixed substrings and set them to zero.

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

D is very nice and the solution can be very short. Thanks to the problem setters. https://codeforces.com/contest/1204/submission/59185334

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

Hey, I'm new to graphs. Can anyone please elaborate 1204C ?

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

    I will tell you how I approached it. You should now floyd warshall as a pre requisite(3 nested loops, very intuitive). Now the moment you have all pairs shortest paths in a matrix, follow this.

    Consider two arrays: steps and successor. Step[i] stores the number of steps required to reach the last node from the $$$A_i th$$$ node, and successor[i] will store the index of the next position to go from i. Remember that we want a subsequence so successor can be any index greater than i. Now let us start from the back and construct both these arrays. So, for every node starting from the last you check whether it is in the path that was given to us as input at $$$d$$$ steps back, here d is distance of the node (varies from 1 to n) from the current node(ith node). If it is then it followed the shortest path since all edge weights are same(so take 1 as edge weight). Now, $$$ step[i - d[j][i]] = min(step[i] + 1, step[i - d[j][i]]) $$$ and to make the successor array simultaneously, update the $$$successor[i - d[j][i]]$$$ to i whenever $$$step[i - d[j][i]] > step[i] + 1$$$. Obviously $$$step[n - 1] = 0$$$ (you reach last node from last node using 0 steps). If a node that is k distance away and is at index k away from the $$$n - 1$$$th node, then it's step will be 1. And so on we will know how many steps will first node need to reach the last node and successor array will lead the way and since I have used the minimization in reaching the ultimate node, the answer will be correct, very similar to the $$$n^2$$$ Longest Increasing Subsequence solution if you want the intuition. I know it will be difficult to understand in the beginning, but try hard that's the learning curve.

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

      very intuitive? was it sarcasm or you find it that way. lol

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

        If you want the intuition, here it is. Let us say i is the outermost loop, j is inside i and k is inside j. So we will minimize the distance between j and k and how will we ensure that it is the minimum, by considering all possible paths where we go from node j to k via all possible intermediate nodes. If there was a direct connection which was the optimal path then it simply wont change because of the fact that it is the minimum. Refer to PrinceOfPersia's blog on graphs for refernce.

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

Simple solution for D using stack: 59187274

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

    Can you explain your code?

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

      Sure! It's essentially solution 1 described in the tutorial.

      Let's process the string in order. If we come across a 1, push the current index to a stack. Otherwise, we are at a zero. If the stack is not empty, then there is a 1 before us which has not been popped. In this case, we pop it from the stack. By the rules in the tutorial, a "fixed" string exists iff there exists such a 1 on the stack. Further, as described in the tutorial, we must keep all of the "fixed" strings in the final answer. Therefore, any time we pop a 1 from the stack, we "fix" that index as a 1 in the answer. The optimal answer is the one where everything else is 0.

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

For C, I just iterated over the array P and checked if the there is a direct edge between vertex P[cur] and the vertex P[cur + 2], if there is direct edge then we can not ignore P[cur + 1] vertex from the final sequence, otherwise we can ignore it ( cur — current index). I am not sure about the correctness of this approach but it passed the system tests. Can anyone find a case where this fails or is it also a correct solution ?

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

    i dont understand why 1-4 is not a good subsequence, could you help me with that? what's wrong with passing through 1−3−4

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

      1-3-4 is not a good subsequence in Test 1 because there is a direct edge from 1 to 3, so the shortest way to go from 1 to 3 does not passes through 2, which would make the given sequence P i.e 1 2 3 4, not good because the shortest path is not passing through 2 if 1-3-4 is considered good. Hence 1-3-4 is not a good subsequence.

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

    lazyneuron does this approach would also work?

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

    Your code fails on this test case.

    4
    0100
    0010
    0001
    0000
    4
    1 2 3 4

    The expected answer is of length 2, but your code outputs a sequence of length 3.

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

    afterwards,can your approach pass all the test? my approach same as yours,but i didnot write ....

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

What happened to the tags of problem C? BTW, I think dp should be added as a tag too.

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

Usually, when there are an easy and hard versions of the same problem, easy one could be solved in a way, which is not possible for the hard one. Could smb share their ideas on solving 1204D1, which will not work for 1204D2, pls?

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

11

1 2 1 2 1 2 1 2 1 2 4

problem C second test case

i got this answer for the second test case and it gives me wrong answer could someone tell me why it is wrong ?

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

I solved C with DP,it took O(n×m).

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

I have seen problem C somewhere before on Codeforces. Can't remember in which contest? Can somebody tell me?

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

Does anyone have a java solution in O(n^3 + m) because my solution TLEs on case 13.

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

      Well that sucks, looks like I drastically underestimated the power of PrintWriter vs System.out.print. I'll be sure to keep that in mind going forward. Thanks!

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

Can anyone look into my code and tell me why I am failing 6th test case in 1204C - Anna, Svyatoslav and Maps problem? I think my approach was same as in the editorial. 59175116

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

I am not getting approach for problem D. Can anyone pls explain why for input 111000 Output is 111000 And not 001000

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

I solved D using a segtree from 145E - Lucky Queries. I started with the initial string and tried setting each 1 to a 0 and seeing if any of the longest nondecreasing sequences in the nodes were affected. I could not prove it is correct; however.

code: 59190439

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

C can't be solved without using Floyd Warshall (equivalently, BFS from all vertices)

Going through the solutions of problem C, I have observed a lot of people (including me), solve the question without using Floyd Warshall and deleting vertices in a greedy manner. Although the solution passes all the system test cases, I believe it is wrong.

People have used 2 approaches for greedily deleting the vertices.

Approach 1 :: Checking every window of size 3 without modifying the array.

This approach fails for this test case.

4
0101
0010
0001
0000
4
1 2 3 4

Expected 1 3 4
Output 1 4

Approach 2 :: Checking every window of size 3 (while actually deleting elements using stack).

This approach fails for this test case.
4
0100
0010
0001
0000
4
1 2 3 4

Expected 1 4
Output 1 2 4

rotavirus Can you add these test cases to the problem ?

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

I can't understand one thing in problem C. Let's look at the third example, expected output is $$$1, 2, 3, 1, 3, 2, 1$$$. But why output $$$1, 2, 3, 3, 2, 1$$$ is wrong? It is the subsequence of $$$1, 2, 3, 1, 3, 2, 1$$$ and because we don't have loops, one of the shortests paths passing through the vertexes $$$1, 2, 3, 3, 2, 1$$$ in that order will be $$$1, 2, 3, 1, 3, 2, 1$$$.

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

    But the shortest paths of "3,3" is "3,1,3".You can't go through the points from themselves to themselves.There is no a edge from "3" to "3".

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

      Output for the task is not path in graph, it's only subsequence of given path. Because $$$3, 3$$$ is subsequence of $$$3, 1, 3$$$ and one of the shortest paths passing through $$$3, 3$$$ is $$$3, 1, 3$$$, so $$$1, 2, 3, 3, 2, 1$$$ might be answer for third example.

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

        This made me confuse also. If you read the output description, it says that continuous vertices of the answer should be different. It's weird because the statement asks for shortest answer first but not asking for this condition

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

          my common sense said that if you want to go from 3 to 3 then you may just not to move

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

        sorry for not pointing at it in the statement, but i think that the shortest path going through 3,3 is a single 3 without any moving even there are not any loops. sorry once again, i hope you liked other problems

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

      the shortest path of 3,3 is just a single 3. sorry for not pointing at it in the statement

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

        That's how skipping the main character's name could accidentally skips the common sense of the problem.

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

There is another solution for problem E that has O(n + m) time complexity and has pretty simple code. Consider for each array the following path: it starts at (0, 0) and for each element in array(in given order) goes right by 1 if current element if equal to 1 and goes 1 to top otherwise (notice every path ends at (n, m)). Then it's rather obvious that if this path intersects line y=x-k, array's maximum prefix sum is more or equal to k(in other words it has prefix with sum equals to k). Now let's notice that answer is equal to sum of amounts of pathes intersecting line y=x-k for each k=1...n. It's correct because each path(and therefore each array) will be counted as many times as its maximum prefix sum equals to. Now the task is following: count how many paths go from (0, 0) to (n, m) that moves only 1 right and 1 top and intersects line y=x-k. There are 2 different cases:

  1. (n, m) lies below or on the line => each path from (0, 0) to (n, m) intersects the line, therefore answer is C(n + m, n)

  2. (n, m) lies above the line. To count this paths let's make the following trick: let's find the first intersection point and reflect rest of the path from the line. Than we'll get path from (0, 0) to (m + k, n — k). Notice that each path from (0, 0) to (m + k, n — k) intersects the line, so the operation may be reversed for each such path, therefore there is a bijection between paths to (n, m) intersecting the line and paths to (m + k, n — k), so the answer for this case is C(n + m, m + k)

Finally we got solution that takes O(n + m) time to precalc some features to compute C(n, k) in O(1) and O(n) time to iterate through lines of type y=x-k for k=1..n and use formulas explained above.

PS. Sorry for my English

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

    Sorry, but Can you explain two cases more clearly ? I'm quite confused when reading to that ! Thank you so much.

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится
      1. Case 1: (n, m) is below or on the line, coz y=x-k is above (0, 0), so (n. m) and (0, 0) lies on different sides of the line, any path that connects these two points must intersect y=x-k.

      2. Case 2: (n, m) is above the line. Then we reflect the (n, m) with regard to y=x-k, it becomes (m+k, n-k), and it's below the line. After we draw a path between (0, 0) to (m+k, n-k), we find the first intersection point between the path and y=x-k, call it q. Then we reflect the path between q and (m+k, n-k), the end point becomes (n, m) again and the path intersects y=x-k.

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

For the A problem.I want to ask a simple question.The largest upper limit 2^100 we should input is decimalism or binary?

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

[delete]

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

I got TLE on problem 1204C - Anna, Svyatoslav and Maps during contest but got accepted when I submitted the same code with C++.

TLE with C: 59165015

AC with C++: 59196569

What is the subtle difference between C and C++? I am really puzzled :(

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

for C i think the systests is too weak, many wrong program can passed all tests for example only one path

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

Isn't the logic for k[x][y] written in reverse order for the case x <= y? If we place x-1 ones and y minus ones in the prefix part, that would sum up to x - 1 - y, thereafter placing a 1 at the end wouldn't matter, as it would sum up to x - y, which is <= 0. Similarly, if we place x ones and y-1 minus ones in the prefix, they would sum up to x - y + 1, and placing a -1 at the end wouldn't matter, because it sums up to x - y, which again is <=0. Although the recurrence is correct, the intuition behind it seems a little incorrect to me. Correct me, if I am wrong rotavirus

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

I think solution 1204A is not correct. Because if s=10000 which is 16 in Decimal form, then Expected output is 3. But According to the given solution, it gives 2 because of l=5.

Anyone can help me to solve this problem.

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

    You need to find the number of trains departed strictly before time s. That's why the value to be considered is 15, not 16.

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

In the problem 1204A,how does the author conclude "Basically, the problem asks you to count ⌈log4s⌉". Can someone clarify?

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

    By definiton $$$\lceil \log_4 s \rceil$$$ is the number of distinct powers of 4 strictly lesser than $$$s$$$ with nonnegative integer exponents $$$4^0, 4^1, 4^2$$$ and so on.

    Problem A asks you to count the number of trains which arrive before given time.

    Time is represented as a binary number $$$s$$$ and $$$i$$$-th train arrives on $$$4^i$$$-th moment, so every power of 4 strictly lesser than $$$s$$$ corresponds to exactly one missed train and every missed train corresponds to exactly one power of 4 strictly lesser than $$$s$$$, so by counting all powers of 4 strictly lesser than $$$s$$$, you equivalently count all trains, that arrive before time $$$s$$$.

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

If we change any 1 to 0 and the longest nondecreasing sequence of the whole string remains the same, then we are able to change it to 0.

can somebody prove this for problem D please??

Also this approach says it is a sufficient condition. and does not talk about its necessity.

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

i am not getting the rules of problem D: in test case 4: 0111001100111011101000 why is the output 0011001100001011101000 and not 0001000100001000101000?

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

    in test case first four symbols 1100 (length of good subsequense — 2) in your solution there is a 0001 (length of good subsequense — 3)

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

      oh okay thank you but why did you skip the first 2 0s in there solution 0011001100001011101000 but did not skip them in my answer 0001000100001000101000 aren't we supposed to compare both using same left and right?

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

        Yes, Seville got it wrong. It should be 0100 instead of 0001 as the substring from your solution. But also, we are comparing the test case input with your solution, not the test case output with your solution, which is what seems you have just done.

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

Can someone please explain what do in problem C after obtaining the shortest path matrix using Floyd-Warshall?

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

    firstly — last_vertex = P[0]

    how to check if we can add vertex P[U] to answer?

    if distance between P[U] and last vertex in path == distance in graph (which getted by floyd) we can see next vertex (U++)

    So, when it become false -> it means we need add P[U — 1] in answer because there is a last good vertex. now last vertex = P[U — 1], and we can check vertexes, while there are vertexes.

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

In the solution for D could someone please prove the last three "obvious" observations? I.e.:

  1. if a string p is fixed, then the string 1p0 is fixed;
  2. each fixed string contains the same number of ones and zeroes;
  3. the length of the longest nondecreasing subsequence for any fixed string is equal to the half of its length, which can be obtained by taking all zeroes or all ones.

And do these observations provide a complete description of all fixed strings?

  • »
    »
    5 лет назад, # ^ |
    Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится
    1. Let the string "1p0" be not fixed. Then there is a string "s" that matches the first condition of the statement. Obviously the string "s" looks like this ?p?, because otherwise the string "p" is not fixed. But the first character of the string "s" cannot be 0 as well as the last character cannot be 1, because otherwise length of the longest nondecreasing sequence of the whole string will be different.
    2. Now consider all the strings formed in this way. They meet this condition "each fixed string contains the same number of ones and zeroes".
    3. Note that these strings can be compared with the correct bracket sequences. Here we need to take first a few closing brackets (x) and then a few opening (y). But each of these brackets has a pair, so x + x + y + y <= n, x + y <= n / 2.
    4. If you delete all the lines obtained in this way, then a few 0 will remain, and then a few 1, because after 1 only 1 can go.
    5. We can replace all the remaining 1 with 0, since: 1) a fixed line is a correct bracket sequence, therefore the number of 1 on the prefix is ​​greater (or equal) than 0, and vice versa on the suffix. 2) as the largest non-decreasing sequence of the entire fixed row, you can choose all 0 or all 1.
    6. The remaining 1 cannot be replaced since they are part of a fixed substring.
    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится
      1. But if a longest non-decreasing subsequence of "1p0" consists of only 1s and there are more 1s than 0s in "1p0", then the length of a longest non-decreasing subsequence of "0p0" is the same as of "1p0". Analogically, I don't see why the last character of "s" cannot be 1.

      2. OK, strings formed in this way meet this condition. But how do we know that ALL fixed strings meet this condition?

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

        To solve the problem, it is enough for us to delete only the lines formed in this way. It's possible that these are not all fixed strings.

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

          Yeah, I think the solution should say that we define recursively a family of fixed strings, which could be viewed as all the correct bracketings, and make some observations about this family. Then at the end I think we could note that these are in fact all the fixed strings because for any string that is not in this family we could remove all strings from this family from it and obtain something where we can change something.

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

In C, why we are not checking the vertices in same path length. Like for the test-case: 4
0101
0010
0000
0010
3
1 2 3

Output: 2
1 3
According to what given in editorial But 1 4 3 is also the shortest path from 1 to 3. In this case, the answer should be 3
1 2 3

Correct me, if I'm understanding this problem wrong?

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

" k[x][y]=k[x−1][y]+k[x][y−1], because if we consider any array consisting of x−1 ones and y minus ones which maximal prefix sum is 0 then adding a minus one to the end leaves it equal to 0. " Here if we take x-1 one and y minus one then why we does not take a one at the end as there is now x-1 one? editorial says that we will take one minus one at the end but here already y minus one exist.

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

rotavirus Can you share your code for Problem D2 Solution 2 ?

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

sorry rotavirus but I think there was a mistake in the explanation of problem E in the sentence "because if we consider any array consisting of x−1 ones and y minus ones which maximal prefix sum is 0 then adding a minus one to the end leaves it equal to 0; also if we consider any array consisting of x ones and y−1 minus ones which maximal prefix sum is 0 then adding a one to the end leaves it equal to 0, because x≤y".

Shouldn't it be "because if we consider any array consisting of x−1 ones and y minus ones which maximal prefix sum is 0 then adding a one to the end ... of x ones and y−1 minus ones which maximal prefix sum is 0 then adding a minus one ..." ?

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

sorry ,I am a rookie,and I can't understand the problem C still now , Can a friend tell me the problem C means ? After using the floyd algorithm , I dont konw how to do in the next step , why should delete the 2 in the first example? In this answer,1->2->4 sequence,the vex2 is not link the vex4,thats why? thanks in advance.....

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

    basicly we only removing numbers from P that obviously will be passed even we didnt mention it, at the frist example the path is 1 2 3 4, if our answer is 1 4 we will missed vertex 2 bcs the route that we take would be 1 3 4 (we need to visit every vertex on the given path on exact same order). so if our answer is 1 2 4 ( we walk from 1 to 2, then from vertex 2 we only have 1 way to go and that is 3, so we dont have to include 3 on our answer, at vertex 3 we continue going to vertex 4 (because of the path said so)). :D

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

      thanks for your reply.I have read the problem carefully ago,now I have already know what the problem means,the given sequence A is a shortest path in some "good" sequence,you have to find a sequence B when visit all the elements of the B,the given sequence A will be the shortest path,after using floyd algorithm,you can get correct answer by greedy algorithm.

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

1204D2 - Kirk and a Binary String (hard version) I have a problem in D.. -> it is confirmed that 10 is fixed, but we can change 11 to 01 as it will not affect the length of nondecreasing series and also we want the maximum possible number of 0 in our output. So thus according to me, if the input string is 001110110, then answer can be 000010010. It follows all the necessary condition. Also if the last two digits are is 01, then it can be changed to 00. So the answer to 0111001100111011101000 can be 0001000100001000101000. Please someone can tell me if I am wrong.

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

    s=0111001100111011101000
    t=0001000100001000101000
    l=3,r=6
    s[3:6] is 1100, its longest nondecreasing subsequence is 11 or 00
    t[3:6] is 0100, its longest nondecreasing subsequence is 000

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

1204C - Anna, Svyatoslav and Maps My answer to this problem to test case -

3

011

101

110

7

1 2 3 1 3 2 1

is coming as —

5

1 3 1 2 1

I can't understand where it is wrong.?

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

    All vertices has two-side relation.

    from 1 to 3 shortest path = 1 (from 1 to 3 directly), and you can't remove 2 from 1-2-3, because you can only delete when 1-x-3 shortest path from 1 to 3.

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

In question D's second solution's statement, "If we change any 1 to 0 and the longest nondecreasing sequence of the whole string remains the same, then we are able to change it to 0.", the word sequence is used, while looking at the DP solution it seems like we are talking about largest non-decreasing subsequence . Can anyone please clarify and explain what it is all about?

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

There is another solution for problem D. Let's go from end of string s to begin and calculate number of ones and longest nondecreasing sequence of substring s[i + 1...n]. If number of ones is less then longest nondecreasing sequence of substring s[i + 1...n] and s[i] = 1, then ans[i] = 1. Otherwise ans[i] = 0. But I don't understand why is it correct.

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

Questions to D2. Why is it true if p and q are fixed, pq also should be fixed?

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

In problem C description it says the graph is directed unweighted without loops but in sample test 3

3 011 101 110

doesnt this refers to graph image link which is a graph with loop

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

due to the binary string, we can calculate LIS in linear time.

You can look my submission

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

Can someone explain why this code I found works 59246266? It seems to be reusing the dp array (where dp[n][m] stores the number of ways for the maximum prefix sum to be 0 and m = # of 1's and n = # of -1's), but it swaps the order of parameters. Below is the part where it's confusing me. It seems to be that there's some kind of correspondence between dp[m][n — 1] and the ways to make a sum of n — m. The second part of the calculation of res makes sense, since you append something with maximal sum of 0.

for(int i=1;i<=N;i++){
    for(int j=0;j<=min(i-1,M);j++){
      ll res=dp[j][i-1]*dp[N-i][M-j]%MOD;
      res=res*(i-j)%MOD;
      add(ans,res);
    }
  }
»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Could someone give me the code of the first solution for problem D2?

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

How can I get obivous like D? I've saw some problems, but I never solved a problem.

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

For problem D why do we just take the LIS of the whole string and check if it changes or not? Isnt there a case where for some l to r the LIS changes but for the whole string the LIS remains same? Can someone please prove it?

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

I found that we can solve F without dynamic programming,mathematics is just enough.

Here's my submission.

Let $$$sum(p)=\sum_{i=1}^p a_i$$$ .

We can try to find the number of sequences that $$$f(a)=x$$$ and,if $$$k$$$ is the first position that $$$sum(k)=x$$$ ,the number of $$$1$$$ from $$$a_1$$$ to $$$a_k$$$ is $$$y$$$ .Let the number be $$$tmp$$$ ,then, $$$ans+=x\cdot tmp$$$ .

For $$$a_1$$$ to $$$a_{k-1}$$$ ,the number of $$$1$$$ is $$$y-1$$$ ,and the number of $$$-1$$$ is $$$y-x$$$ .Each prefix sum from $$$1$$$ to $$$k-1$$$ must be less than x,otherwise $$$k$$$ is not the first position that $$$sum(k)=x$$$ .Similarly to Catalans,the number of this part is $$$t_1={2y-x-1\choose y-1}-{2y-x-1\choose y-x-1}$$$ .

$$$a_k$$$ must be $$$1$$$ .

For $$$a_{k+1}$$$ to $$$a_n$$$ ,the number of $$$1$$$ is $$$n-y$$$ ,and the number of $$$1$$$ is $$$m+x-y$$$ .And $$$sum(p)-sum(k)$$$ cannot be positive,otherwise $$$f(a)>x$$$ .Also similarly to Catalans,the number of this part is $$$t_2={n+m+x-2y\choose n-y}-{n+m+x-2y\choose m+x-y+1}$$$ .

Then $$$tmp=t_1\cdot t_2$$$ .The solution above works in $$$O(n^2)$$$ time.

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

I did same as told in editorial of Problem C , still my solution is failing on test 6. Can anyone help me in debugging this . https://codeforces.com/contest/1204/submission/61395687

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

Problem C is exactly same as SECRETMI from codechef

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

Problem $$$E$$$ alternative solution:

$$$dp[n][m] = dp[n - 1][m] + dp[n][m - 1]$$$ whenever $$$n \le m$$$.

If $$$n > m$$$, then we can let $$$g[n][m] = dp[n][m] - dp[n - 1][m] - dp[n][m - 1]$$$.

Then, via observation, if $$$n > m$$$, $$$dp[n][m] = dp[n - 1][m] + dp[n][m - 1] + g[n - 1][m] + g[n][m - 1]$$$.

Base cases: if $$$n = 0$$$ or $$$n = 1$$$, answer is $$$n$$$. If $$$m = 0$$$, answer is $$$n$$$, and if $$$m = 1$$$, answer is $$$n^2$$$.