ateeque219's blog

By ateeque219, history, 10 months ago, In English

https://mirror.codeforces.com/contest/455/submission/242903355

How my code got accepted ..without .the base case...

455A - Boredom

  • Vote: I like it
  • -1
  • Vote: I do not like it

»
10 months ago, # |
  Vote: I like it +1 Vote: I do not like it

because when index becomes greater than the size of dp array the value of dp[index] is not -1.

  • »
    »
    10 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    accessing dp[index] (index == size of dp) is an out-of-bounds access. C++ doesn't perform bounds checking by default, so accessing an index out of the vector's bounds will not necessarily result in an access violation error, but it leads to undefined behavior. Undefined behavior means that the program's behavior is unpredictable and it may lead to crashes, unexpected results, or other issues .

  • »
    »
    10 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    it means that if we access dp[x] where x>dp.size(), then it will be not be equal to -1 that's why it get return there as dp[x]...but what is the surity that it will always give some negative garbage value..it can also give some random positive garbage value...which will result in wrong answer......because we are taking maximum of all the dp values as answer.......?? ??

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It is not guaranteed to be 0 due to the garbage values in the vector, but it is a common scenario where the garbage value might often be interpreted as 0. Call it luck I guess.

      • »
        »
        »
        »
        10 months ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        Ya that's what i am also thinking ..luckily it passed....because we can't be sure always that it will give negative garbage value...it is random...