How python [internally] retrieves elements from array and finds minimum -
for question http://www.spoj.com/problems/acpc10d/ on spoj, wrote python solution below:
count = 1 while true: no_rows = int(raw_input()) if no_rows == 0: break grid = [[none x in range(3)] y in range(2)] input_arr = map(int, raw_input().split()) grid[0][0] = 10000000 grid[0][1] = input_arr[1] grid[0][2] = input_arr[1] + input_arr[2] r = 1 in range(0, no_rows-1): input_arr = map(int, raw_input().split()) _r = r ^ 1 grid[r][0] = input_arr[0] + min(grid[_r][0], grid[_r][1]) grid[r][1] = input_arr[1] + min(min(grid[_r][0], grid[r][0]), min(grid[_r][1], grid[_r][2])) grid[r][2] = input_arr[2] + min(min(grid[_r][1], grid[r][1]), grid[_r][2]) r = _r print str(count) + ". " + str(grid[(no_rows -1) & 1][1]) count += 1
the above code exceeds time limit. however, when change line
grid[r][2] = input_arr[2] + min(min(grid[_r][1], grid[r][1]), grid[_r][2])
to
grid[r][2] = input_arr[2] + min(min(grid[_r][1], grid[_r][2]), grid[r][1])
the solution accepted. if notice difference, first line compares, grid[_r][1], grid[r][1]
minimum (i.e. row number different
) , second line compares grid[_r][1], grid[_r][2]
minimum(i.e. row number same
)
this consistent behaviour. want understand, how python processing 2 lines - 1 results in exceeding time limit, while other fine.
Comments
Post a Comment