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

Автор Kerolloz, история, 5 лет назад, По-английски

I have been trying to solve Div2-767-A!
But every time I try to submit my solution it doesn't even pass the sample test cases -_- "WA on test 1".
My code doesn't print the last number of the output however it works fine when I tried it.
I tried my code on my PC it works properly, but I don't know what's the problem when I try to submit it.
Here is my submission link: submission .
Any help would be appreciated! Thanks in advance.

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

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

You are iterating over the set and erasing elements from while doing that. erase invalidates iterators and you can no longer use them to continue the iteration over the container.

For more information:

https://www.geeksforgeeks.org/iterator-invalidation-cpp/

https://en.cppreference.com/w/cpp/container/set/erase

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

    Thank you very much that solved the issue. You are right. I didn't know the behavior of the iterator in the case of erasing. Editing the code from st.erase(it); it++; to it = st.erase(it); fixed it and I got AC finally ^_^