description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): s = A moves = 1 while len(A) < len(B): A += s moves += 1 if A.find(B) != -1: return moves A += s moves += 1 if A.find(B) != -1: return moves else: return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): n = len(B) m = len(A) count = n // m + 2 for i in range(1, count): if B in A * i: return i return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): if A == B: return 1 if len(A) > len(B): if B in A: return 1 else: return -1 i = 0 j = 0 while i < len(A) and j < len(B): if A[i] == B[j]: i += 1 j += 1 else: j = 0 i += 1 if j == 0: return -1 i = 0 ans = 0 flag = True while i < len(A) and j < len(B): if A[i] == B[j]: i += 1 j += 1 if i == len(A) and j != len(B): ans += 1 i = 0 if i == len(A) and j == len(A): flag = False ans += 1 else: return -1 if flag == False: return ans else: return ans + 2
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, a, b): s = a for elm in a: if elm not in b: return -1 i = 0 j = len(b) - 1 count = 1 while len(s) < 2 * len(b): if len(s) < len(b): s += a count += 1 else: temp = s[i : j + 1] while j < len(s): if temp == b: return count temp = s[i : j + 1] i += 1 j += 1 if temp == b: return count s += a count += 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR VAR VAR VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): if len(A) > len(B): return 2 a = int(len(B) / len(A)) if B in A * a: return a elif B in A * (a + 1): return a + 1 else: return -1
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR RETURN VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): t = len(B) // len(A) + 3 count = 0 ans = "" for i in range(t): if B in ans: return count ans += A count += 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR VAR VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): max = len(B) / len(A) + 1 min = 1 rep = A for i in range(int(max)): if B in A: return min A = str(A) + str(rep) min += 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): if set(A) != set(B): return -1 c = 1 temp = A while len(B) > len(A): A += temp c += 1 if B not in A: A += temp c += 1 if B in A: return c return -1
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): if set(B) != set(A): return -1 min_times = len(B) // len(A) for i in range(2): _A = A * (min_times + i) if _A.find(B) != -1: return min_times + i return -1
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, a, b): len_a = len(a) len_b = len(b) for i in range(0, len_a): if a[i] == b[0]: k = i count = 1 for j in range(0, len_b): if k >= len_a: k = 0 count = count + 1 if a[k] != b[j]: break k = k + 1 else: return count return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, a, b): repeat = 1 c = a while len(b) > len(c): c += a repeat += 1 if not b in c: c += a repeat += 1 if b in c: return repeat return -1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): x = set(A) if set(A) != set(B): return -1 n = len(B) n1 = len(A) p = n // n1 y = A * p if y.find(B) != -1: return p elif (y + A).find(B) != -1: return p + 1 else: return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, a, b): n = len(a) m = len(b) tmp = "" c = 0 while len(tmp) < n + m: tmp += a c += 1 if b in tmp: return c return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): n = len(A) m = len(B) count = 0 for i in range(n): if A[i] == B[0]: k = i count = 1 for j in range(m): if k >= n: k = 0 count += 1 if A[k] != B[j]: break k += 1 else: return count return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): x = A * 10**3 if B not in x: return -1 else: x = A c = 1 while B not in x: x += A c += 1 return c
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): i = c = 0 while True: a = A * i c = c + 1 if a.find(B) != -1: return c - 1 if len(a) > len(B): return -1 else: i = i + 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): sup = A count = 1 if B in A: return count for i in range(len(B) // len(A)): sup += A count += 1 if B in sup: return count return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): arr = [] for i in range(len(A)): if B[0] == A[i]: arr.append(i) for val in arr: i = val j = 1 neq = False res = 1 while j < len(B): if (i + 1) % len(A) == 0: res += 1 if A[(i + 1) % len(A)] != B[j]: neq = True break i += 1 j += 1 if neq == False: return res return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): S = "" c = 0 while B not in S: S += A c += 1 if len(S) >= len(B): break if B in S: return c elif B in S + A: return c + 1 else: return -1
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): n1 = len(A) n2 = len(B) n3 = n1 C = A while n3: if C != B[:n3]: C = C[1:] n3 -= 1 else: break count = 1 while n3 < n2: C += A n3 += n1 count += 1 return count if C[:n2] == B else -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): n1 = len(A) n2 = len(B) count = 1 k = 0 while k < n1: i = k j = 0 while B[j] == A[i] and j < n2: j += 1 if j == n2: return count i += 1 if i == n1: i = 0 count += 1 k += 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): possibleStart = [i for i in range(len(A)) if B[0] == A[i]] alen = len(A) for i in possibleStart: start = i status = True count = 1 for j in B: if start == alen: start = 0 count += 1 if A[start] != j: status = False break start += 1 if status: return count return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): str1 = A count = 1 while len(str1) < len(B): str1 = str1 + A count = count + 1 if str1.find(B) == -1: str1 = str1 + A count = count + 1 if str1.find(B) == -1: return -1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): a = len(A) b = len(B) for i in range(0, a): if A[i : min(a, b + i)] != B[: min(a - i, b)]: continue cnt = 1 j = min(a - i, b) valid = True while j < b: if A[: min(a, b - j)] == B[j : min(a + j, b)]: cnt += 1 j += a else: valid = False break if valid: return cnt return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): n = len(A) m = len(B) c = 0 tmp = "" while len(tmp) < m: tmp += A c += 1 if B in tmp: return c tmp += A c += 1 if B in tmp: return c return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): a = len(A) b = len(B) for k in range(b): if B[k] not in A: return -1 i = 1 while i < 1000: c = A * i if B in c: return i i += 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): m = len(A) n = len(B) count = 0 found = False for i in range(m): j = i k = 0 count = 1 while k < n and A[j] == B[k]: if k == n - 1: found = True break j = (j + 1) % m if j == 0: count = count + 1 k = k + 1 if found: return count return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): if len(A) > len(B): if B in A: return 1 elif B in A + A: return 2 else: return -1 else: ans = 1 temp = A while len(temp) < len(B): ans += 1 temp += A if B in temp: return ans elif B in temp + A: return ans + 1 else: return -1
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): for i in B: if i not in A: return -1 if A == B: return 1 S = A c = 1 if A in B: while len(A) < len(B): A += S c += 1 if B in A: return c else: A += S c += 1 if B in A: return c return -1
CLASS_DEF FUNC_DEF FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): repeat = 0 backup = A if A == B: return 1 while B not in backup and len(backup) < len(B) * 1.5: repeat += 1 backup = A * repeat if repeat == 0 or B not in backup: repeat = -1 return repeat
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER WHILE VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER RETURN VAR
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): if B in A: return 1 set_b = set(c for c in B) for c in A: if c not in set_b: return -1 min_rep = len(B) // len(A) if len(B) % len(A) != 0 or A != B[: len(A)]: min_rep += 1 new_A = A * min_rep n = len(new_A) if B in new_A: return min_rep for ind, c in enumerate(new_A[::-1]): if B[0] == c: found = True cur_ind = n - 1 - ind for c2 in B: if c2 != new_A[cur_ind]: found = False break cur_ind = (cur_ind + 1) % (n - 1) if found: if cur_ind > ind: cur_ind = cur_ind - ind min_rep += (cur_ind + 1) // len(A) + 1 return min_rep return -1
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): parent = str(A) ans = 1 while len(parent) < len(B): parent = parent + str(A) + "" ans += 1 if parent.find(B) == -1: parent = parent + str(A) + "" ans += 1 if parent.find(B) == -1: return -1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): for i in B: if A.find(i) == -1: return -1 for i in range(1, len(B)): if (A * i).find(B) != -1: return i if len(A * i) >= len(A) + len(B): return -1
CLASS_DEF FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL BIN_OP VAR VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): ans = 1 s = A while len(s) < len(B): s += A ans += 1 if s.__contains__(B): return ans s += A if s.__contains__(B): return ans + 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR VAR IF FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER RETURN NUMBER
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): direction = [[0, 1], [1, 0], [0, -1], [-1, 0]] visited = [[(0) for i in range(m)] for j in range(n)] def dfs(row, col): visited[row][col] = 1 for dir in direction: r = row + dir[0] c = col + dir[1] if (r >= 0 and r < n) and (c >= 0 and c < m) and not visited[r][c]: if mat[r][c] == "O": dfs(r, c) for i in range(n): if mat[i][0] == "O" and visited[i][0] == 0: dfs(i, 0) for i in range(n): if mat[i][m - 1] == "O" and visited[i][m - 1] == 0: dfs(i, m - 1) for j in range(m): if mat[0][j] == "O" and visited[0][j] == 0: dfs(0, j) for j in range(m): if mat[n - 1][j] == "O" and visited[n - 1][j] == 0: dfs(n - 1, j) for i in range(n): for j in range(m): if visited[i][j] == 1: visited[i][j] = "O" else: visited[i][j] = "X" return visited
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): directions = [[0, 1], [1, 0], [-1, 0], [0, -1]] def dfs(row, col): if row >= 0 and row < n and col >= 0 and col < m and mat[row][col] == "O": mat[row][col] = "NO" for dir in directions: newRow = row + dir[0] newCol = col + dir[1] dfs(newRow, newCol) for i in range(n): dfs(i, 0) for i in range(n): dfs(i, m - 1) for i in range(m): dfs(0, i) for i in range(m): dfs(n - 1, i) for i in range(n): for j in range(m): if mat[i][j] == "NO": mat[i][j] = "O" else: mat[i][j] = "X" return mat
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): vis = [[(False) for i in range(m)] for j in range(n)] res = [[(0) for i in range(m)] for j in range(n)] queue = [] for i in range(m): if mat[0][i] == "O": queue.append([0, i]) vis[0][i] = 1 if mat[-1][i] == "O": queue.append([n - 1, i]) vis[-1][i] = 1 for i in range(1, n - 1): if mat[i][0] == "O": queue.append([i, 0]) vis[i][0] = 1 if mat[i][m - 1] == "O": queue.append([i, m - 1]) vis[i][m - 1] = 1 while len(queue) > 0: r, s = queue.pop(0) res[r][s] = "O" dirx = [-1, 1, 0, 0] diry = [0, 0, -1, 1] for ele in range(4): newx = r + dirx[ele] newy = s + diry[ele] if ( 0 <= newx < len(mat) and 0 <= newy < len(mat[0]) and vis[newx][newy] == False and mat[newx][newy] == "O" ): queue.append([newx, newy]) vis[newx][newy] = 1 for i in range(len(mat)): for j in range(len(mat[0])): if res[i][j] == 0: res[i][j] = "X" return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING EXPR FUNC_CALL VAR LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, a): q = [] for i in range(n): if a[i][0] == "O": q.append((i, 0)) if a[i][m - 1] == "O": q.append((i, m - 1)) for j in range(m): if a[0][j] == "O": q.append((0, j)) if a[n - 1][j] == "O": q.append((n - 1, j)) while len(q) != 0: i, j = q.pop(0) a[i][j] = "1" for k in [-1, 1]: if i + k >= 0 and i + k < n and a[i + k][j] == "O": q.append((i + k, j)) if j + k >= 0 and j + k < m and a[i][j + k] == "O": q.append((i, j + k)) for i in range(n): for j in range(m): if a[i][j] == "1": a[i][j] = "O" elif a[i][j] == "O": a[i][j] = "X" return a
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING EXPR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR STRING FOR VAR LIST NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def DFS(self, row, col, mat, visited, n, m): visited[row][col] = True delta_col = [-1, 0, 1, 0] delta_row = [0, 1, 0, -1] for i in range(0, 4): nrow = row + delta_row[i] ncol = col + delta_col[i] if ( nrow >= 0 and nrow < n and ncol >= 0 and ncol < m and not visited[nrow][ncol] and mat[nrow][ncol] == "O" ): self.DFS(nrow, ncol, mat, visited, n, m) def fill(self, n, m, mat): visited = [[(False) for i in range(0, m)] for j in range(0, n)] for j in range(0, m): if not visited[0][j] and mat[0][j] == "O": self.DFS(0, j, mat, visited, n, m) if not visited[n - 1][j] and mat[n - 1][j] == "O": self.DFS(n - 1, j, mat, visited, n, m) for i in range(0, n): if not visited[i][0] and mat[i][0] == "O": self.DFS(i, 0, mat, visited, n, m) if not visited[i][m - 1] and mat[i][m - 1] == "O": self.DFS(i, m - 1, mat, visited, n, m) for i in range(0, n): for j in range(0, m): if not visited[i][j] and mat[i][j] == "O": mat[i][j] = "X" return mat
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): res = mat n = len(mat) m = len(mat[0]) if n == 1 or n == 2: return mat vis = [] for i in range(n): vis.append([0] * m) for i in range(n): for j in range(m): if i == 0 or i == n - 1 and 0 <= j < m: if mat[i][j] == "O": vis[i][j] = 1 self.dfs(i, j, mat, vis) if j == 0 or j == m - 1 and 0 <= i < n: if mat[i][j] == "O": vis[i][j] = 1 self.dfs(i, j, mat, vis) for x in range(1, n - 1): for y in range(1, m - 1): if mat[x][y] == "O" and vis[x][y] != 1: res[x][y] = "X" return res def dfs(self, i, j, mat, vis): n = len(mat) m = len(mat[0]) if i - 1 >= 0 and j >= 0 and vis[i - 1][j] == 0 and mat[i - 1][j] == "O": vis[i - 1][j] = 1 self.dfs(i - 1, j, mat, vis) if i >= 0 and j - 1 >= 0 and vis[i][j - 1] == 0 and mat[i][j - 1] == "O": vis[i][j - 1] = 1 self.dfs(i, j - 1, mat, vis) if ( i + 1 <= n - 1 and j <= m - 1 and vis[i + 1][j] == 0 and mat[i + 1][j] == "O" ): vis[i + 1][j] = 1 self.dfs(i + 1, j, mat, vis) if ( i <= n - 1 and j + 1 <= m - 1 and vis[i][j + 1] == 0 and mat[i][j + 1] == "O" ): vis[i][j + 1] = 1 self.dfs(i, j + 1, mat, vis)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): def cap(r, c): if r < 0 or c < 0 or r >= n or c >= m or mat[r][c] != "O": return mat[r][c] = "T" cap(r + 1, c) cap(r - 1, c) cap(r, c + 1) cap(r, c - 1) for i in range(n): for j in range(m): if mat[i][j] == "O" and (i in [0, n - 1] or j in [0, m - 1]): cap(i, j) for r in range(n): for c in range(m): if mat[r][c] == "O": mat[r][c] = "X" for r in range(n): for c in range(m): if mat[r][c] == "T": mat[r][c] = "O" return mat
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR STRING RETURN ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR LIST NUMBER BIN_OP VAR NUMBER VAR LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def dfs(self, i, j, vis, mat): n, m = len(mat), len(mat[0]) vis[i][j] = 1 dx, dy = [-1, 0, 1, 0], [0, -1, 0, 1] for k in range(4): nx, ny = i + dx[k], j + dy[k] if ( nx >= 0 and nx < n and ny >= 0 and ny < m and vis[nx][ny] == 0 and mat[nx][ny] == "O" ): self.dfs(nx, ny, vis, mat) def fill(self, n, m, mat): vis = [([0] * m) for _ in range(n)] ans = mat for i in range(n): for j in range(m): if i == 0 or j == 0 or i == n - 1 or j == m - 1: if ans[i][j] == "O" and vis[i][j] == 0: self.dfs(i, j, vis, mat) for i in range(n): for j in range(m): if vis[i][j] == 0 and ans[i][j] == "O": ans[i][j] = "X" return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, M, N, mat): def floodFillUtil(mat, x, y, prevV, newV): if x < 0 or x >= M or y < 0 or y >= N: return if mat[x][y] != prevV: return mat[x][y] = newV floodFillUtil(mat, x + 1, y, prevV, newV) floodFillUtil(mat, x - 1, y, prevV, newV) floodFillUtil(mat, x, y + 1, prevV, newV) floodFillUtil(mat, x, y - 1, prevV, newV) def replaceSurrounded(mat): for i in range(M): for j in range(N): if mat[i][j] == "O": mat[i][j] = "-" for i in range(M): if mat[i][0] == "-": floodFillUtil(mat, i, 0, "-", "O") for i in range(M): if mat[i][N - 1] == "-": floodFillUtil(mat, i, N - 1, "-", "O") for i in range(N): if mat[0][i] == "-": floodFillUtil(mat, 0, i, "-", "O") for i in range(N): if mat[M - 1][i] == "-": floodFillUtil(mat, M - 1, i, "-", "O") for i in range(M): for j in range(N): if mat[i][j] == "-": mat[i][j] = "X" replaceSurrounded(mat) return mat
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR NUMBER STRING STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR STRING STRING FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR STRING STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): arr = [] neighbors = [[1, 0], [0, 1], [-1, 0], [0, -1]] for i in range(n): for j in range(m): if (i == 0 or j == 0 or i == n - 1 or j == m - 1) and mat[i][j] == "O": arr.append([i, j]) visited = [([0] * m) for _ in range(n)] for i in arr: row = i[0] col = i[1] q = [] ptr = 0 if visited[row][col] == 0: q.append(i) visited[row][col] = 1 mat[row][col] = 1 while ptr < len(q): row = q[ptr][0] col = q[ptr][1] for x in neighbors: try: if ( row - x[0] >= 0 and col - x[1] >= 0 and visited[row - x[0]][col - x[1]] == 0 and mat[row - x[0]][col - x[1]] == "O" ): q.append([row - x[0], col - x[1]]) visited[row - x[0]][col - x[1]] = 1 mat[row - x[0]][col - x[1]] = 1 except: pass ptr += 1 for i in range(n): for j in range(m): if mat[i][j] == 1: mat[i][j] = "O" else: mat[i][j] = "X" return mat
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): visited = [[(0) for i in range(m)] for j in range(n)] def dfs(visited, row, col, mat): row_del = [-1, 1, 0, 0] col_del = [0, 0, -1, 1] visited[row][col] = 1 for i in range(4): new_row = row + row_del[i] new_col = col + col_del[i] if ( new_row < n and new_col < m and new_row > -1 and new_col > -1 and mat[new_row][new_col] == "O" and visited[new_row][new_col] == 0 ): dfs(visited, new_row, new_col, mat) for i in range(m): if mat[0][i] == "O" and visited[0][i] == 0: dfs(visited, 0, i, mat) if mat[n - 1][i] == "O" and visited[n - 1][i] == 0: dfs(visited, n - 1, i, mat) for i in range(n): if mat[i][0] == "O" and visited[i][0] == 0: dfs(visited, i, 0, mat) if mat[i][m - 1] == "O" and visited[i][m - 1] == 0: dfs(visited, i, m - 1, mat) for i in range(n): for j in range(m): if visited[i][j] == 0 and mat[i][j] == "O": mat[i][j] = "X" return mat
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: ans = 1 def gen(self, i, j, n, m, mat, visited): visited[i][j] = 1 if i - 1 >= 0 and visited[i - 1][j] == 0 and mat[i - 1][j] == "O": visited[i - 1][j] = 1 self.gen(i - 1, j, n, m, mat, visited) if i + 1 < n and visited[i + 1][j] == 0 and mat[i + 1][j] == "O": visited[i + 1][j] = 1 self.gen(i + 1, j, n, m, mat, visited) if j - 1 >= 0 and visited[i][j - 1] == 0 and mat[i][j - 1] == "O": visited[i][j - 1] = 1 self.gen(i, j - 1, n, m, mat, visited) if j + 1 < m and visited[i][j + 1] == 0 and mat[i][j + 1] == "O": visited[i][j + 1] = 1 self.gen(i, j + 1, n, m, mat, visited) return def fill(self, n, m, mat): q = [] for i in range(n): for j in range(m): if mat[i][j] == "O" and (i == 0 or j == 0 or i == n - 1 or j == m - 1): q.append([i, j]) visited = [] for i in range(n): arr = [] for j in range(m): arr.append(0) visited.append(arr) for i in q: self.gen(i[0], i[1], n, m, mat, visited) for i in range(n): for j in range(m): if mat[i][j] == "O" and visited[i][j] == 0: mat[i][j] = "X" return mat
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): for i in range(n): for j in range(m): if mat[i][j] == "O": mat[i][j] = "-" for i in range(n): if mat[i][0] == "-": self.helper(mat, i, 0, "-", "O") for i in range(n): if mat[i][m - 1] == "-": self.helper(mat, i, m - 1, "-", "O") for i in range(m): if mat[0][i] == "-": self.helper(mat, 0, i, "-", "O") for i in range(m): if mat[n - 1][i] == "-": self.helper(mat, n - 1, i, "-", "O") for i in range(n): for j in range(m): if mat[i][j] == "-": mat[i][j] = "X" return mat def helper(self, mat, x, y, prevV, newV): if x < 0 or x >= n or y < 0 or y >= m: return if mat[x][y] != prevV: return mat[x][y] = newV self.helper(mat, x + 1, y, prevV, newV) self.helper(mat, x - 1, y, prevV, newV) self.helper(mat, x, y + 1, prevV, newV) self.helper(mat, x, y - 1, prevV, newV)
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR NUMBER STRING STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR STRING STRING FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR STRING STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def __dfs__(self, visited, final_ans, root, row, col, rows, columns): visited[row][col] = True nodes_to_visit = [ (row - 1, col), (row + 1, col), (row, col + 1), (row, col - 1), ] for temp_row, temp_col in nodes_to_visit: if ( 0 <= temp_row < rows and 0 <= temp_col < columns and not visited[temp_row][temp_col] and final_ans[temp_row][temp_col] == "O" ): self.__dfs__( visited, final_ans, root, temp_row, temp_col, rows, columns ) def fill(self, rows, columns, grid): final_ans = [row[:] for row in grid] visited_arr = [[(False) for _ in range(columns)] for _ in range(rows)] for row in range(rows): for col in range(columns): if ( not visited_arr[row][col] and grid[row][col] == "O" and (row == rows - 1 or row == 0 or col == 0 or col == columns - 1) ): self.__dfs__( visited_arr, final_ans, grid[row][col], row, col, rows, columns ) for i in range(rows): for j in range(columns): if final_ans[i][j] == "O" and not visited_arr[i][j]: final_ans[i][j] = "X" return final_ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): def dfs(row, col, visited, mat, r, c): visited[row][col] = 1 for i in range(4): nrow = row + r[i] ncol = col + c[i] if ( nrow >= 0 and nrow < n and ncol >= 0 and ncol < m and visited[nrow][ncol] != 1 and mat[nrow][ncol] == "O" ): dfs(nrow, ncol, visited, mat, r, c) r = [-1, 0, 1, 0] c = [0, 1, 0, -1] visited = [([0] * m) for i in range(n)] for i in range(m): if visited[0][i] != 1 and mat[0][i] == "O": dfs(0, i, visited, mat, r, c) if visited[n - 1][i] != 1 and mat[n - 1][i] == "O": dfs(n - 1, i, visited, mat, r, c) for i in range(n): if visited[i][0] != 1 and mat[i][0] == "O": dfs(i, 0, visited, mat, r, c) if visited[i][m - 1] != 1 and mat[i][m - 1] == "O": dfs(i, m - 1, visited, mat, r, c) for i in range(n): for j in range(m): if visited[i][j] != 1 and mat[i][j] == "O": mat[i][j] = "X" return mat
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR STRING EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, mat): visited = [[(0) for i in range(m)] for j in range(n)] ans = mat.copy() def dfs(row, col): visited[row][col] = 1 path = [(row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)] for nrow, ncol in path: if ( nrow >= 0 and nrow < n and ncol >= 0 and ncol < m and visited[nrow][ncol] == 0 and mat[nrow][ncol] == "O" ): dfs(nrow, ncol) for i in range(n): if mat[i][0] == "O" and visited[i][0] == 0: dfs(i, 0) if mat[i][-1] == "O" and visited[i][m - 1] == 0: dfs(i, m - 1) for j in range(m): if mat[0][j] == "O" and visited[0][j] == 0: dfs(0, j) if mat[n - 1][j] == "O" and visited[n - 1][j] == 0: dfs(n - 1, j) for i in range(n): for j in range(m): if ans[i][j] == "O" and visited[i][j] == 0: ans[i][j] = "X" return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING RETURN VAR
Given a matrix mat of size N x M where every element is either O or X. Replace all O with X that are surrounded by X. A O (or a set of O) is considered to be surrounded by X if there are X at locations just below, just above, just left and just right of it. Example 1: Input: n = 5, m = 4 mat = {{'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'O', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Output: ans = {{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'O'}} Explanation: Following the rule the above matrix is the resultant matrix. Your Task: You do not need to read input or print anything. Your task is to complete the function fill() which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix. Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n, m ≤ 500
class Solution: def fill(self, n, m, board): def dfs(m, n, delcol, delrow, board, vis, row, col): vis[row][col] = 1 for i in range(4): nrow = row + delrow[i] ncol = col + delcol[i] if ( nrow >= 0 and nrow < n and ncol >= 0 and ncol < m and vis[nrow][ncol] == 0 and board[nrow][ncol] == "O" ): dfs(m, n, delcol, delrow, board, vis, nrow, ncol) vis = [[(0) for j in range(m)] for i in range(n)] delrow = [-1, 0, 1, 0] delcol = [0, 1, 0, -1] for j in range(m): if vis[0][j] == 0 and board[0][j] == "O": dfs(m, n, delcol, delrow, board, vis, 0, j) if vis[n - 1][j] == 0 and board[n - 1][j] == "O": dfs(m, n, delcol, delrow, board, vis, n - 1, j) for i in range(n): if vis[i][0] == 0 and board[i][0] == "O": dfs(m, n, delcol, delrow, board, vis, i, 0) if vis[i][m - 1] == 0 and board[i][m - 1] == "O": dfs(m, n, delcol, delrow, board, vis, i, m - 1) for i in range(n): for j in range(m): if board[i][j] == "O" and vis[i][j] == 0: board[i][j] = "X" return board
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING RETURN VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
d = {} def pro(x, y, w): res = 0 while x != y: if x < y: x, y = y, x d[x] = d.get(x, 0) + w res += d[x] x //= 2 return res q = int(input()) while q > 0: q -= 1 s = list(map(int, input().split())) if s[0] == 1: pro(s[1], s[2], s[3]) else: print(pro(s[1], s[2], 0))
ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
n = int(input()) t1 = {} t2 = {} for i in range(n): q = list(map(int, input().split())) if q[0] == 1: _, u, v, w = q while u != v: v, u = max(u, v), min(u, v) if v & 1 == 1: t2[v] = t2.get(v, 0) + w v = (v - 1) // 2 else: t1[v] = t1.get(v, 0) + w v = v // 2 else: _, u, v = q c = 0 while u != v: v, u = max(u, v), min(u, v) if v & 1 == 1: c += t2.get(v, 0) v = (v - 1) // 2 else: c += t1.get(v, 0) v = v // 2 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
q = int(input()) price = {} def get_price(u): if u in price: return price[u] else: return 0 for i in range(0, q): temp = str(input()).split(" ") u = int(temp[1]) v = int(temp[2]) if temp[0] == "2": res = 0 while u != v: if u > v: res = res + get_price(u) u //= 2 else: res = res + get_price(v) v //= 2 print(res) else: w = int(temp[3]) while u != v: if u > v: price[u] = get_price(u) + w u //= 2 else: price[v] = get_price(v) + w v //= 2
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
q = int(input()) fee = {} for _ in range(q): s = input().split() if len(s) == 4: event_type, u, v, w = map(int, s) su = set() sv = set() while u > 1: su.add(u) u >>= 1 while v > 1: sv.add(v) v >>= 1 for i in su ^ sv: if i in fee: fee[i] += w else: fee[i] = w else: event_type, u, v = map(int, s) su = set() sv = set() while u > 1: su.add(u) u >>= 1 while v > 1: sv.add(v) v >>= 1 ret = 0 for i in su ^ sv: if i in fee: ret += fee[i] print(ret)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
def ter(a, b): if a > b: a, b = b, a c = "" for i in range(a.bit_length() + 2): if bin(a)[i] != bin(b)[i]: break c += bin(a)[i] c = int(c, 2) o = [] while a != c: o.append(a) a //= 2 while b != c: o.append(b) b //= 2 return o n = int(input()) a = {} y = [] for i in range(n): s = [int(i) for i in input().split()] if s[0] == 1: o = ter(s[1], s[2]) for i in o: if i in a: a[i] += s[3] else: a[i] = s[3] else: ans = 0 o = ter(s[1], s[2]) for i in o: if i in a: ans += a[i] y.append(ans) for i in y: print(i)
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
q = int(input()) def get_path(u, v): u2 = u v2 = v u_visited = set() while u2 > 1: u_visited.add(u2) u2 = u2 // 2 u_visited.add(1) v2_path = [] while v2 not in u_visited: v2_path.append(v2) v2 = v2 // 2 path = v2_path + sorted([x for x in u_visited if x >= v2]) return path prices = {} for i in range(q): row = list(map(int, input().split())) if row[0] == 1: u, v, w = row[1:] path = get_path(*sorted([u, v])) for a_, b_ in zip(path, path[1:]): a, b = sorted([a_, b_]) prices.setdefault((a, b), 0) prices[a, b] += w else: u, v = row[1:] path = get_path(*sorted([u, v])) price = 0 for a_, b_ in zip(path, path[1:]): a, b = sorted([a_, b_]) price += prices.get((a, b), 0) print(price)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
tree = [] n = int(input()) dic = {} for i in range(n): line = list(input().split()) if line[0] == "1": a = int(line[1]) b = int(line[2]) line[3] = float(line[3]) old_a = a old_b = b while a != b: if a > b: a = a // 2 key = "%d:%d" % (old_a, a) old_a = a if key not in dic: dic[key] = line[3] else: dic[key] += line[3] else: b = b // 2 key = "%d:%d" % (old_b, b) old_b = b if key not in dic: dic[key] = line[3] else: dic[key] += line[3] if line[0] == "2": a = int(line[1]) b = int(line[2]) old_a = a old_b = b value = 0 while a != b: if a > b: a = a // 2 key = "%d:%d" % (old_a, a) old_a = a if key not in dic: value += 0 else: value += dic[key] else: b = b // 2 key = "%d:%d" % (old_b, b) old_b = b if key not in dic: value += 0 else: value += dic[key] print("%d" % value)
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
q = int(input()) L = [list(map(int, input().split())) for _ in range(q)] T = {(1): 0} def binaire(n): if n <= 1: return [n] else: return binaire(n // 2) + [n % 2] def inserer2(T, A, w, i): if i in T: T[i] += w else: T[i] = w if A == []: return T return inserer2(T, A[1:], w, 2 * i + A[0]) def inserer(T, A, B, w, i): if A == []: T = inserer2(T, B[1:], w, 2 * i + B[0]) return T if B == []: T = inserer2(T, A[1:], w, 2 * i + A[0]) return T if A[0] == B[0]: return inserer(T, A[1:], B[1:], w, 2 * i + A[0]) else: T = inserer2(T, A[1:], w, 2 * i + A[0]) T = inserer2(T, B[1:], w, 2 * i + B[0]) return T def compter2(T, A, i): if A == []: if i in T: return T[i] else: return 0 elif i in T: return T[i] + compter2(T, A[1:], 2 * i + A[0]) else: return compter2(T, A[1:], 2 * i + A[0]) def compter(T, A, B, i): if A == []: return compter2(T, B[1:], 2 * i + B[0]) if B == []: return compter2(T, A[1:], 2 * i + A[0]) if A[0] == B[0]: return compter(T, A[1:], B[1:], 2 * i + A[0]) else: a = compter2(T, A[1:], 2 * i + A[0]) b = compter2(T, B[1:], 2 * i + B[0]) return a + b for i in range(q): x = L[i] A = binaire(x[1]) B = binaire(x[2]) if x[0] == 1: T = inserer(T, A[1:], B[1:], x[3], 1) else: print(compter(T, A[1:], B[1:], 1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN LIST VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR LIST RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER FUNC_DEF IF VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN VAR IF VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR LIST IF VAR VAR RETURN VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER FUNC_DEF IF VAR LIST RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR LIST RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
n = int(input()) d = {} def lca(u, v, w): res = 0 while u != v: if u < v: v, u = u, v d[u] = d.get(u, 0) + w res += d[u] u = u // 2 return res for i in range(n): l = list(map(int, input().split())) if l[0] == 1: lca(l[1], l[2], l[3]) else: print(lca(l[1], l[2], 0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
q = int(input()) ans = [] cost = {} def getn(v, k=1): res = [] while k <= v: res.append(v) v //= 2 return res def getp(arr): res = [] for i in range(len(arr) - 1): res.append((arr[i], arr[i + 1])) return res def gets(u, v): resu, resv = getn(u), getn(v) for i in resv: if i in resu: return getp(getn(u, i)) + getp(getn(v, i)) for i in range(q): t, *p = map(int, input().split()) u, v = p[0], p[1] if v < u: u, v = v, u path = gets(u, v) if t == 1: for rd in path: if rd in cost: cost[rd] += p[2] else: cost[rd] = p[2] else: tot = 0 for rd in path: if rd in cost: tot += cost[rd] ans.append(tot) print("\n".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF NUMBER ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
q = int(input()) d = {} for sfsdfdsg in range(q): L = [int(x) for x in input().split()] if L[0] == 1: a, b, w = L[1:] while a != b: if a < b: a, b = b, a c = a >> 1 if (c, a) not in d: d[c, a] = 0 d[c, a] += w a = c else: a, b = L[1:] cost = 0 while a != b: if a < b: a, b = b, a c = a >> 1 if (c, a) not in d: d[c, a] = 0 cost += d[c, a] a = c print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
import sys q = int(input()) fees = {} for i in range(q): event_type, v, u, *w = map(int, input().split()) if event_type == 1: w = w[0] u, v = min(u, v), max(u, v) while u != v: parent = v // 2 if v not in fees: fees[v] = 0 fees[v] += w v = parent u, v = min(u, v), max(u, v) elif event_type == 2: u, v = min(u, v), max(u, v) res = 0 while u != v: parent = v // 2 if v in fees: res += fees[v] v = parent u, v = min(u, v), max(u, v) print(res)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
q = int(input()) def full_way(u): res = set() while u >= 1: res.add(u) u //= 2 return res def get_way(u, v): res1 = full_way(u) res2 = full_way(v) m = max(res1 & res2) res = set() for x in res1 | res2: if x > m: res.add(x) return res d = {} for i in range(q): a = input().split() if a[0] == "1": v, u, w = map(int, a[1:]) for x in get_way(u, v): if x not in d: d[x] = 0 d[x] += w else: v, u = map(int, a[1:]) res = 0 for x in get_way(u, v): if x in d: res += d[x] print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
from sys import stdin, stdout n = int(stdin.readline()) prices = {} for i in range(n): s = stdin.readline().strip() if int(s[0]) == 1: ind, a, b, w = map(int, s.split()) while a != b: if a > b: if (a, a // 2) not in prices: prices[a, a // 2] = w else: prices[a, a // 2] += w a //= 2 else: if (b, b // 2) not in prices: prices[b, b // 2] = w else: prices[b, b // 2] += w b //= 2 else: ind, a, b = map(int, s.split()) cnt = 0 while a != b: if a > b: if (a, a // 2) in prices: cnt += prices[a, a // 2] a //= 2 else: if (b, b // 2) in prices: cnt += prices[b, b // 2] b //= 2 stdout.write(str(cnt) + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
def IncreaseCost(x, y, z): a = min(x, y) b = max(x, y) while a != b: while b > a: if not str(b // 2) + " " + str(b) in d: d[str(b // 2) + " " + str(b)] = z else: d[str(b // 2) + " " + str(b)] = d[str(b // 2) + " " + str(b)] + z b = b // 2 a, b = b, a def CountCost(x, y): res = 0 a = min(x, y) b = max(x, y) while a != b: while b > a: if str(b // 2) + " " + str(b) in d: res = res + d[str(b // 2) + " " + str(b)] b = b // 2 a, b = b, a return res q = int(input()) d = {} res = [] for i in range(0, q): line = input().split() if line[0] == "1": IncreaseCost(int(line[1]), int(line[2]), int(line[3])) else: res = res + [CountCost(int(line[1]), int(line[2]))] for i in res: print(i)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR WHILE VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR WHILE VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
we = {} q = int(input()) for i in range(q): l = list(map(int, input().split())) t = l[0] u = l[1] v = l[2] if len(l) == 4: w = l[3] p1 = [u] p2 = [v] while u != 1: u = u // 2 p1 += [u] while v != 1: v = v // 2 p2 += [v] p1 = p1[::-1] p2 = p2[::-1] curr = 0 ans = 0 while curr != min(len(p1), len(p2)) and p1[curr] == p2[curr]: curr += 1 lca = p1[curr - 1] c1 = len(p1) - 1 c2 = len(p2) - 1 while p1[c1] != lca: we[p1[c1]] = we.get(p1[c1], 0) if t == 1: we[p1[c1]] = we.get(p1[c1], 0) + w else: ans += we[p1[c1]] c1 -= 1 while p2[c2] != lca: we[p2[c2]] = we.get(p2[c2], 0) if t == 1: we[p2[c2]] = we.get(p2[c2], 0) + w else: ans += we[p2[c2]] c2 -= 1 if t == 2: print(ans)
ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR LIST VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. <image> Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Output 94 0 32 Note In the example testcase: Here are the intersections used: <image> 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
def way(v): A = [] while v: A.append(v) v //= 2 return A[::-1] def edges_between(u, v): A = way(u) B = way(v) i = 0 while i < min(len(A), len(B)) and A[i] == B[i]: i += 1 C = A[i - 1 :][::-1] + B[i:] D = [] for i in range(len(C) - 1): D.append((C[i], C[i + 1])) return D def make_good(A): return min(A), max(A) def mark(u, v, w, cost): A = edges_between(u, v) for elem in A: new_elem = make_good(elem) if new_elem in cost: cost[new_elem] += w else: cost[new_elem] = w def calc(u, v, cost): A = edges_between(u, v) total = 0 for elem in A: new_elem = make_good(elem) if new_elem in cost: total += cost[new_elem] return total def main(): cost = dict() for i in range(int(input())): tmp = list(map(int, input().split())) if tmp[0] == 1: mark(tmp[1], tmp[2], tmp[3], cost) else: print(calc(tmp[1], tmp[2], cost)) main()
FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
t = eval(input()) for i in range(t): n = eval(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) p = [] k = 1000000000000.0 for i in range(n): k = min(abs(a[0] - b[i]), k) p.append(k) k = 1000000000000.0 for i in range(n): k = min(abs(a[-1] - b[i]), k) p.append(k) k = 1000000000000.0 for i in range(n): k = min(abs(a[i] - b[0]), k) p.append(k) k = 1000000000000.0 for i in range(n): k = min(abs(a[i] - b[-1]), k) p.append(k) la, lb, ra, rb = p[0], p[2], p[1], p[3] ll = abs(a[0] - b[0]) lr = abs(a[0] - b[n - 1]) rl = abs(a[n - 1] - b[0]) rr = abs(a[n - 1] - b[n - 1]) ans = min( la + lb + ra + rb, ll + rr, lr + rl, ll + ra + rb, lr + ra + lb, rl + la + rb, rr + la + lb, ) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
import sys input = sys.stdin.readline def yes(): print("YES") def no(): print("NO") for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 10**10 ans = min(ans, abs(a[0] - b[0]) + abs(a[-1] - b[-1])) ans = min(ans, abs(a[0] - b[-1]) + abs(a[-1] - b[0])) tmp1 = 10**10 tmp2 = 10**10 tmp3 = 10**10 tmp4 = 10**10 for i in range(n): tmp1 = min(tmp1, abs(b[0] - a[i])) tmp2 = min(tmp2, abs(a[0] - b[i])) tmp3 = min(tmp3, abs(b[-1] - a[i])) tmp4 = min(tmp4, abs(a[-1] - b[i])) ans = min(ans, tmp1 + tmp2 + tmp3 + tmp4) ans = min(ans, abs(a[-1] - b[0]) + tmp2 + tmp3) ans = min(ans, abs(a[0] - b[-1]) + tmp1 + tmp4) ans = min(ans, abs(a[0] - b[0]) + tmp3 + tmp4) ans = min(ans, abs(a[-1] - b[-1]) + tmp1 + tmp2) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
R = lambda: [*map(int, input().split())] for _ in [0] * R()[0]: R() a = R() b = R() print( min( sum( min( abs(x[0] - y[0]), sum(min(abs(u[0] - z) for z in v) for u, v in ((x, y), (y, x))), ) for x, y in ((c, d), (c[::-1], d[::-1])) ) for c, d in ((a, b), (a, b[::-1])) ) )
ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
tc_n = int(input()) def read_int_ln(): return [int(x) for x in input().split()] def c(x, y): return abs(x - y) def mc(x, arr): return min(abs(x - y) for y in arr) for tc in range(tc_n): n = int(input()) a = read_int_ln() b = read_int_ln() c0 = c(a[0], b[0]) c1 = c(a[-1], b[-1]) cx01 = c(a[0], b[-1]) cx10 = c(a[-1], b[0]) mca0 = mc(a[0], b) mca1 = mc(a[-1], b) mcb0 = mc(b[0], a) mcb1 = mc(b[-1], a) res = min(c0 + c1, cx01 + cx10) res = min(res, c0 + mca1 + mcb1) res = min(res, c1 + mca0 + mcb0) res = min(res, cx01 + mca1 + mcb0) res = min(res, cx10 + mca0 + mcb1) res = min(res, mca0 + mca1 + mcb0 + mcb1) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
INF = pow(10, 9) def best(arr, val): bst = INF + 10 pos = -1 for i in range(n): if bst > abs(val - arr[i]): bst = abs(val - arr[i]) pos = i return pos t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) bst = INF * 10 cds1 = [0, best(b, a[0]), n - 1] cds2 = [0, best(b, a[n - 1]), n - 1] for i in cds1: for j in cds2: ans = abs(a[0] - b[i]) + abs(a[n - 1] - b[j]) if i > 0 and j > 0: ans += abs(b[0] - a[best(a, b[0])]) if i < n - 1 and j < n - 1: ans += abs(b[n - 1] - a[best(a, b[n - 1])]) bst = min(bst, ans) print(bst)
ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
INF = 10**9 + 1 def main_tc(): n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] min_a_left = INF min_a_right = INF min_b_left = INF min_b_right = INF for elem in b: min_a_left = min(min_a_left, abs(a[0] - elem)) min_a_right = min(min_a_right, abs(a[-1] - elem)) for elem in a: min_b_left = min(min_b_left, abs(b[0] - elem)) min_b_right = min(min_b_right, abs(b[-1] - elem)) answer = 4 * INF answer = min(answer, abs(a[0] - b[0]) + abs(a[-1] - b[-1])) answer = min(answer, abs(a[0] - b[-1]) + abs(a[-1] - b[0])) answer = min(answer, abs(a[0] - b[0]) + min_a_right + min_b_right) answer = min(answer, abs(a[0] - b[-1]) + min_a_right + min_b_left) answer = min(answer, abs(a[-1] - b[0]) + min_a_left + min_b_right) answer = min(answer, abs(a[-1] - b[-1]) + min_a_left + min_b_left) answer = min(answer, min_a_left + min_a_right + min_b_left + min_b_right) print(answer) def main(): tc = int(input()) for _ in range(tc): main_tc() main()
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) p, q, r, s = [10**9] * 4 for i in range(n): p = min(p, abs(a[0] - b[i])) q = min(q, abs(a[-1] - b[i])) r = min(r, abs(b[0] - a[i])) s = min(s, abs(b[-1] - a[i])) t = abs(a[0] - b[0]) u = abs(a[-1] - b[-1]) v = abs(a[0] - b[-1]) w = abs(a[-1] - b[0]) print(min(t + u, v + w, t + q + s, p + r + u, w + p + s, v + q + r, p + q + r + s))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) mna0 = 10**9 mnan = 10**9 mnb0 = 10**9 mnbn = 10**9 for i in range(n): if abs(a[0] - b[i]) < mna0: mna0 = abs(a[0] - b[i]) if abs(a[n - 1] - b[i]) < mnan: mnan = abs(a[n - 1] - b[i]) if abs(b[0] - a[i]) < mnb0: mnb0 = abs(b[0] - a[i]) if abs(b[n - 1] - a[i]) < mnbn: mnbn = abs(b[n - 1] - a[i]) c = min( [ mna0 + mnan + mnb0 + mnbn, abs(a[0] - b[0]) + mnan + mnbn, abs(a[0] - b[n - 1]) + mnb0 + mnan, abs(a[n - 1] - b[0]) + mna0 + mnbn, abs(a[n - 1] - b[n - 1]) + mna0 + mnb0, ] ) c = min( [ c, abs(a[0] - b[0]) + abs(a[n - 1] - b[n - 1]), abs(a[0] - b[n - 1]) + abs(a[n - 1] - b[0]), ] ) print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR LIST BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c1 = abs(a[0] - b[0]) c2 = abs(a[-1] - b[-1]) d1, d2, d3, d4 = [10**18] * 4 for i in range(n): d1 = min(d1, abs(b[i] - a[0])) d2 = min(d2, abs(a[-1] - b[i])) d3 = min(d3, abs(b[0] - a[i])) d4 = min(d4, abs(b[-1] - a[i])) c1 = min(c1, d1 + d3) c2 = min(c2, d2 + d4) f1 = abs(a[0] - b[-1]) f2 = abs(a[-1] - b[0]) ans = c1 + c2 f2 = min(f2, d2 + d3) f1 = min(f1, d1 + d4) ans = min(ans, f1 + f2) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def findmin(x, seq): t = 1000000000 for i in seq: if abs(x - i) < t: t = abs(x - i) choose = i return choose, t t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) ca0, xa0 = findmin(a[0], b) ca1, xa1 = findmin(a[-1], b) cb0, xb0 = findmin(b[0], a) cb1, xb1 = findmin(b[-1], a) match00 = ca0 == b[0] or cb0 == a[0] match01 = ca0 == b[-1] or cb1 == a[0] match10 = ca1 == b[0] or cb0 == a[-1] match11 = ca1 == b[-1] or cb1 == a[-1] value00 = min(xa0 + xb0, abs(a[0] - b[0])) value01 = min(xa0 + xb1, abs(a[0] - b[-1])) value10 = min(xa1 + xb0, abs(a[-1] - b[0])) value11 = min(xa1 + xb1, abs(a[-1] - b[-1])) if match00: value00 = xa0 if ca0 == b[0] else xb0 if match01: value01 = xa0 if ca0 == b[-1] else xb1 if match10: value10 = xa1 if ca1 == b[0] else xb0 if match11: value11 = xa1 if ca1 == b[-1] else xb1 print(min(value00 + value11, value01 + value10))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
for _ in range(int(input())): n = int(input()) l1 = list(map(int, input().strip().split())) l2 = list(map(int, input().strip().split())) lans = min( abs(l1[0] - l2[0]) + abs(l1[-1] - l2[-1]), abs(l1[0] - l2[-1]) + abs(l1[-1] - l2[0]), ) p1 = abs(l1[0] - l2[0]) p2 = abs(l1[-1] - l2[-1]) p3 = abs(l1[0] - l2[-1]) p4 = abs(l1[-1] - l2[0]) lans = min(p1 + p2, p3 + p4) la1, la2, lb1, lb2 = p1, p2, p3, p4 for i in range(n): k1 = abs(l2[0] - l1[i]) k2 = abs(l2[-1] - l1[i]) k3 = abs(l1[0] - l2[i]) k4 = abs(l1[-1] - l2[i]) if k1 < la1: la1 = k1 if k2 < la2: la2 = k2 if k3 < lb1: lb1 = k3 if k4 < lb2: lb2 = k4 print( min( lans, la1 + la2 + lb1 + lb2, p1 + la2 + lb2, p2 + la1 + lb1, p3 + la1 + lb2, p4 + la2 + lb1, ) )
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def solve_problem(): n = int(input()) for _ in range(n): _ = input() row1 = [int(i) for i in input().split()] row2 = [int(i) for i in input().split()] left1 = min(abs(row1[0] - j) for j in row2) left2 = min(abs(row2[0] - j) for j in row1) right1 = min(abs(row1[-1] - j) for j in row2) right2 = min(abs(row2[-1] - j) for j in row1) tmp1 = abs(row1[0] - row2[0]) tmp2 = abs(row1[-1] - row2[-1]) tmp3 = abs(row1[0] - row2[-1]) tmp4 = abs(row1[-1] - row2[0]) tmp5 = left1 + left2 tmp6 = right1 + right2 excep1 = tmp1 + tmp2 excep2 = tmp1 + tmp6 excep3 = tmp3 + left2 + right1 excep4 = tmp2 + tmp5 excep5 = tmp4 + left1 + right2 excep6 = tmp3 + tmp4 result = min(tmp5 + tmp6, excep1, excep2, excep3, excep4, excep5, excep6) print(result) solve_problem()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
t = int(input()) while t: t -= 1 n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) a1 = a[0] a2 = a[-1] b1 = b[0] b2 = b[-1] y1 = 10000000000 y2 = 10000000000 y3 = abs(a[0] - b[0]) y4 = abs(a[-1] - b[0]) y5 = abs(a[0] - b[-1]) y6 = abs(a[-1] - b[-1]) x1 = 10000000000 x2 = 10000000000 for i in a: y1 = min(y1, abs(i - b1)) y2 = min(y2, abs(i - b2)) for i in b: x1 = min(x1, abs(i - a1)) x2 = min(x2, abs(i - a2)) print( min( y3 + y6, y4 + y5, y3 + y2 + x2, y4 + x1 + y2, y5 + x2 + y1, y6 + x1 + y1, x1 + x2 + y1 + y2, ) )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def main(): results = [] number_of_test_cases = int(input()) for _ in range(number_of_test_cases): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) minimum = min( abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[0] - b[-1]) + abs(a[-1] - b[0]) ) minimum_a_start = min([abs(a[0] - b[i]) for i in range(n)]) minimum_a_end = min([abs(a[-1] - b[i]) for i in range(n)]) minimum_b_start = min([abs(a[i] - b[0]) for i in range(n)]) minimum_b_end = min([abs(a[i] - b[-1]) for i in range(n)]) minimum = min(minimum, abs(a[0] - b[0]) + minimum_a_end + minimum_b_end) minimum = min(minimum, abs(a[0] - b[-1]) + minimum_a_end + minimum_b_start) minimum = min(minimum, abs(a[-1] - b[0]) + minimum_a_start + minimum_b_end) minimum = min(minimum, abs(a[-1] - b[-1]) + minimum_a_start + minimum_b_start) minimum = min( minimum, minimum_a_start + minimum_a_end + minimum_b_start + minimum_b_end ) results.append(minimum) for result in results: print(result) main()
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def calculate(arr_a, arr_b): out_a_1 = min(abs(x - arr_a[0]) for x in arr_b) out_a_2 = min(abs(x - arr_b[0]) for x in arr_a) out_b_1 = min(abs(x - arr_a[-1]) for x in arr_b) out_b_2 = min(abs(x - arr_b[-1]) for x in arr_a) return min(abs(arr_a[0] - arr_b[0]), out_a_1 + out_a_2) + min( abs(arr_a[-1] - arr_b[-1]), out_b_1 + out_b_2 ) _ = int(input()) for __ in range(_): n = int(input()) arr_1 = list(map(int, input().split())) arr_2 = list(map(int, input().split())) print(min(calculate(arr_1, arr_2), calculate(arr_1, arr_2[::-1])))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
import sys readline = sys.stdin.readline T = int(readline()) while T: T -= 1 length = int(readline()) a = [int(i) for i in readline().split()] b = [int(i) for i in readline().split()] a_begin = min(abs(a[0] - element) for element in b) a_end = min(abs(a[-1] - element) for element in b) b_begin = min(abs(b[0] - element) for element in a) b_end = min(abs(b[-1] - element) for element in a) begin_connect = abs(a[0] - b[0]) end_connect = abs(a[-1] - b[-1]) left_cross = abs(a[0] - b[-1]) right_cross = abs(a[-1] - b[0]) full_cross = right_cross + left_cross cases = [ begin_connect + end_connect, begin_connect + a_end + b_end, end_connect + a_begin + b_begin, a_begin + a_end + b_begin + b_end, full_cross, left_cross + a_end + b_begin, right_cross + b_end + a_begin, ] print(min(cases))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
import sys input = sys.stdin.readline def solve(n, A, B): ans = min( abs(A[0] - B[0]) + abs(A[-1] - B[-1]), abs(A[0] - B[-1]) + abs(A[-1] - B[0]) ) minA1, minB1 = min([abs(A[0] - b) for b in B]), min([abs(a - B[0]) for a in A]) minA2, minB2 = min([abs(A[-1] - b) for b in B]), min([abs(a - B[-1]) for a in A]) ans = min(ans, abs(A[0] - B[0]) + minA2 + minB2) ans = min(ans, abs(A[-1] - B[-1]) + minA1 + minB1) ans = min(ans, abs(A[0] - B[-1]) + minB1 + minA2) ans = min(ans, abs(A[-1] - B[0]) + minB2 + minA1) ans = min(ans, minA1 + minB1 + minA2 + minB2) print(ans) for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) solve(n, A, B)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
t = int(input()) while t: t = t - 1 n = int(input()) arr = list(map(int, input().split())) brr = list(map(int, input().split())) a = abs(arr[0] - brr[n - 1]) b = abs(arr[0] - brr[0]) d = abs(brr[n - 1] - arr[n - 1]) c = abs(brr[0] - arr[n - 1]) ans = min(a + c, b + d) ans = min(ans, a + b + d) ans = min(ans, c + b + d) a0 = abs(arr[0] - brr[0]) b0 = abs(brr[0] - arr[0]) an = abs(arr[n - 1] - brr[n - 1]) bn = abs(arr[n - 1] - brr[n - 1]) for i in range(n): if abs(arr[0] - brr[i]) < a0: a0 = abs(arr[0] - brr[i]) if abs(arr[n - 1] - brr[i]) < an: an = abs(arr[n - 1] - brr[i]) if abs(brr[0] - arr[i]) < b0: b0 = abs(brr[0] - arr[i]) if abs(brr[n - 1] - arr[i]) < bn: bn = abs(brr[n - 1] - arr[i]) ans = min(ans, b + an + bn) ans = min(ans, a + b0 + an) ans = min(ans, c + a0 + bn) ans = min(ans, a0 + d + b0) ans = min(ans, a0 + b0 + an + bn) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def minfault(a, b): ans = min( abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[0] - b[-1]) + abs(a[-1] - b[0]) ) n = len(a) res = [] for num in [a[0], a[-1]]: tmp = float("inf") for bi in b: tmp = min(tmp, abs(num - bi)) res.append(tmp) for num in [b[0], b[-1]]: tmp = float("inf") for ai in a: tmp = min(tmp, abs(num - ai)) res.append(tmp) ans = min( ans, abs(a[0] - b[0]) + res[1] + res[3], abs(a[0] - b[-1]) + res[1] + res[2], abs(a[-1] - b[0]) + res[0] + res[3], abs(a[-1] - b[-1]) + res[0] + res[2], sum(res), ) return ans t = int(input()) for _ in range(t): _ = input() a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] print(minfault(a, b))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
t = int(input()) for k in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) mi_a_1 = 1e18 mi_a_n = 1e18 mi_b_1 = 1e18 mi_b_n = 1e18 for i in range(n): mi_a_1 = min(abs(a[0] - b[i]), mi_a_1) mi_a_n = min(abs(a[-1] - b[i]), mi_a_n) mi_b_1 = min(abs(b[0] - a[i]), mi_b_1) mi_b_n = min(abs(b[-1] - a[i]), mi_b_n) print( min( abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[0] - b[0]) + mi_a_n + mi_b_n, mi_a_1 + mi_a_n + mi_b_1 + mi_b_n, abs(a[-1] - b[-1]) + mi_a_1 + mi_b_1, abs(a[0] - b[-1]) + abs(a[-1] - b[0]), abs(a[0] - b[-1]) + mi_a_n + mi_b_1, abs(a[-1] - b[0]) + mi_b_n + mi_a_1, ) )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
for i in [0] * int(input()): n = int(input()) a = [*map(int, input().split())] b = [*map(int, input().split())] x = min(abs(a[0] - i) for i in b) y = min(abs(a[n - 1] - b[i]) for i in range(n - 1, -1, -1)) z = min(abs(b[0] - i) for i in a) t = min(abs(b[n - 1] - a[i]) for i in range(n - 1, -1, -1)) print( min( x + y + z + t, abs(a[0] - b[0]) + abs(a[n - 1] - b[n - 1]), abs(a[0] - b[0]) + y + t, abs(a[n - 1] - b[n - 1]) + x + z, abs(a[0] - b[n - 1]) + abs(b[0] - a[n - 1]), abs(a[0] - b[n - 1]) + y + z, abs(b[0] - a[n - 1]) + x + t, ) )
FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def solve_problem(): n = int(input()) for _ in range(n): nb_computers = int(input()) tmp = nb_computers - 1 row1 = [int(i) for i in input().split()] row2 = [int(i) for i in input().split()] result = 10000000000 left1 = min(abs(row1[0] - j) for j in row2) left2 = min(abs(row2[0] - j) for j in row1) right1 = min(abs(row1[-1] - j) for j in row2) right2 = min(abs(row2[-1] - j) for j in row1) excep1 = abs(row1[0] - row2[0]) + abs(row1[-1] - row2[-1]) excep2 = abs(row1[0] - row2[0]) + right1 + right2 excep3 = abs(row1[0] - row2[-1]) + left2 + right1 excep4 = abs(row1[-1] - row2[-1]) + left1 + left2 excep5 = abs(row1[-1] - row2[0]) + left1 + right2 excep6 = abs(row1[0] - row2[-1]) + abs(row1[-1] - row2[0]) result = min( left1 + left2 + right1 + right2, excep1, excep2, excep3, excep4, excep5, excep6, ) print(result) solve_problem()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def findmin(A1, B): ans = abs(A1 - B[0]) for i in B: ans = min(ans, abs(A1 - i)) return ans def join(j1, j2, l1, l2): ans = abs(j1[0] - j2[0]) extra = min(abs(l1[0] - l2[0]), l1[1] + l2[1]) ans += extra return ans for iters in range(int(input())): n = int(input()) A = [int(i) for i in input().split(" ")] B = [int(i) for i in input().split(" ")] if n == 1: print(abs(A[0] - B[0])) continue A1 = [A[0], findmin(A[0], B)] A2 = [A[-1], findmin(A[-1], B)] B1 = [B[0], findmin(B[0], A)] B2 = [B[-1], findmin(B[-1], A)] ans = A1[1] + A2[1] + B1[1] + B2[1] ans = min(ans, join(A1, B1, A2, B2)) ans = min(ans, join(A1, B2, A2, B1)) ans = min(ans, join(A2, B1, A1, B2)) ans = min(ans, join(A2, B2, A1, B1)) print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
t = int(input()) for case in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) af = at = bf = bt = 0 for i in range(n): if abs(a[0] - b[af]) > abs(a[0] - b[i]): af = i if abs(a[-1] - b[at]) > abs(a[-1] - b[i]): at = i if abs(b[0] - a[bf]) > abs(b[0] - a[i]): bf = i if abs(b[-1] - a[bt]) > abs(b[-1] - a[i]): bt = i ans = ( abs(a[0] - b[af]) + abs(a[-1] - b[at]) + abs(b[0] - a[bf]) + abs(b[-1] - a[bt]) ) ans = min( ans, abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[-1] - b[0]) + abs(b[-1] - a[0]), ) ans = min(ans, abs(a[0] - b[0]) + abs(a[-1] - b[at]) + abs(b[-1] - a[bt])) ans = min(ans, abs(a[-1] - b[-1]) + abs(a[0] - b[af]) + abs(b[0] - a[bf])) ans = min(ans, abs(a[-1] - b[0]) + abs(a[0] - b[af]) + abs(b[-1] - a[bt])) ans = min(ans, abs(b[-1] - a[0]) + abs(a[-1] - b[at]) + abs(b[0] - a[bf])) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) bi = [float("inf"), float("inf")] ai = [float("inf"), float("inf")] for i, k in enumerate(a): bi[0] = min(bi[0], abs(k - b[0])) bi[1] = min(bi[1], abs(k - b[-1])) for i, k in enumerate(b): ai[0] = min(ai[0], abs(k - a[0])) ai[1] = min(ai[1], abs(k - a[-1])) res = min( sum(bi) + sum(ai), ai[0] + bi[0] + abs(a[-1] - b[-1]), ai[-1] + bi[-1] + abs(a[0] - b[0]), ai[0] + bi[-1] + abs(a[-1] - b[0]), ai[-1] + bi[0] + abs(a[0] - b[-1]), abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[0] - b[-1]) + abs(a[-1] - b[0]), ) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) m1 = 10**9 m2 = 10**9 p = a[0] q = a[-1] for i in range(len(b)): m1 = min(m1, abs(p - b[i])) m2 = min(m2, abs(q - b[i])) m3 = 10**9 m4 = 10**9 r = b[0] s = b[-1] for i in range(len(b)): m3 = min(m3, abs(r - a[i])) m4 = min(m4, abs(s - a[i])) ans1 = m1 + m2 + m3 + m4 ans2 = min(abs(p - r) + abs(q - s), abs(p - s) + abs(q - r)) ans3 = min( abs(p - r) + m2 + m4, abs(q - s) + m1 + m3, abs(p - s) + m2 + m3, abs(q - r) + m1 + m4, ) print(min(ans1, ans2, ans3))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) a1 = 1000000000 a2 = 1000000000 a3 = 1000000000 a4 = 1000000000 for i in range(n): a1 = min(abs(a[0] - b[i]), a1) a2 = min(abs(a[n - 1] - b[i]), a2) a3 = min(abs(b[0] - a[i]), a3) a4 = min(abs(b[n - 1] - a[i]), a4) ans = min(abs(a[0] - b[0]), a1 + a3) + min(abs(a[n - 1] - b[n - 1]), a2 + a4) ans2 = min(abs(a[0] - b[n - 1]), a1 + a4) + min(abs(a[n - 1] - b[0]), a2 + a3) print(min(ans, ans2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
import sys input = sys.stdin.readline N = int(input()) for i in range(N): n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] m1 = min_cost_a_1 = min([abs(a[0] - b[i]) for i in range(n)]) m2 = min_cost_b_1 = min([abs(b[0] - a[i]) for i in range(n)]) m3 = min_cost_a_n = min([abs(a[n - 1] - b[i]) for i in range(n)]) m4 = min_cost_b_n = min([abs(b[n - 1] - a[i]) for i in range(n)]) print( min( [ m1 + m2 + abs(a[n - 1] - b[n - 1]), m1 + m4 + abs(a[n - 1] - b[0]), m3 + m2 + abs(a[0] - b[n - 1]), m3 + m4 + abs(a[0] - b[0]), abs(a[0] - b[0]) + abs(a[n - 1] - b[n - 1]), abs(a[n - 1] - b[0]) + abs(a[0] - b[n - 1]), m1 + m2 + m3 + m4, ] ) )
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def solve_problem(): for _ in range(int(input())): _ = input() row1 = [int(i) for i in input().split()] row2 = [int(i) for i in input().split()] r10 = row1[0] r20 = row2[0] r11 = row1[-1] r21 = row2[-1] diff0 = abs(r10 - r20) diff1 = abs(r11 - r21) left1 = min(abs(r10 - j) for j in row2) left2 = min(abs(r20 - j) for j in row1) right1 = min(abs(r11 - j) for j in row2) right2 = min(abs(r21 - j) for j in row1) left = left1 + left2 right = right1 + right2 res = left + right excep1 = diff0 + diff1 if excep1 < res: res = excep1 excep2 = diff0 + right if excep2 < res: res = excep2 excep3 = abs(r10 - r21) + left2 + right1 if excep3 < res: res = excep3 excep4 = diff1 + left if excep4 < res: res = excep4 excep5 = abs(r11 - r20) + left1 + right2 if excep5 < res: res = excep5 excep6 = abs(r10 - r21) + abs(r11 - r20) if excep6 < res: res = excep6 print(res) solve_problem()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def md(a, b): mn = 999999999 for el in b: if abs(el - a) < mn: mn = abs(el - a) return mn t = int(input()) for i in range(t): n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] f = abs(a[0] - b[0]) + abs(a[-1] - b[-1]) p = ( abs(md(a[0], b[1:])) + abs(md(b[0], a[1:])) + abs(md(a[-1], b[: n - 1])) + abs(md(b[-1], a[: n - 1])) ) q = abs(a[0] - b[0]) + abs(md(a[-1], b[: n - 1])) + abs(md(b[-1], a[: n - 1])) r = abs(a[-1] - b[-1]) + abs(md(a[0], b[1:])) + abs(md(b[0], a[1:])) w = abs(a[0] - b[-1]) + abs(a[-1] - b[0]) a1 = abs(a[0] - b[-1]) + abs(md(b[0], a[1:])) + abs(md(a[-1], b[: n - 1])) b1 = abs(a[-1] - b[0]) + abs(md(a[0], b[1:])) + abs(md(b[-1], a[: n - 1])) print(min(f, p, q, r, w, a1, b1))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def find_best_index(ai, b): summr = 10**30 for j in range(len(b)): if abs(ai - b[j]) < summr: summr = abs(ai - b[j]) index = j return index def func(a, b, n): result = 10**30 List_1 = [0, find_best_index(a[0], b), n - 1] List_2 = [0, find_best_index(a[n - 1], b), n - 1] for i in List_1: for j in List_2: sum = abs(a[0] - b[i]) + abs(a[n - 1] - b[j]) if i > 0 and j > 0: sum += abs(b[0] - a[find_best_index(b[0], a)]) if i < n - 1 and j < n - 1: sum += abs(b[n - 1] - a[find_best_index(b[n - 1], a)]) result = min(result, sum) return result t = int(input()) for _ in range(t): n = int(input()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] print(func(a, b, n))
FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow. The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row. It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the minimum total cost to make a fault-tolerant network. -----Examples----- Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Output 31 1999999998 -----Note----- In the first test case, it's optimal to connect four pairs of computers: computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$; computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$; computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$; In total, $3 + 3 + 10 + 15 = 31$. In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
def best(A, i): return min([abs(i - j) for j in A]) def solv(A, B): a1, an = A[0], A[-1] b1, bn = B[0], B[-1] a1b = best(B, a1) anb = best(B, an) b1b = best(A, b1) bnb = best(A, bn) d11 = min(abs(a1 - b1), a1b + b1b) dnn = min(abs(an - bn), anb + bnb) d1n = min(abs(a1 - bn), a1b + bnb) dn1 = min(abs(an - b1), anb + b1b) return min(d11 + dnn, d1n + dn1) for _ in range(int(input())): input() A = list(map(int, input().split())) B = list(map(int, input().split())) print(solv(A, B))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR