Nour's blog

By Nour, history, 4 years ago, In English

Hey Codeforces! I am using c++ language and I got time limit in a question cause of datatype and, min() and max() functions. I was getting the min between an int datatype and long long and casting that long long variable into int in the max and min functions and that made me go through time limit even my code is correct that made me doubt that my code idea is incorrect. When I switch that int variable into long long datatype (not to make casting inside the max and min functions), my code got accepted! Any thoughts ?? The time limit code: 80418368.. The accepted code: 80419422.. The problem: 1355A - Sequence with Digits.

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

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

It isn't related to max an min functions and not from the datatypes. The casting made the loop endless, when casting from long long to int you need to make sure that the number fits in 32 bits, otherwise it will cause an overflow. So even if the code's verdict was not TLE it will surely be a WA

I'm not sure what test case caused the TLE, but here is your code but I declared $$$mn$$$ and $$$mx$$$ as long long instead of casting $$$n$$$ to int and it got accepted (with a slightly more time because long long takes slightly more time than int). 80424109

You could do this as well: max(mx,(int)(n%10));. This well calculate n%10 first then cast it into int which surely fits.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Well, I have tried to declare them long long before and it worked but I was trying to know only for more info why this happened. ,,Thank you, I appreciate your reply. :)

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Imagine that when casting $$$n$$$ and the overflow happened, $$$mn$$$ and $$$mx$$$ can be any value in the range $$$[-9,9]$$$ and they are totally random (it could be negative because $$$n$$$ could be negative after the overflow occurred). so $$$mn*mx$$$ could be negative or positive so you might add one then subtract one so you're stuck in the loop until $$$k$$$ becomes one

      I think something like this caused the TLE.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +5 Vote: I do not like it

        Oh I will take care next time not to cast a big number into small one. Thank you!