tanu38's blog

By tanu38, history, 6 years ago, In English

Hello everyone!

Recently while solving a question 313B (in Python3 ), Me and my friend observed that TLE comes when we directly iterate using input, but not when we store intput in a variable and then iterate.

Please explain the reason why this is happening!

Case 1: TLE occured a = input() w=[0] z,j =0,1 for i in range(1,len(a)): if a[i]==a[i-1]: z+=1 w.append(z) for i in range(int(input())): a,b = map(int,input().split()) print(w[b-1]-w[a-1])

Case 2: No TLE, got Accepted a = input() w=[0] z,j =0,1 for i in range(1,len(a)): if a[i]==a[i-1]: z+=1 w.append(z) t =int(input()) for i in range(t): a,b = map(int,input().split()) print(w[b-1]-w[a-1])

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

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

Try submitting using Pypy 3 or Pypy 2. For me, I've seen that Pypy 2 is a bit faster. Of course C++ is much faster than either one and I have encountered a problem where the C++ solution passed (in 15 ms!), but Python solution got TLE (1000 ms).

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

Can you link the submissions?

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

When you submit a solution multiple times it doesn't always get same execution time. There might be a difference in a few ms and it's normal. For example, I submitted your TL code and got AC: https://codeforces.me/contest/313/submission/83834926