StellarSpecter's blog

By StellarSpecter, history, 2 days ago, In English

I can't understand why this solution 268341805 is passing but this 268341778 is giving WA on testcase 2, both codes are 99% same.

TLDR: I am writing down the part of my code which I changed so that my code got AC.

Please all the help would be greatly appreciated. Thank you so much in advance.

auto it = upper_bound(v.begin(),v.end(),val);
                        if(it!=v.end()&&it!=v.begin()) {
                            ans=min(ans,min(abs(*(it)-val),abs(*(--it)-val))); // old
                        }
                        else if(it==v.end()) {
                            ans = min(ans,abs(*(--it)-val));  //old
                        }
                        else if(it==v.begin()) {
                            ans=min(ans,abs(*(it) - val));  //old
                        }
auto it = upper_bound(v.begin(),v.end(),val);
                        if(it!=v.end()&&it!=v.begin()) {
                            auto it2 = it; it--;                                   // new
                            ans=min(ans,min(abs(*it2-val),abs(*(it)-val)));
                        }
                        else if(it==v.end()) {
                            it--;
                            ans = min(ans,abs(*(it)-val));                        // new
                        }
                        else if(it==v.begin()) {
                            ans=min(ans,abs(*it - val));                         // new
                        }
»
2 days ago, # |
  Vote: I like it 0 Vote: I do not like it

you're <999 and you solve Div2 C? :O

»
2 days ago, # |
  Vote: I like it 0 Vote: I do not like it

I think *(--it) is wrong because it gives WA always ive seen this alot many times in the past histories and i think this is the greatest mistake you can do in life

»
2 days ago, # |
  Vote: I like it 0 Vote: I do not like it

I see the changes you made to your code, and it makes sense why these adjustments led to getting an Accepted (AC) verdict. Let's break down why the new version works better:

Handling upper_bound Result Properly:

Old Code: Directly uses *(--it) which modifies it and could cause unexpected behavior if it is at v.begin(). New Code: Introduces a temporary iterator it2 to store the original upper_bound result and safely decrements it for the correct comparison. This ensures that both it and it2 are valid and not out of bounds. Edge Case Handling:

Old Code: Might mishandle edge cases where it is at the boundaries of the array. New Code: Explicitly ensures it is decremented safely and used for comparisons, which prevents undefined behavior when it is at the start or end of the vector.

»
2 days ago, # |
  Vote: I like it -15 Vote: I do not like it

PS: newbies please don't comment non-sensical things here, Only 1400+ are allowed to comment.

  • »
    »
    44 hours ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    not a good thing to say, some people below 1000 are actually really good but just haven't done any CF.

»
2 days ago, # |
  Vote: I like it 0 Vote: I do not like it

gholyo could help.

»
2 days ago, # |
Rev. 3   Vote: I like it +1 Vote: I do not like it

ans=min(ans,min(abs(*(it)-val),abs(*(--it)-val)));

For checking minimum, It will calculate both values for the comparison, but you decrement it which will reflect on the first argument too lol

»
2 days ago, # |
  Vote: I like it +7 Vote: I do not like it

if you compare min(a,b) b is evaluated first however in some compilers a can be evaluated as it depend on compiler so when you do min(*it,*(--it)) it first evaluate second one which cause it to go to previous position and now you compare previous position with previous position

here change ans=min(ans,min(abs(*(--it)-val),abs(*(it)-val))); // compiler is not dumbass

»
37 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Ok