我试图解决一个codechef问题(涡轮排序)。问题是
Am trying to solve a Codechef problem (Turbo Sort). The problem is
由于号码的列表,你对它们进行排序的非递减 订购。
Given the list of numbers, you are to sort them in non decreasing order.
T - 列表中的号码的数量,则T线按照[T< = 10 ^ 6]。
t – the number of numbers in list, then t lines follow [t <= 10^6].
每个行包含一个整数:N [0℃= N&LT; = 10 ^ 6]
Each line contains one integer: N [0 <= N <= 10^6]
输出提供的数字非递减顺序。
Output given numbers in non decreasing order.
输入:
5 5 3 6 7 1
5 5 3 6 7 1
输出:
1 3 5 6 7
1 3 5 6 7
我的解决办法是:
l = []
t = input()
MAX = 10**6
while t <= MAX and t != 0:
n = input()
l.append(n)
t = t - 1
st = sorted(l)
for x in st:
print x
目前的挑战是这个计划应在5秒内运行。当我提交的文件,codechef说,这是超越时间,需要优化。
The challenge is this program should run in 5 sec. When i submit the file, codechef says it is exceeding the time and needs optimization.
能有人帮助,如何优化呢?
Can some one help, how to optimize it ?
我接受的解决办法:
import sys
from itertools import imap
T = int(raw_input())
lines = sys.stdin.readlines()
lis = imap(str, sorted(imap(int, lines)))
print "\n".join(lis)
一个可读的版本(接受的解决方案):
A readable version(accepted solution) :
import sys
T = raw_input()
lines = sys.stdin.readlines() #fetch all lines from the STDIN
lines.sort(key=int) #sort the list in-place(faster than sorted)
print "\n".join(lines) #use `str.join` instead of a for-loop