1 / 3
Jan 2012

Can someone tell me why I get TLE? open_mouth

import psyco
psyco.full()
import sys
def main():
    s=sys.stdin.readline
    for test in range(int(s())):
        n=int(s())
        numbers=map(int, s().split())
        save={}
        for number in numbers:
            if number in save:
                save[number]+=1
            else:
                save[number]=1
        flag=False
        for key in save:
            if save[key]>n/2:
                flag=True
                name=key
                break
            else:
                continue
        if flag:
            print "YES", name
        else:
            print "NO"
if __name__=='__main__':
    main()
  • created

    Jan '12
  • last reply

    Jan '12
  • 2

    replies

  • 171

    views

  • 2

    users

Because it is too slow ... wink

My Python solution passes in approx. 1.5 s and is about 5 times faster than your code according to some random data I processed.
So much work for you to do.

You should generate some sample data and run your code on large input files (look at the constraints!). Then find the bottlenecks and then think about it, how to make them wider. Or use a faster language.

ok i will try it....i want to do it in python only...because I want to know this language. I have done most of the ques with python. Can you tell me some other way for counting in python which is faster..?