Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
超过时间限制-Kattis问题(python)_Python_Performance_Runtime_Kattis - Fatal编程技术网

超过时间限制-Kattis问题(python)

超过时间限制-Kattis问题(python),python,performance,runtime,kattis,Python,Performance,Runtime,Kattis,嘿,我希望有人能帮我 我试图解决这个问题:在kattis中识别地图分幅 我得到了正确的答案,直到第六次测试,我一直得到一个超过时间限制的错误 这是我的代码,我不确定出了什么问题。我可以做些什么来优化它,使它运行得更快?谢谢:D #! /usr/bin/env python3 import sys s=sys.stdin.read().splitlines() quadkey=s[0] zoom_level=len(quadkey) #Lists of x and y cordinates

嘿,我希望有人能帮我

我试图解决这个问题:在kattis中识别地图分幅

我得到了正确的答案,直到第六次测试,我一直得到一个超过时间限制的错误

这是我的代码,我不确定出了什么问题。我可以做些什么来优化它,使它运行得更快?谢谢:D

#! /usr/bin/env python3
import sys
s=sys.stdin.read().splitlines()

quadkey=s[0]

zoom_level=len(quadkey)

#Lists of x and y cordinates
x_cords=[]
y_cords=[]
for i in range(2**zoom_level):
    x_cords.append(i)
    y_cords.append(i)

#Try to loop through the quadkey and locate the cordinates
for i in range(len(quadkey)):
    half=int(len(x_cords)/2)
    if quadkey[i]=='0':
        x_cords=x_cords[:half]
        y_cords=y_cords[:half]
    elif quadkey[i]=='1':
        x_cords=x_cords[half:]
        y_cords=y_cords[:half]
    elif quadkey[i]=='2':
        x_cords=x_cords[:half]
        y_cords=y_cords[half:]
    elif quadkey[i]=='3':
        x_cords=x_cords[half:]
        y_cords=y_cords[half:]

x_cord=x_cords[0]
y_cord=y_cords[0]
print(zoom_level, x_cord, y_cord)

Kaddis有专门设计的问题,不能用蛮力方法来解决,所以通常,你必须研究这个问题并找到算法上不同的解决方案。我建议研究一下这个案例,找到正确的捷径