I was solving the problem https://codeforces.com/contest/1814/problem/C using C++ maps was getting wrong answer on Codeforces submission , was getting the correct answer by compiling and running the same code on the sublime text. I tried Custom Invocation , still was getting wrong answer . I solved the same problem with the same logic using vectors of pair and pair and got accepted solution . I am unable to understand why the solution using maps failed. Problem Link :- https://codeforces.com/contest/1814/problem/C` Submitted Solution using Maps in C++ :- https://codeforces.com/contest/1814/submission/223668739 Accepted Solution using vector : https://codeforces.com/contest/1814/submission/223672537
I thought the issue is happening when the map has only one element , so i tried to cover the case when the map has only one element separately , but still got the same error. The code is failing on this test case :-
n = 5 , s1 = 1 , s2 = 1 value of the array :- 1 1 1 1 1
Forgive me , if i have not expressed my views clearly or haven't explained the question properly because i am new to writting on codeforces
Decrement of begin-iterator is always UB
Decrementing a
begin()
iterator will result in undefined behavior.As you are traversing in a backward direction please use a reverse iterator.
An accepted solution using the same Solution
Thanks bro for the help . Understood this