### mesanu's blog

By mesanu, history, 16 months ago,

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
• +20

| Write comment?
 » 16 months ago, # | ← 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?
•  » » 16 months ago, # ^ | ← 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
•  » » » 16 months ago, # ^ |   0 because you're only a specialist
•  » » » 15 months ago, # ^ |   0 Shouldn't be 4*n*m states cause tle according to constraints?
•  » » 16 months ago, # ^ |   +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.
•  » » » 15 months ago, # ^ |   0 Shouldn't be 4*n*m states cause tle according to constraints?
•  » » » » 15 months ago, # ^ |   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).
•  » » » » » 10 months ago, # ^ |   0 Great
•  » » 16 months ago, # ^ |   0 i just wrote a long one bfs https://codeforces.com/contest/1807/submission/198385698
 » 16 months ago, # | ← Rev. 2 →   -9 How did the next direction calculated in the problem F?
•  » » 16 months ago, # ^ |   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: It hits the corner, $r=n$ and $c=m$: Change direction to $UL$. It hits the bottom first, $r=n$ and $c  » 16 months ago, # | +4 Yes Or No Forces  » 16 months ago, # | +2 Another opportunity to feel ashamed....!!!  » 16 months ago, # | +4 What is the proof for$\text{states} \leq 2mn$in problem F? •  » » 16 months ago, # ^ | +4 Think of the grid as a chessboard — you always stay on squares of the same colour.  » 16 months ago, # | 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? •  » » 16 months ago, # ^ | ← Rev. 2 → 0 SpoilerPractice is the key •  » » » 16 months ago, # ^ | 0 But did you prove the solution using induction? Or did you rely on intuition? •  » » » » 16 months ago, # ^ | 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. •  » » » » » 16 months ago, # ^ | 0 Wow man, I could not prove the solution even after seeing the AC solutions. I really need to practice! •  » » » » » » 16 months ago, # ^ | 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.) •  » » » » » » » 16 months ago, # ^ | 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. •  » » » » » » » » 16 months ago, # ^ | 0 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 → 0 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 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, # ^ | 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! •  » » » » » » » » » 13 months ago, # ^ | 0 Could you explain why you are using that presum variable?? •  » » » » » » » » » 13 months ago, # ^ | 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. •  » » » 4 months ago, # ^ | ← Rev. 2 → 0 from cses problemset Spoileralmost similar question •  » » 16 months ago, # ^ | ← Rev. 2 → 0 I came up to the solution remembering almost the same problem from CSES problem set. So practice indeed helps.  » 16 months ago, # | 0 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, # ^ | 0 Easier implementation.  » 16 months ago, # | ← 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.  » 16 months ago, # | +4 I solved F in O(n+m) xD!  » 16 months ago, # | +1 I am very excited for the next div4 contest to be unrated for me.  » 16 months ago, # | 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. •  » » 15 months ago, # ^ | +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 * nso the finite sum is between n operations and 2 * n operations, hence its O(n)  » 16 months ago, # | 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? •  » » 16 months ago, # ^ | 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>> v(n, vector> (m, vector(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, # | 0 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, # ^ | 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)$.  » 12 months ago, # | 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 ?? •  » » 12 months ago, # ^ | 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\$.
 » 6 months ago, # | ← Rev. 2 →   0 count odd and even num and check it