Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя Siddharth.Pathak

Автор Siddharth.Pathak, 5 часов назад, По-английски

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.

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

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

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

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

    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?

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

      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

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

        Ohh, thanks buddy..