Kerolloz's blog

By Kerolloz, history, 5 years ago, In English

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.

  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +6 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    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 ^_^