TangyStone8's blog

By TangyStone8, history, 6 years ago, In English

Here is the submission..... https://codeforces.me/contest/1490/submission/108030618

It works in other online editors if i copy paste the Test 1... But it gives TLE on test 1 on submission.

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

»
6 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Your "prev" variable is declared inside the while loop, so every time it is reinitialized with a garbage value. You can declare this variable before the while loop.

»
6 years ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

As far as I can tell, you wanted to keep the previous value of the array's element into the variable prev. But if you take a closer look at the code, you declare prev at the same level as now, so after you go out of the loop, prev should be logically gone. And also you did not give any initial value to prev, there will be an undefined behavior when you are trying to use it. If you happened to have the correct answer, I think the compiler actually reuses the same memory of prev, so that time your answer will always be correct. But sometimes the compiler will try to do different things, like assign prev with zero, and on Codeforces it did that, I confirmed it by printing prev then exit after your 18-th line.

In conclusion, you are doing things that make undefined behavior occurs. One fix is just to move the prev variable to the outer scope.