Блог пользователя FlyingElephant

Автор FlyingElephant, история, 16 месяцев назад, По-английски

I was solving this problem. But I keep getting RTE on test2 and I dont understand why.

My idea is selecting the smallest 2*n elements by putting them into a set. Then I use two pointers on the array sorted by coordinate component in order to get the system of nested segments. No wonder what goes wrong with that.

SUBMISSION

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

»
16 месяцев назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

Let's analize this testcase:

1
1 3
1 -5
2 -6
3 -5

Your multiset will look like this s = {-6, -5}
For i = 0 and j = 2, both weights are -5, so you are going in the first if and erasing -5 and then trying to erase it again, but there was only one -5 in the multiset.

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

    but wait.. in the multiset there must be 2 times -5. right?

    • »
      »
      »
      16 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится
      if((ll) s.size() > 2*n){
      	ll big = *(--s.end());
      	s.erase(s.find(big));
      }
      

      You are deleting the biggest value in multiset when it size is bigger than 2 * n, so no, there is only one -5 at the end in it.