mesanu's blog

By mesanu, history, 2 months ago, In English

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
  • Vote: I like it
  • +20
  • Vote: I do not like it

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

  • »
    »
    2 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

  • »
    »
    2 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.

    • »
      »
      »
      2 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?

      • »
        »
        »
        »
        2 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).

  • »
    »
    2 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
»
2 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?

  • »
    »
    2 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

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

Yes Or No Forces

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

My first fully solved contest

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

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

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

  • »
    »
    2 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.

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

      What about points where two lines (or edges) intersect? Those points can have all 4 directions?

      Edit: Okay it works out. We have $$$\frac{mn}{2}$$$ white/black squares. If we start at a colour we stay on it. Each cell at max can have 4 directions so it becomes $$$4 \cdot \frac{mn}{2}=2mn$$$

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

      How to solve in 2 mn states

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

  • »
    »
    2 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it
    Spoiler
    • »
      »
      »
      2 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?

      • »
        »
        »
        »
        2 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.

        • »
          »
          »
          »
          »
          2 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!

          • »
            »
            »
            »
            »
            »
            2 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.)

            • »
              »
              »
              »
              »
              »
              »
              2 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.

              • »
                »
                »
                »
                »
                »
                »
                »
                2 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.

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

                hey there are 2 solutions of this problem, the one given in tutorial is relatively tough to understand. u can see this solution [https://codeforces.com/contest/1807/submission/198847704]

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

        Well, after practicing reasonably you will start to get the intuition to approach a problem, that intuition can be wrong or right. To check whether you are on right track you should think about the idea of intuition and prove it mathematically or intuitively. Coming back to your question. Yes, we rely on our intuition. But along with that, we prove our solution too.

  • »
    »
    2 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.

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

i got it.

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

Prefix-Sum forces

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

I'd say this was harder than an average div 4

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

how can we do the G1 using the memoization technique in dp instead of tabulation

I am not able to write the memoization code

Can anyone help?

»
2 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

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

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

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

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

In the D problem, can anybody explain to me what is the 'prefix sums technique' and how is it being implemented here?

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

    The prefix sums allows you to build an array $$$\texttt{pre}$$$ where $$$\texttt{pre}[i] = \sum_{j=0}^{i} a[j]$$$. In the problem, you are given a segment $$$[l, k]$$$. We want to subtract out $$$\sum_{j=l}^{r} a[j]$$$ and add $$$(r - l + 1) \cdot k$$$. To find the amount we want to subtract, observe that it is simply $$$\texttt{pre}[r] - \texttt{pre}[l-1]$$$.

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

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

  • »
    »
    2 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)

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

  • »
    »
    2 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

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

Can anyone tell me why I'm getting an idleness error in test 3? PROBLEM LINK: https://codeforces.com/contest/1807/problem/E

include <bits/stdc++.h>

using namespace std;

define ll long long

void solve() { int test; cin >> test; while (test--) { int n; cin >> n; vector v(n + 1); for (int i = 1; i <= n; i++) { cin >> v[i]; } int x = 30; int start = 1, end = (n + 1) / 2; if (n == 1) { cout << "! 1" << "\n" << flush; continue; } while (x--) {

int sum = 0;
        cout << "? " << (end - start) + 1;
        for (int i = start; i <= end; i++)
        {
            sum += v[i];
            cout << " " << i;
        }
        cout << "\n"
             << flush;

        int ask;
        cin >> ask;
        // cout << "SUM " << sum << endl;
        if (ask == sum)
        {
            start = end + 1;
            end = n;
        }
        else
        {
            // n = end;
            end -= ((end - start) + 1) / 2;
        }

        if (start == end)
        {
            cout << "? 1 " << start << "\n"
                 << flush;

            cin >> ask;
            if (ask == v[start])
            {
                cout << "! " << start + 1 << "\n"
                     << flush;
            }
            else
            {
                cout << "! " << start << "\n"
                     << flush;
            }
            break;
        }
    }

} // while end

} // solve end int main() {

// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
solve();

return 0;

}

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

Can anyone tell what's the mistake in my code.Problem G1 Test case 2 : wrong answer expected NO, found YES [265th token]

submission id: https://codeforces.com/problemset/submission/1807/199620687

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

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")
  • »
    »
    10 days 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)$$$.