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

Автор mesanu, история, 14 месяцев назад, По-английски

Thank you for participating!

1807A - Plus or Minus

Idea: flamestorm

Tutorial
Solution

1807B - Grab the Candies

Idea: mesanu

Tutorial
Solution

1807C - Find and Replace

Idea: flamestorm

Tutorial
Solution

1807D - Odd Queries

Idea: SlavicG

Tutorial
Solution

1807E - Interview

Idea: SlavicG

Tutorial
Solution

1807F - Bouncy Ball

Idea: mesanu

Tutorial
Solution

1807G1 - Subsequence Addition (Easy Version)

Idea: flamestorm

Tutorial
Solution

1807G2 - Subsequence Addition (Hard Version)

Idea: flamestorm

Tutorial
Solution
Разбор задач Codeforces Round 859 (Div. 4)
  • Проголосовать: нравится
  • +20
  • Проголосовать: не нравится

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

It will be good if there exists some more explanation of F. Bouncy Ball Problem.

I am still not able to simulate the movement of ball.

Can anybody else explain it to me?

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

    For changing the direction, you just have to make sure that if the ball continues going in this certain direction on the x axis or y axis, it WILL go out of the boundaries (for example the column index is m and the second character is 'R'). In this case you just flip 'R' to 'L' or vice versa as needed, same for 'D' and 'U'. This automatically handles the corner case too.

    Then you make the ball go in the updated direction until it either reaches the desired cell, or starts the loop all over again (in which case the answer will be -1).

    Submission: 198207442

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

    If you hit the right wall, Definitely you will change direction R to L but to know you will move U or D you must know where you are coming from if you coming from U you will change it to D and vice versa.

    And do this if you hit the left, up, or down wall.

    If you hit any vertex you will change both of two directions.

    I hope you understand xD.

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

      Shouldn't be 4*n*m states cause tle according to constraints?

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

        No, read the input again and you will notice that he is write (It is guaranteed that the sum of n * m over all test cases does not exceed 5 * 10 ^ 4).

        So, 4 * n * m = 20 * 10 ^ 4 (in the worst case).

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

How did the next direction calculated in the problem F?

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

    For each direction, think about how you can transition to other directions.

    Let's say the ball is at $$$(r,c)$$$ with direction $$$DR$$$. There are 3 possibilities:

    1. It hits the corner, $$$r=n$$$ and $$$c=m$$$: Change direction to $$$UL$$$.
    2. It hits the bottom first, $$$r=n$$$ and $$$c<m$$$: Change direction to $$$UR$$$.
    3. It hits the side first, $$$r<n$$$ and $$$c=m$$$: Change direction to $$$DL$$$.

    Try this for each direction.

    Submission: 198396729

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

Yes Or No Forces

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

Another opportunity to feel ashamed....!!!

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

What is the proof for $$$\text{states} \leq 2mn$$$ in problem F?

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

    Think of the grid as a chessboard — you always stay on squares of the same colour.

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

How do you come up with the solution of G2? Can some experienced people tell me? I tried a lot to solve that problem but was not able to. I read the codes of some AC solutions after the contest but was not able to understand why that worked. Only after reading the editorial, I understood the solution. The people who solved it, did you prove the solution using induction during the contest? How do I develop this skill?

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

      But did you prove the solution using induction? Or did you rely on intuition?

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

        yes after submitting the solution but its bad practice to rely on just intuition I would recommend you to prove it as well it improves accuracy and build foundation.

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

          Wow man, I could not prove the solution even after seeing the AC solutions. I really need to practice!

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

            G2 is simply a greedy problem, and you will be able to detect it after working on enough similar problems. It's a matter of familiarity, while some describe it as intuition. (After all, G is a relatively basic problem.)

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

              Man, yesterday the whole night and even today morning I was still thinking about the solution of this problem. It's marvelous that people find it basic. I need to buckle up and practice.

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

                Yeah I understand you. We (most of us) all came from that phase. Just keep practicing and you will find yourself improved.

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

              Could you explain the solution for the problem 1807G1 - Subsequence Addition (Easy Version). I tried nearly for half a day and not able to solve it. Could you please tell the step by step solution from the recursion to dp?

              My code :

              bool flag = true;
              sort(arr.begin(), arr.end());
              vector<int> dp(5001, 0);
              dp[1] = 1;
              if(arr[0] != 1) {
                      flag = false;
              } 
              if(flag) {
              	for(int i = 1; i < n; i++) {
              	        if(dp[arr[i]] != 1) {
              			flag = false;
              			break;
              		}
              		//If arr[i] is possible, then all dp[j] which is true, then dp[j + arr[i]]
              		//should also be possible ryt?
              		for(int j = 0; j <= 5000; j++) {
              			if(dp[j]) {
              				if(j + arr[i] <= 5000) dp[j + arr[i]] = 1;
              			}
                              }
              	}
              }
              

              I am getting wrong answer :(

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

                I just worked on your method. The problem lies in the j loop: the bound should be the prefix sum instead of 5000. Check this out: https://codeforces.com/contest/1807/submission/209104779 Feel free to ask me if you have further questions!

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

                  Could you explain why you are using that presum variable??

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

                  Let's say you got a=[1,1,2,5]. In your implementation, when visiting a[1], when j=1, dp[2] is set to 1; when j=2, dp[3] is set to 1, and so on. Finally, dp[2..5000] is all set to 1, so when you visit a[3] then, you will get dp[a[3]] = 1, but actually you cannot get 5 from the subarray [1,1,2], resulting in wa.

                  So instead of iterating j from 1 to 5000, you should iterate it from 1 to the prefix sum, so that it will not set values you cannot achieve to 1.

                  P.S. Whenever your feel confusing, you can print some variables in the process to check if your code acts as you expected.

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

                  Thankyou so much for you time and explanation, it helped me to understand the problem that I had doing.

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

      from cses problemset

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

    I came up to the solution remembering almost the same problem from CSES problem set. So practice indeed helps.

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

problem F why they set vis[n][m][4], i think the ball only vis a pair (x, y) at most 2 times

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

Can any one tell me why my solution 198576200 got tle on test 23. Why not this submission got tle 198142369. I just wanted to know the reason. Thanks in Advance.

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

I solved F in O(n+m) xD!

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

I am very excited for the next div4 contest to be unrated for me.

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

This might be a dumb doubt, but shouldn't the time complexity of E be $$$O(n*log(n))$$$? I mean we are also printing numbers in each query.

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

    replying to a 9 days old comment, but here is your answer.

    Its actually O(n) because you are printing n numbers at first, then n/2, then n/4 and so on....

    and we know n + n/2 + n/4 + .... (infinite sum) = 2 * n

    so the finite sum is between n operations and 2 * n operations, hence its O(n)

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

For problem F, I didn't consider the approach of creating a matrix, such as bool vis[n][m][4] from solution, because I thought it would go over 1GB of memory (25000x25000x4 x 1 byte).

Since this wasn't the case, can someone point me out why?

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

    "It is guaranteed that the sum of $$$n*m$$$ over all test cases does not exceed $$$5*10^4$$$"

    So you can conclude that $$$n*m$$$ is at most $$$5*10^4$$$. You can create that matrix accordingly with the input, vector<vector<vector<bool>>> v(n, vector<vector<bool>> (m, vector<bool>(4, false)));

    I don't know if this code above is the best way to do it, but It seems to work good

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

1807D - Odd Queries Getting TLE

for _ in range(int(input())):
    n, q = map(int, input().split())
    a = [int(x) for x in input().split()]
    sum_of_a = sum(a)
    for _ in range(q):
        l, r, k = map(int, input().split())
        ans = sum_of_a - sum(a[l-1:r]) + ((r - l + 1) * k)
        print("NO" if (ans % 2 == 0) else "YES")
  • »
    »
    11 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    You are summing the values in the specified range for each query, so your runtime is $$$O(nq)$$$. You can instead use a prefix sum to get the sum of values in any range of the array in $$$O(1)$$$.

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

In the problem E Why we are subtracted pref[mid]-pref[l-1]

Instead of this why we can't use only pref[mid] ??? Can anybody explain to me ??

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

    Because as you binary search, the value of $$$l$$$ changes, so you are not always summing from the start of the array. You want to compute the sum from index $$$l$$$ to index $$$r$$$ quickly so you can use the prefix sum array since $$$\sum_{i=l}^{r} A_i = \sum_{i=0}^{r} A_i - \sum_{i=0}^{l-1} A_i$$$.

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

count odd and even num and check it