Please read the new rule regarding the restriction on the use of AI tools. ×

Siddharth.Pathak's blog

By Siddharth.Pathak, 2 hours ago, In English

Hi guys,

In the Problem B of Round 975 (Div. 2), I was going through an irritating problem in my code, that I did not rectify till now. I thought to solve the problem by solving two quadratic equations as follows:

$$$p(n-p+1)-1 = k \quad \& \quad p(n-p) = k$$$

The first one is to find if any array elements have the same count as $$$k$$$ and the second one to find the elements not in the array but are part of $$$k$$$ intervals. Here is my submission: 283252905

The problem was not though and can be solved in other ways too (as one is in the tutorial), but I am just curious to find my mistake, I will be very glad if someone can help me.

»
113 minutes ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

Nah, if you do like that, your code would absolutely get TLE (or in this case is WA).

So here is the basic ideas: you are given an array $$$a$$$ with $$$n$$$ elements then the element $$$a_i$$$ will be contained by exactly $$$(n-1-i) \times i+n-1$$$ segments and all the elements between $$$a_i$$$ and $$$a_{i+1}$$$ will be contained by exactly $$$(n-i) \times i$$$ segments. Also, in this case you only need to count how many points that contained by exactly $$$k$$$ segments so you can use map for the efficiency

For example, The first element $$$a[0]$$$ will be contained by exactly $$$n-1$$$ segments so m[n-1]++. All the elements in $$$a[0]$$$ and $$$a[1]$$$ will be contained by exactly $$$(n-1)*1$$$ segments so m[(n-1)*1] += (a[1] - a[0] - 1). Then the element $$$a[1]$$$ will be contained by exactly $$$(n-2)*1 + n-1$$$ so m[(n-2)*1 + n-1]++, etc.

m[n-1]++;
for (int i = 1; i < n; ++i) {
    m[(n-i)*i] += (x[i] - x[i-1] - 1);
    m[(n-1-i)*i+n-1]++;
}

And since there are only $$$10^5$$$ elements so the size of the map $$$m$$$ can only be $$$\leq 2*n$$$. Therefore, if $$$k$$$ is exist in the map then the answer is $$$m[k]$$$ otherwise, $$$0$$$

You can refer to my code if needed (it is cleaned and easy to visualize) 283198814

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

    Thanks BinhPhamT, for the explanation. I understand this logic, in fact, it is the best way to do this. Can you also please tell me the explicit reason for WA/TLE, in my code?

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

      Oh hey I'm sorry I don't think it's tle, but I know why your code wrong it's because you set p1 and p2 in your code to int which may be overflowed you may have to change it to long long to get accepted