Created
April 21, 2024 07:56
-
-
Save Nina07/e435061e4656cae6358efc20041a5537 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| global min_diff, ans_for_mindiff | |
| def permute(start, end, hole_depth, column_height, colors, col_index_params): | |
| if start == end: | |
| global min_diff, ans_for_mindiff | |
| N = len(col_index_params) | |
| new_height = [column_height[col_index_params[i]] - hole_depth[i] for i in range(N)] | |
| new_color = [colors[col_index_params[i]] for i in range(N)] | |
| diff, max_diff = 0, 0 | |
| # diff in consecutive columns | |
| for i in range(N-1): | |
| diff = abs(new_height[i] - new_height[i+1]) | |
| max_diff = max(max_diff, diff) | |
| diff = abs(new_height[0] - new_height[N-1]) | |
| max_diff = max(max_diff, diff) | |
| min_diff = min(max_diff, min_diff) | |
| if max_diff == min_diff: | |
| count, max_cons_count = 0, 0 | |
| for i in range(N-1): | |
| if new_color[i] == new_color[i+1]: | |
| count += 1 | |
| else: | |
| count = 0 | |
| max_cons_count = max(max_cons_count, count) | |
| if max_cons_count+1 != N: | |
| if new_color[0] == new_color[N-1]: | |
| count += 1 | |
| for i in range(N-1): | |
| if new_color[i] == new_color[i+1]: | |
| count += 1 | |
| else: | |
| max_cons_count = max(max_cons_count, count) | |
| break | |
| max_cons_count = max(max_cons_count, count) | |
| max_cons_count += 1 | |
| if max_cons_count > ans_for_mindiff[min_diff]: | |
| ans_for_mindiff[min_diff] = max_cons_count | |
| else: | |
| for i in range(start, end+1): | |
| col_index_params[start], col_index_params[i] = col_index_params[i], col_index_params[start] | |
| permute(start+1,end, hole_depth, column_height, colors, col_index_params) | |
| col_index_params[start], col_index_params[i] = col_index_params[i], col_index_params[start] | |
| if __name__ == '__main__': | |
| TC = int(input()) | |
| for tt in range(1, TC+1): | |
| N = int(input()) | |
| hole_depth = [int(x) for x in input().split()] | |
| column_height = [int(x) for x in input().split()] | |
| colors = [int(x) for x in input().split()] | |
| col_index_params = [i for i in range(N)] | |
| min_diff = 999 | |
| ans_for_mindiff = [0 for i in range(300)] | |
| permute(0, N-1, hole_depth, column_height, colors, col_index_params) | |
| print(ans_for_mindiff, min_diff) | |
| print("#{} {}".format(tt, ans_for_mindiff[min_diff])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment