mesanu's blog

By mesanu, history, 16 months ago, In English

Thank you for participating!

1807A - Плюс или минус

Idea: flamestorm

Tutorial
Solution

1807B - Хватайте конфеты

Idea: mesanu

Tutorial
Solution

1807C - Найти и заменить

Idea: flamestorm

Tutorial
Solution

1807D - Нечётные запросы

Idea: SlavicG

Tutorial
Solution

1807E - Интервью

Idea: SlavicG

Tutorial
Solution

1807F - Мяч-попрыгунчик

Idea: mesanu

Tutorial
Solution

1807G1 - Последовательное сложение (простая версия)

Idea: flamestorm

Tutorial
Solution

1807G2 - Последовательное сложение (сложная версия)

Idea: flamestorm

Tutorial
Solution
  • Vote: I like it
  • +20
  • Vote: I do not like it

| Write comment?
»
16 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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?

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

    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

    • »
      »
      »
      16 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      because you're only a specialist

    • »
      »
      »
      15 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

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

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

    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.

    • »
      »
      »
      15 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

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

      • »
        »
        »
        »
        15 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    16 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
»
16 months ago, # |
Rev. 2   Vote: I like it -9 Vote: I do not like it

How did the next direction calculated in the problem F?

  • »
    »
    16 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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

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

Yes Or No Forces

»
16 months ago, # |
  Vote: I like it +2 Vote: I do not like it

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

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

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

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

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

»
16 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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?

  • »
    »
    16 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it
    Spoiler
    • »
      »
      »
      16 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

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

      • »
        »
        »
        »
        16 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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.

        • »
          »
          »
          »
          »
          16 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

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

          • »
            »
            »
            »
            »
            »
            16 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

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

            • »
              »
              »
              »
              »
              »
              »
              16 months ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              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.

              • »
                »
                »
                »
                »
                »
                »
                »
                16 months ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

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

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

              Could you explain the solution for the problem 1807G1 - Последовательное сложение (простая версия). 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 :(

              • »
                »
                »
                »
                »
                »
                »
                »
                13 months ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                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!

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  13 months ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

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

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  13 months ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  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.

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

      from cses problemset

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

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

»
16 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
16 months ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

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.

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

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

»
16 months ago, # |
  Vote: I like it +1 Vote: I do not like it

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

»
16 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

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

    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)

»
16 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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?

  • »
    »
    16 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    "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

»
15 months ago, # |
  Vote: I like it 0 Vote: I do not like it

1807D - Нечётные запросы 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")
  • »
    »
    14 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    12 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

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

count odd and even num and check it