self.right = None self.parent = None self.value = None def traverse_tree(T, array): if T is not None: traverse_tree(T.left, array) array.append(T) traverse_tree(T.right, array) def ConvertTree(T): array = [] traverse_tree(T, array) for i in range(len(array)): left = 2 * i + 1 right = 2 * i + 2 if left < len(array): array[i].left = array[left] array[left].parent = array[i] array[left].left = array[left].right = None if right < len(array): array[i].right = array[right] array[right].parent = array[i] array[right].left = array[right].right = None return array[0] runtests(ConvertTree)
for k in range(p, r + 1): if L[i][0] <= R[j][0]: T[k] = L[i] i += 1 else: T[k] = R[j] j += 1 def merge_sort(T, p, r): if len(T) <= 1: return T elif p < r: m = (p + r) // 2 merge_sort(T, p, m) merge_sort(T, m + 1, r) merge(T, p, m, r) def chaos_index(T): for i in range(len(T)): T[i] = (T[i], i) merge_sort(T, 0, len(T) - 1) max_chaotic = 0 for i in range(len(T)): max_chaotic = max(max_chaotic, abs(i - T[i][1])) return max_chaotic runtests(chaos_index)
distance[u][v] = min(distance[u][v], distance[u][k] + distance[k][v]) return distance def keep_distance(M, x, y, d): distance = floyd_warshall_algorithm(M) size = len(M) * len(M) graph = [[0] * size for _ in range(size)] for i in range(len(M)): for j in range(len(M)): if i != j or distance[i][j] > d: for k in range(len(M)): for m in range(len(M)): if distance[k][m] < d or (k == j and m == i): continue if (j == m and M[i][k] != 0) or (i == k and M[j][m] != 0) or (M[i][k] != 0 and M[j][m] != 0): graph[i * len(M) + j][k * len(M) + m] = 1 parent = [None] * len(graph) source = x * len(M) + y bfs(graph, parent, source) result = [] index = y * len(M) + x while index is not None: result.append((index // len(M), index % len(M))) index = parent[index] result.reverse() return result runtests(keep_distance)
T[i] = (i, T[i][0], T[i][1], T[i][2], T[i][3]) T.sort(key=lambda x: x[3]) cost = [0] * len(T) num_of_students = [0] * len(T) parents = [None] * len(T) DP = [[0] * (p + 1) for _ in range(len(T))] for i in range(len(T)): num_of_students[i] = (T[i][3] - T[i][2]) * T[i][1] cost[i] = T[i][4] for j in range(i - 1, -1, -1): if T[i][2] > T[j][3]: parents[i] = j break for i in range(len(T)): for j in range(p + 1): DP[i][j] = DP[i - 1][j] if parents[i] is not None and j >= cost[i]: DP[i][j] = max( DP[i][j], DP[parents[i]][j - cost[i]] + num_of_students[i]) elif parents[i] is None and j >= cost[i]: DP[i][j] = max(DP[i][j], num_of_students[i]) buildings = [] get_result(DP, num_of_students, parents, cost, buildings, len(T) - 1, p) for i in range(len(buildings)): buildings[i] = T[buildings[i]][0] buildings.sort() return buildings runtests(select_buildings)
T.sort() vertices = [T[0]] idx = 0 for i in range(1, len(T)): if T[i] != vertices[idx]: vertices.append(T[i]) idx += 1 result = [] if binary_search(vertices, x) == -1 or binary_search(vertices, y) == -1: return result x_graph = [[] for _ in range(len(vertices))] y_graph = [[] for _ in range(len(vertices))] for i in range(len(I)): value_1 = binary_search(vertices, I[i][0]) value_2 = binary_search(vertices, I[i][1]) x_graph[value_1].append(value_2) y_graph[value_2].append(value_1) x_visited = [False] * len(vertices) dfs(x_graph, x_visited, binary_search(vertices, x)) y_visited = [False] * len(vertices) dfs(y_graph, y_visited, binary_search(vertices, y)) for i in range(len(I)): value_1 = binary_search(vertices, I[i][0]) value_2 = binary_search(vertices, I[i][1]) if x_visited[value_1] and y_visited[value_2]: result.append(i) return result runtests(intuse)
binary_search(T, 0, n * n - 1, diagonal) binary_search(T, 0, n * n - 1, diagonal + n - 1) index = 0 for i in range(diagonal, diagonal + n): swap(T, i, index) index += (n + 1) lower_index = current_lower_index = n * (n - 1) upper_index = current_upper_index = n - 1 while current_lower_index != 0 or current_upper_index != 0: while T[current_lower_index // n][current_lower_index % n] <= T[0][0] and current_lower_index != 0: if current_lower_index // n == n - 1: lower_index -= n current_lower_index = lower_index else: current_lower_index += (n + 1) while T[current_upper_index // n][current_upper_index % n] > T[0][0] and current_upper_index != 0: if current_upper_index % n == n - 1: upper_index -= 1 current_upper_index = upper_index else: current_upper_index += (n + 1) swap(T, current_upper_index, current_lower_index) return T runtests(Median)