description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≀ N ≀ 10^{5} 1 ≀ A_{i} ≀ 10^{5}
class Solution: def sortByFreq(self, a, n): ans = [] dic = dict() for i in a: if i in dic: dic[i] += 1 else: dic[i] = 1 lst = list(dic.items()) lst.sort(key=lambda a: (-a[1], a[0])) for k, v in lst: for i in range(v): ans.append(k) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≀ N ≀ 10^{5} 1 ≀ A_{i} ≀ 10^{5}
class Solution: def sortByFreq(self, a, n): dic = {} a.sort() for each in a: if each in dic: dic[each] += 1 else: dic[each] = 1 res = [] for each in a: res.append([each, dic[each]]) res.sort(key=lambda x: x[1], reverse=True) res1 = [] for each in res: res1.append(each[0]) return res1
CLASS_DEF FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≀ N ≀ 10^{5} 1 ≀ A_{i} ≀ 10^{5}
class Solution: def sortByFreq(self, a, n): d = {} for i in a: if i not in d: d[i] = 1 else: d[i] += 1 res = sorted(a, key=lambda x: (d[x], -x), reverse=True) return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≀ N ≀ 10^{5} 1 ≀ A_{i} ≀ 10^{5}
class Solution: def sortByFreq(self, a, n): dic = {} for elem in a: dic[elem] = dic.get(elem, 0) + 1 sorteddic = sorted(dic.items(), key=lambda x: (x[1], -x[0]), reverse=True) ansAr = [] for k, v in sorteddic: ansAr.extend([k] * v) return ansAr
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR RETURN VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
n = int(input()) s, p = {}, [""] * n for k in range(n): p[k] = t = input() for q in [t[i:j] for i in range(9) for j in range(i + 1, 10)]: s[q] = -1 if q in s and s[q] != k else k for q, k in s.items(): if k >= 0 and len(p[k]) > len(q): p[k] = q print("\n".join(p))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR DICT BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
n = int(input()) num = [] for i in range(n): num.append(input()) d = {} q = [] for i in range(n): r = {} g = [] for j in range(0, 9): for l in range(j + 1, 10): t = num[i][j:l] g.append(t) if t in r: continue r[t] = 1 if t in d: d[t] = d[t] + 1 else: d[t] = 1 g.sort(key=len) q.append(g) if n == 1: print(num[0][0]) exit(0) answ = [] for i in range(n): for j in q[i]: if d[j] == 1: answ.append(j) break for i in answ: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
n = int(input()) m = {} a = [] for i in range(n): s = input() sub = set() for i in range(len(s)): for j in range(i, len(s)): k = s[i : j + 1] if k in sub: continue else: sub.add(k) if k in m: m[k] += 1 else: m[k] = 1 a.append(s) for s in a: ans = s for i in range(len(s)): for j in range(i, len(s)): k = s[i : j + 1] if m[k] == 1 and len(k) < len(ans): ans = k print(ans)
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 ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
cnt = {} n = int(input()) ls = [input() for _ in range(n)] for s in ls: subs = set() for end in range(10): for start in range(end): subs.add(s[start:end]) for sub in subs: if sub not in cnt: cnt[sub] = 1 else: cnt[sub] += 1 for s in ls: subs = set() for end in range(10): for start in range(end): subs.add(s[start:end]) for sub in sorted(subs, key=len): if cnt[sub] == 1: print(sub) break
ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
n = int(input()) numbers = [input() for i in range(n)] telep = {} count = 1 for number in numbers: for l in range(1, len(number) + 1): for i in range(0, len(number) - l + 1): key = number[i : i + l] if key in telep and telep[key] != count: telep[key] = -1 else: telep[key] = count count += 1 reverse = {} for key in telep: if telep[key] == -1: continue elif telep[key] not in reverse: reverse[telep[key]] = key elif len(key) < len(reverse[telep[key]]): reverse[telep[key]] = key for i in range(1, n + 1): print(reverse[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
indexs = [] for length in range(1, 10): for start in range(0, 9 - length + 1): indexs.append([start, start + length]) n = int(input()) used = {} s = [] for i in range(n): s.append(input()) for un in set(s[-1][index[0] : index[1]] for index in indexs): used[un] = used.get(un, 0) + 1 for num in s: for index in indexs: if used[num[index[0] : index[1]]] == 1: print(num[index[0] : index[1]]) break
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
n = int(input()) l = [] for i in range(n): l.append(input()) d = {} for k in l: for i in range(1, 9 + 1): for j in range(9 - i + 1): a = k[j : j + i] z = d.keys() if a in z: if k != d.get(a)[1]: w = d.get(a) w[0] += 1 w[1] = k d.update({a: w}) else: d.update({a: [1, k]}) r = list(d.items()) ans = [] for i in r: if i[1][0] == 1: ans.append([i[0], i[1][1]]) d1 = {} for i in ans: z = d1.keys() if i[1] in z: if len(str(i[0])) < len(str(d1.get(i[1]))): d1.update({i[1]: i[0]}) else: d1.update({i[1]: i[0]}) for i in l: print(d1.get(i))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR DICT VAR VAR EXPR FUNC_CALL VAR DICT VAR LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR DICT VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
def podstroki(s): return sorted(set(s[i:i1] for i in range(9) for i1 in range(i + 1, 10)), key=len) res = {} spisok = [podstroki(input()) for i in range(int(input()))] for s in spisok: for podstr in s: if podstr in res: res[podstr] += 1 else: res[podstr] = 1 for s in spisok: for podstr in s: if res[podstr] == 1: print(podstr) break
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
n = int(input()) maps = {} for i in range(n): string = input() for j in range(len(string)): for a in range(0, j + 1): substr = string[a : j + 1] if substr not in maps: maps[substr] = {"index": i, "count": 1} elif i != maps[substr]["index"]: maps[substr]["count"] += 1 maps = sorted(maps.items(), key=lambda i: i[1]["index"]) count = 0 min_v = "" min_count = 10 for k, value in maps: if count < value["index"]: count += 1 print(min_v) min_v = "idk" min_count = 10 if value["count"] == 1: if len(k) < min_count: min_count = len(k) min_v = k print(min_v)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR DICT STRING STRING VAR NUMBER IF VAR VAR VAR STRING VAR VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR STRING NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
n = int(input()) mass = [] for i in range(n): a = input() mass.append(a) a = dict() for i in range(len(mass)): for j in range(len(mass[i])): for k in range(j + 1, len(mass[i]) + 1): if mass[i][j:k] in a: if a[mass[i][j:k]][1] != i: a[mass[i][j:k]][0] += 1 a[mass[i][j:k]][1] = i else: a[mass[i][j:k]] = [1, i] for i in range(len(mass)): ans = "1" * 11 for j in range(len(mass[i])): for k in range(j + 1, len(mass[i]) + 1): if a[mass[i][j:k]][0] == 1: if k - j < len(ans): ans = mass[i][j:k] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: * if he enters 00 two numbers will show up: 100000000 and 100123456, * if he enters 123 two numbers will show up 123456789 and 100123456, * if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer n (1 ≀ n ≀ 70000) β€” the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91
class CodeforcesTask860BSolution: def __init__(self): self.result = "" self.n = 0 self.phones = [] def read_input(self): self.n = int(input()) for x in range(self.n): self.phones.append(input()) def process_task(self): phone_book = {} for phone in self.phones: for x in range(1, len(phone) + 1): for y in range(len(phone) - x + 1): subnum = phone[y : y + x] if subnum in phone_book.keys(): if phone_book[subnum] != phone: phone_book[subnum] = False else: phone_book[subnum] = phone results = [] for phone in self.phones: found = False for x in range(1, len(phone) + 1): for y in range(len(phone) - x + 1): subnum = phone[y : y + x] if subnum in phone_book.keys(): if phone_book[subnum]: found = True results.append(subnum) break if found: break self.result = "\n".join([str(x) for x in results]) def get_result(self): return self.result Solution = CodeforcesTask860BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): dict = {} for i in range(n): for j in range(m): if not i - j in dict: dict[i - j] = [matrix[i][j]] else: dict[i - j].append(matrix[i][j]) for k, v in dict.items(): if k > 0: v.sort(reverse=True) elif k < 0: v.sort() else: v[:] = v[::-1] for i in range(n): for j in range(m): matrix[i][j] = dict[i - j].pop()
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
def diagonalSort1(mat): n = len(mat) m = len(mat[0]) v = list() for row in range(1, n): for i, j in zip(range(row, n), range(0, m)): v.append(mat[i][j]) v.sort(reverse=False) t = 0 for i, j in zip(range(row, n), range(0, m)): mat[i][j] = v[t] t += 1 v.clear() for col in range(1, m): for i, j in zip(range(0, n), range(col, m)): v.append(mat[i][j]) v.sort(reverse=True) t = 0 for i, j in zip(range(0, n), range(col, m)): mat[i][j] = v[t] t += 1 v.clear() class Solution: def diagonalSort(self, matrix, n, m): diagonalSort1(matrix)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): lower_triangle = [[] for i in range(n)] upper_triangle = [[] for i in range(m)] major_diagonal = [] for i in range(n): for j in range(m): if j < i: lower_triangle[i - j].append(matrix[i][j]) elif j > i: upper_triangle[j - i].append(matrix[i][j]) else: major_diagonal.append(matrix[i][j]) for i in range(n): lower_triangle[i].sort() lower_triangle[i] = lower_triangle[i][::-1] for i in range(m): upper_triangle[i].sort() for i in range(n): for j in range(m): if j < i: d = i - j l = len(lower_triangle[d]) - 1 matrix[i][j] = lower_triangle[d][l] lower_triangle[d].pop() elif j > i: d = j - i l = len(upper_triangle[d]) - 1 matrix[i][j] = upper_triangle[d][l] upper_triangle[d].pop()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): for k in range(1, n): j = 0 i = k arr1 = [] while i < n and j < m: arr1.append(matrix[i][j]) i += 1 j += 1 arr1.sort() j = 0 i = k while i < n and j < m: matrix[i][j] = arr1[j] i += 1 j += 1 for k in range(1, m): j = k i = 0 arr1 = [] while i < n and j < m: arr1.append(matrix[i][j]) i += 1 j += 1 arr1.sort(reverse=True) j = k i = 0 while i < n and j < m: matrix[i][j] = arr1[i] i += 1 j += 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def find(self, i, j, arr, m, n, rev): yt = [] ii = i jj = j while ii < n and jj < m: yt.append(arr[ii][jj]) ii += 1 jj += 1 if not rev: yt.sort(reverse=True) else: yt.sort() a = i b = j tmp = 0 while a < n and b < m: arr[a][b] = yt[tmp] a += 1 b += 1 tmp += 1 def diagonalSort(self, matrix, n, m): for i in range(1, m): self.find(0, i, matrix, m, n, False) for i in range(1, n): self.find(i, 0, matrix, m, n, True)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, N, M): orgi = 1 i = 1 j = 0 for i in range(1, N - 1): k = i j = 0 arr = [] while k < N and j < M: arr.append(matrix[k][j]) k += 1 j += 1 arr.sort() k = i j = 0 l = 0 while k < N and j < M and l < len(arr): matrix[k][j] = arr[l] k += 1 j += 1 l += 1 i = 0 j = 1 for j in range(1, M - 1): k = j i = 0 arr = [] while k < M and i < N: arr.append(matrix[i][k]) k += 1 i += 1 arr.sort(reverse=True) k = j i = 0 l = 0 while k < M and i < N and l < len(arr): matrix[i][k] = arr[l] k += 1 i += 1 l += 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): k = dict() for i in range(n): for j in range(m): if k.get(j - i): k[j - i].append(matrix[i][j]) else: k[j - i] = [matrix[i][j]] for keys in k.keys(): if keys != 0: if keys < 0: k[keys].sort(reverse=True) else: k[keys].sort() for i in range(n): for j in range(m): if i - j == 0: continue matrix[i][j] = k[j - i].pop()
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): for j in range(1, m): items = [] for i in range(min(n, m - j)): items.append(matrix[i][i + j]) items.sort(reverse=True) for i in range(min(n, m - j)): matrix[i][i + j] = items[i] for i in range(1, n): items = [] for j in range(min(m, n - i)): items.append(matrix[i + j][j]) items.sort() for j in range(min(m, n - i)): matrix[i + j][j] = items[j] return matrix
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, mat, m, n): for i in range(1, m): j = i k = 0 li = [] while j < m and k < n: li.append(mat[j][k]) j += 1 k += 1 li.sort() j = i k = 0 l = 0 while j < m and k < n: mat[j][k] = li[l] j += 1 k += 1 l += 1 for i in range(1, n): j = 0 k = i li = [] while j < m and k < n: li.append(mat[j][k]) j += 1 k += 1 li.sort() h = len(li) j = 0 k = i l = h - 1 while j < m and k < n: mat[j][k] = li[l] j += 1 k += 1 l -= 1 return mat if __name__ == "__main__": tc = int(input()) while tc > 0: n, m = map(int, input().strip().split()) inputLine = list(map(int, input().strip().split())) matrix = [[(0) for j in range(m)] for i in range(n)] for i in range(n): for j in range(m): matrix[i][j] = inputLine[i * m + j] ob = Solution() ob.diagonalSort(matrix, n, m) for i in range(n): for j in range(m): print(matrix[i][j], end=" ") print() tc -= 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR 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 NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): arr = [] for first in range(1, m): j = first i = 0 arr.clear() while i < n and j < m: arr.append(matrix[i][j]) i += 1 j += 1 arr.sort(reverse=True) j = first i = 0 k = 0 while k < len(arr): matrix[i][j] = arr[k] i += 1 j += 1 k += 1 for first in range(1, n): i = first j = 0 arr.clear() while i < n and j < m: arr.append(matrix[i][j]) i += 1 j += 1 arr.sort() i = first j = 0 k = 0 while k < len(arr): matrix[i][j] = arr[k] i += 1 j += 1 k += 1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): return rt(matrix) def rt(m): for i in range(1, len(m[0])): p = [] v = i j = 0 while 0 <= v < len(m[0]) and 0 <= j < len(m): p.append(m[j][v]) v += 1 j += 1 j = 0 p.sort(reverse=True) r = 0 v = i while 0 <= v < len(m[0]) and 0 <= j < len(m): m[j][v] = p[r] j += 1 v += 1 r += 1 for i in range(1, len(m)): p = [] v = i j = 0 while 0 <= v < len(m) and 0 <= j < len(m[0]): p.append(m[v][j]) v += 1 j += 1 j = 0 r = 0 v = i p.sort() while 0 <= v < len(m) and 0 <= j < len(m[0]): m[v][j] = p[r] j += 1 v += 1 r += 1 return m
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR WHILE NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def sortFirstHalf(self, matrix, n, m): start, gap = n - 1, n - 1 while gap > 0: temp = [] i = start while i < n and i - gap < m: temp.append(matrix[i][i - gap]) i += 1 temp.sort() i = start while i < n and i - gap < m: matrix[i][i - gap] = temp.pop(0) i += 1 gap -= 1 start -= 1 return matrix def sortSecondHalf(self, matrix, n, m): cnt = 0 start, gap = 0, 1 while gap < m: temp = [] i = start while i < n and i + gap < m: temp.append(matrix[i][i + gap]) i += 1 temp.sort(reverse=True) i = start while i < n and i + gap < m: matrix[i][i + gap] = temp.pop(0) i += 1 gap += 1 def diagonalSort(self, matrix, n, m): matrix = self.sortFirstHalf(matrix, n, m) matrix = self.sortSecondHalf(matrix, n, m) return matrix if __name__ == "__main__": tc = int(input()) while tc > 0: n, m = map(int, input().strip().split()) inputLine = list(map(int, input().strip().split())) matrix = [[(0) for j in range(m)] for i in range(n)] for i in range(n): for j in range(m): matrix[i][j] = inputLine[i * m + j] ob = Solution() ob.diagonalSort(matrix, n, m) for i in range(n): for j in range(m): print(matrix[i][j], end=" ") print() tc -= 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR 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 NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): positiveDiff = [[] for i in range(m)] negativeDiff = [[] for j in range(n)] for i in range(n): for j in range(m): if i != j: if j > i: positiveDiff[j - i].append(matrix[i][j]) else: negativeDiff[i - j].append(matrix[i][j]) for i in range(m): positiveDiff[i].sort() for i in range(n): negativeDiff[i].sort(reverse=True) for i in range(n): for j in range(m): if i != j: if j > i: matrix[i][j] = positiveDiff[j - i].pop() else: matrix[i][j] = negativeDiff[i - j].pop()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): for i in range(1, n): x = [] j = 0 k = i while j < m and k < n: x.append(matrix[k][j]) j += 1 k += 1 x = sorted(x) a = 0 b = i while a < m and b < n: matrix[b][a] = x[a] a += 1 b += 1 high = [] for i in range(1, m): x = [] j = 0 k = i while j < n and k < m: x.append(matrix[j][k]) j += 1 k += 1 x = sorted(x, reverse=True) high.append(x) a = 0 b = i while a < n and b < m: matrix[a][b] = x[a] a += 1 b += 1 return matrix
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def fun(): arr = matrix for itr in range(1, m - 1): mm = m - itr nn = n - itr - 1 for r in range(0, nn): for c in (r + 1, mm): print("r=", r, " c=", c) if arr[r][c] < arr[r + 1][c + 1]: t = arr[r][c] arr[r][c] = arr[r + 1][c + 1] arr[r + 1][c + 1] = t def diagonalSort(self, matrix, n, m): for itr in range(1, m - 1): i = 0 j = itr ar = [] while i < n and j < m: ar.append(matrix[i][j]) i += 1 j += 1 ar.sort(reverse=True) i = 0 j = itr while i < n and j < m: matrix[i][j] = ar[i] i += 1 j += 1 for itr in range(1, n - 1): i = itr j = 0 ar = [] while i < n and j < m: ar.append(matrix[i][j]) i += 1 j += 1 ar.sort() i = itr j = 0 while i < n and j < m: matrix[i][j] = ar[j] i += 1 j += 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): diag = {} for i in range(n): for j in range(m): if i - j in diag.keys(): diag[i - j].append(matrix[i][j]) else: diag[i - j] = [matrix[i][j]] for i in range(n): for j in range(m): if i - j in diag.keys() and i - j != 0: if i - j < 0: diag[i - j].sort() else: diag[i - j].sort(reverse=True) matrix[i][j] = diag[i - j].pop()
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): r = 0 k = m - 2 c = k while k >= 1: temp = [] while c <= m - 1 and r <= n - 1 and r >= 0: temp.append(matrix[r][c]) c += 1 r += 1 r = 0 temp.sort() temp = temp[::-1] c = k i = 0 while c <= m - 1 and r <= n - 1 and r >= 0: matrix[r][c] = temp[i] c += 1 r += 1 i += 1 r = 0 k -= 1 c = k r = 1 k = 1 c = 0 while k <= n - 1: temp = [] while r <= n - 1 and c <= m - 1 and c >= 0: temp.append(matrix[r][c]) c += 1 r += 1 r = k temp.sort() c = 0 i = 0 while r <= n - 1 and c <= m - 1 and c >= 0: matrix[r][c] = temp[i] c += 1 r += 1 i += 1 k += 1 r = k c = 0 return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): des = {} acs = {} dig = [] for i in range(n): for j in range(m): if i == j: dig.append(matrix[i][j]) elif i > j: if i - j not in acs: acs[i - j] = [matrix[i][j]] else: acs[i - j].append(matrix[i][j]) elif j - i not in des: des[j - i] = [matrix[i][j]] else: des[j - i].append(matrix[i][j]) for i in acs: acs[i].sort() for i in des: des[i].sort() des[i] = des[i][::-1] for i in range(n): for j in range(m): if i == j: matrix[i][j] = dig.pop(0) elif i > j: matrix[i][j] = acs[i - j].pop(0) else: matrix[i][j] = des[j - i].pop(0) return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): upper = [] for i in range(m - 1): upper.append([]) lower = [] for i in range(n - 1): lower.append([]) for i in range(0, n): for j in range(i + 1, m): upper[j - i - 1].append(matrix[i][j]) for i in range(0, n): for j in range(0, min(i, m)): lower[i - j - 1].append(matrix[i][j]) for l in upper: l.sort() for l in lower: l.sort(reverse=True) for i in range(0, n): for j in range(i + 1, m): matrix[i][j] = upper[j - i - 1].pop() for i in range(0, n): for j in range(0, min(i, m)): matrix[i][j] = lower[i - j - 1].pop()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): left = [] right = [] l = [] for i in range(n): left.append([]) for j in range(m): right.append([]) for i in range(n): for j in range(m): if i > j: left[i - j].append(matrix[i][j]) if j > i: right[j - i].append(matrix[i][j]) for i in left: i.sort() i.reverse() for i in right: i.sort() for i in range(n): for j in range(m): if j > i: matrix[i][j] = right[j - i].pop() if i > j: matrix[i][j] = left[i - j].pop()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): dict1 = {} for i in range(n): for j in range(m): if j - i not in dict1: dict1[j - i] = [matrix[i][j]] else: z = dict1[j - i] z.append(matrix[i][j]) for i in dict1: if i != 0: dict1[i] = sorted(dict1[i]) matrix.clear() for i in range(n): arr = [] for j in range(m): z = j - i if z == 0: arr.append(dict1[0].pop(0)) elif z > 0: arr.append(dict1[z].pop(-1)) elif z < 0: arr.append(dict1[z].pop(0)) matrix.append(arr)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): for s in range(1, n): tmp_list = [] i, j = s, 0 k = 0 while i + k < n and j + k < m: tmp_list.append(matrix[i + k][j + k]) k += 1 tmp_list.sort() k = 0 while i + k < n and j + k < m: matrix[i + k][j + k] = tmp_list[k] k += 1 for s in range(1, m): i, j = 0, s tmp_list = [] k = 0 while i + k < n and j + k < m: tmp_list.append(matrix[i + k][j + k]) k += 1 tmp_list.sort(key=lambda x: -1 * x) k = 0 while i + k < n and j + k < m: matrix[i + k][j + k] = tmp_list[k] k += 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, mat, n, m): for x in range(1, len(mat[0])): a, b = x, 0 l = [] while a < len(mat[0]) and b < len(mat): l.append(mat[b][a]) a += 1 b += 1 l.sort() l.reverse() a -= 1 b -= 1 while l: mat[b][a] = l.pop() a -= 1 b -= 1 for x in range(1, len(mat)): a, b = 0, x l = [] while a < len(mat[0]) and b < len(mat): l.append(mat[b][a]) a += 1 b += 1 l.sort() a -= 1 b -= 1 while l: mat[b][a] = l.pop() a -= 1 b -= 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): l = [] r = [] for x in range(1, n): lri = x lci = 0 temp = [] while lri < n and lci < m: temp.append(matrix[lri][lci]) lri += 1 lci += 1 temp.sort() l.append(temp) for x in range(1, m): rri = 0 rci = x temp = [] while rri < n and rci < m: temp.append(matrix[rri][rci]) rri += 1 rci += 1 temp.sort(reverse=True) r.append(temp) lidx = 0 for x in range(1, n): lri = x lci = 0 temp = l[lidx] j = 0 while j < len(temp) and lri < n and lci < m: matrix[lri][lci] = temp[j] lri += 1 lci += 1 j += 1 lidx += 1 ridx = 0 for x in range(1, m): lri = 0 lci = x temp = r[ridx] j = 0 while j < len(temp) and lri < n and lci < m: matrix[lri][lci] = temp[j] lri += 1 lci += 1 j += 1 ridx += 1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): groups_dict = {} for i in range(n): for j in range(m): group = i - j if group in groups_dict.keys(): groups_dict[group].append(matrix[i][j]) else: groups_dict[group] = [matrix[i][j]] for k, v in groups_dict.items(): if k < 0: groups_dict[k] = sorted(groups_dict[k], reverse=True) elif k > 0: groups_dict[k] = sorted(groups_dict[k]) for i in range(n): for j in range(m): matrix[i][j] = groups_dict[i - j].pop(0) return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): d = dict() for i in range(1, n): d[i] = [] for i in range(1, m): d[-i] = [] for i in range(n): for j in range(m): if i == j: continue elif i > j: d[i - j].append(matrix[i][j]) else: d[i - j].append(matrix[i][j]) for i in d.keys(): if i < 0: d[i].sort() else: d[i].sort(reverse=True) for i in range(n): for j in range(m): if i == j: continue elif i > j: matrix[i][j] = d[i - j].pop() else: matrix[i][j] = d[i - j].pop() return matrix if __name__ == "__main__": tc = int(input()) while tc > 0: n, m = map(int, input().strip().split()) inputLine = list(map(int, input().strip().split())) matrix = [[(0) for j in range(m)] for i in range(n)] for i in range(n): for j in range(m): matrix[i][j] = inputLine[i * m + j] ob = Solution() ob.diagonalSort(matrix, n, m) for i in range(n): for j in range(m): print(matrix[i][j], end=" ") print() tc -= 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR 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 NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): i = 1 j = 0 while i < m: j = 0 arr = sorted( [matrix[j][j + i] for j in range(n) if j + i < m], reverse=True ) for k in range(n): if k + i >= m: break matrix[k][k + i] = arr[k] i += 1 i = 1 j = 0 while i < n: j = 0 arr = sorted([matrix[j + i][j] for j in range(m) if j + i < n]) for k in range(m): if k + i >= n: break matrix[k + i][k] = arr[k] i += 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, mat, n, m): d = {} for row in range(n): for col in range(m): if not d.get(row - col): d[row - col] = [] d[row - col].append(mat[row][col]) for k, v in d.items(): if k < 0: d[k] = sorted(d[k]) elif k > 0: d[k] = sorted(d[k], reverse=True) for row in range(n): for col in range(m): if row == col: continue mat[row][col] = d[row - col].pop()
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): start_num = [(0, x) for x in range(m)] + [(y, 0) for y in range(n)] start_num.pop(0) for x, y in start_num: if x == y: continue step = min(abs(n - x - 1), abs(m - y - 1)) line_list = [(x + i, y + i) for i in range(step + 1)] point_list = [matrix[i][j] for i, j in line_list] if x < y: point_list.sort(reverse=True) else: point_list.sort() for i, j in line_list: matrix[i][j] = point_list.pop(0) return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER RETURN VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, mat, n, m): ti = 1 tj = 0 while ti < n: tmp = [] i = ti j = 0 while i < n and j < m: tmp.append(mat[i][j]) i += 1 j += 1 tmp = sorted(tmp) i = ti j = 0 k = 0 while k < len(tmp): mat[i][j] = tmp[k] i += 1 j += 1 k += 1 ti += 1 ti = 1 while ti < m: tmp = [] j = ti i = 0 while i < n and j < m: tmp.append(mat[i][j]) i += 1 j += 1 tmp = sorted(tmp, reverse=True) i = 0 j = ti k = 0 while k < len(tmp): mat[i][j] = tmp[k] i += 1 j += 1 k += 1 ti += 1 return mat
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): for j in range(1, m): x, y = 0, j v = [] while 0 <= x < n and 0 <= y < m: v.append(matrix[x][y]) x += 1 y += 1 v.sort(reverse=True) k = 0 x, y = 0, j while 0 <= x < n and 0 <= y < m: matrix[x][y] = v[k] x += 1 y += 1 k += 1 for i in range(1, n): x, y = i, 0 v = [] while 0 <= x < n and 0 <= y < m: v.append(matrix[x][y]) x += 1 y += 1 v.sort() k = 0 x, y = i, 0 while 0 <= x < n and 0 <= y < m: matrix[x][y] = v[k] x += 1 y += 1 k += 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR LIST WHILE NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): for col in range(1, m): f = 0 c = col arriba = [] while f < n and c < m: arriba.append(matrix[f][c]) f += 1 c += 1 arriba = sorted(arriba) f = 0 c = col index = len(arriba) - 1 while f < n and c < m: matrix[f][c] = arriba.pop() f += 1 c += 1 for fila in range(1, n): abajo = [] f = fila c = 0 while f < n and c < m: abajo.append(matrix[f][c]) f += 1 c += 1 f = fila c = 0 abajo = list(reversed(sorted(abajo))) index = 0 while f < n and c < m: matrix[f][c] = abajo.pop() f += 1 c += 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): for gap in range(1, m): i, j = 0, gap temp = [] while j < m and i < n: temp.append(matrix[i][j]) j += 1 i += 1 i, j = 0, gap temp.sort() while j < m and i < n: matrix[i][j] = temp.pop() j += 1 i += 1 for gap in range(1, n): i, j = gap, 0 temp = [] while i < n and j < m: temp.append(matrix[i][j]) i += 1 j += 1 i, j = gap, 0 temp.sort(reverse=True) while i < n and j < m: matrix[i][j] = temp.pop() i += 1 j += 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Given an NxM 2D matrix, rearrange such that Each diagonal in the lower left triangle of the rectangular grid is sorted in ascending order. Each diagonal in the upper right triangle of the rectangular grid is sorted in descending order. The major diagonal in the grid starting from the top-left corner is not rearranged. Example 1: Input: N = 4, M = 5 matrix = {{3 6 3 8 2}, {4 1 9 5 9}, {5 7 2 4 8}, {8 3 1 7 6}} Output: 3 9 8 9 2 1 1 6 5 8 3 4 2 6 3 8 5 7 7 4 Explanation: Before: After: Your Task: You don't need to read input or print anything. Your task is to complete the function diagonalSort() which takes the matrix, n and m as input parameter and rearranges the elements of the matrix. Expected Time Complexity: O(NxM) Expected Auxiliary Space: O(N+M) Constraints: 1 <= N,M <= 10^{4 }, 1<=N*M<=10^{5} 1 <= matrix[i] <= 10^{3}
class Solution: def diagonalSort(self, matrix, n, m): dictt = {} for r in range(n): for c in range(m): if r - c == 0: continue if r - c in dictt.keys(): dictt[r - c].append(matrix[r][c]) else: dictt[r - c] = [matrix[r][c]] for k, v in dictt.items(): if k > 0: v.sort() else: v.sort(reverse=True) for r in range(n): for c in range(m): if r - c in dictt.keys(): matrix[r][c] = dictt[r - c].pop(0)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for i in range(int(input())): n, m = map(int, input().split()) a = [] b = [] for i in range(n): a.append(list(map(int, input().split()))) for i in range(n): b.append(list(map(int, input().split()))) if m == 1 or n == 1: if a == b: print("YES") else: print("NO") else: a1 = [] a2 = [] b1 = [] b2 = [] for i in range(n): for j in range(m): if (i + j) % 2 == 0: a1.append(a[i][j]) b1.append(b[i][j]) else: a2.append(a[i][j]) b2.append(b[i][j]) a1.sort() b1.sort() a2.sort() b2.sort() if a1 == b1 and a2 == b2: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for i in range(int(input())): [n, m] = [int(i) for i in input().split()] a1 = [] for i in range(n): a1.append([int(i) for i in input().split()]) a2 = [] for i in range(n): a2.append([int(i) for i in input().split()]) if m == 1 or n == 1: if a1 == a2: print("YES") else: print("NO") continue e1 = {} o1 = {} e2 = {} o2 = {} for i in range(n): for j in range(m): if i + j & 1 == 0: if a1[i][j] in e1: e1[a1[i][j]] += 1 else: e1[a1[i][j]] = 1 if a2[i][j] in e2: e2[a2[i][j]] += 1 else: e2[a2[i][j]] = 1 else: if a1[i][j] in o1: o1[a1[i][j]] += 1 else: o1[a1[i][j]] = 1 if a2[i][j] in o2: o2[a2[i][j]] += 1 else: o2[a2[i][j]] = 1 if e1 == e2 and o1 == o2: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) while t: s = input().split() n, m = map(int, s) A = [] for i in range(n): s = input().split() temp = list(map(int, s)) A.append(temp) B = [] for i in range(n): s = input().split() temp = list(map(int, s)) B.append(temp) aA = [] bA = [] aB = [] bB = [] for i in range(n): for j in range(m): if (i + j) % 2 == 0: aA.append(A[i][j]) aB.append(B[i][j]) else: bA.append(A[i][j]) bB.append(B[i][j]) aA.sort() bB.sort() aB.sort() bA.sort() if m == 1 or n == 1: if A == B: ans = "YES" else: ans = "NO" elif aA == aB and bB == bA: ans = "YES" else: ans = "NO" print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) while t > 0: t = t - 1 x, y = map(int, input().split()) a = [] for i in range(0, x): a.append([int(j) for j in input().split()]) b = [] for i in range(0, x): b.append([int(j) for j in input().split()]) oddA = [] evenA = [] evenB = [] oddB = [] if x == 1 or y == 1: if a == b: print("YES") else: print("NO") else: for i in range(x): for j in range(y): if (i + j) % 2 == 0: evenA.append(a[i][j]) else: oddA.append(a[i][j]) for i in range(x): for j in range(y): if (i + j) % 2 == 0: evenB.append(b[i][j]) else: oddB.append(b[i][j]) evenA = sorted(evenA) evenB = sorted(evenB) oddA = sorted(oddA) oddB = sorted(oddB) if evenA == evenB and oddA == oddB: res = "YES" else: res = "NO" print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) for i in range(t): n, m = map(int, input().split()) ma1 = [] ma2 = [] for i in range(n): ma1.append(list(map(int, input().split()))) for i in range(n): ma2.append(list(map(int, input().split()))) p = [] q = [] if n == 1 or m == 1: if ma1 == ma2: print("YES") else: print("NO") else: for i in range(n): for j in range(m): p.append(ma1[i][j]) q.append(ma2[i][j]) p.sort() q.sort() if p != q: print("NO") else: sum1, sum2 = 0, 0 f = 0 for i in range(n): for j in range(m): if f == 0 and j % 2 == 0: sum1 += ma1[i][j] sum2 += ma2[i][j] elif f == 1 and j % 2 == 1: sum1 += ma1[i][j] sum2 += ma2[i][j] if f == 1: f = 0 else: f = 1 if sum1 == sum2: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
test_cases = int(input()) for _ in range(test_cases): rows, columns = list(map(int, input().split())) a = [list(map(int, input().split())) for _ in range(rows)] b = [list(map(int, input().split())) for _ in range(rows)] if rows == 1 or columns == 1: if a == b: print("YES") else: print("NO") else: white_a = list() black_a = list() white_b = list() black_b = list() for i in range(rows): for j in range(columns): if (i + j) % 2 == 0: white_a.append(a[i][j]) white_b.append(b[i][j]) else: black_a.append(a[i][j]) black_b.append(b[i][j]) if set(white_a) == set(white_b) and set(black_a) == set(black_b): print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for _ in range(int(input())): n_m = [int(i) for i in input().split(" ")] a = [] b = [] for i in range(n_m[0]): a.append([int(i) for i in input().split(" ")]) for i in range(n_m[0]): b.append([int(i) for i in input().split(" ")]) list_a_1 = [] list_a_2 = [] list_b_1 = [] list_b_2 = [] for x in range(n_m[0]): for y in range(n_m[1]): if (x + y) % 2 == 0: list_a_1.append(a[x][y]) list_b_1.append(b[x][y]) elif (x + y) % 2 == 1: list_a_2.append(a[x][y]) list_b_2.append(b[x][y]) list_a_1 = sorted(list_a_1) list_b_1 = sorted(list_b_1) list_a_2 = sorted(list_a_2) list_b_2 = sorted(list_b_2) if n_m[0] == 1 or n_m[1] == 1: if a == b: print("YES") else: print("NO") elif list_a_1 == list_b_1 and list_a_2 == list_b_2: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
test_case = int(input()) output = [] def translate_input_small(r): matrix = [] for i in range(r): line = input().split() line = list(map(int, line)) matrix += line return matrix def translate_input_large(r, c): odd_pattern = [] even_pattern = [] for i in range(r): line = input().split() line = list(map(int, line)) if i % 2 == 0: odd_pattern += [line[i] for i in range(c) if i % 2 == 1] even_pattern += [line[i] for i in range(c) if i % 2 == 0] else: odd_pattern += [line[i] for i in range(c) if i % 2 == 0] even_pattern += [line[i] for i in range(c) if i % 2 == 1] return sorted(even_pattern), sorted(odd_pattern) for i in range(test_case): case = input().split() r = int(case[0]) c = int(case[1]) if r != 1 and c != 1: matrix1 = translate_input_large(r, c) matrix1_even = matrix1[0] matrix1_odd = matrix1[1] matrix2 = translate_input_large(r, c) matrix2_even = matrix2[0] matrix2_odd = matrix2[1] if matrix1_even == matrix2_even and matrix1_odd == matrix2_odd: print("YES") else: print("NO") else: matrix1 = translate_input_small(r) matrix2 = translate_input_small(r) matrix2.reverse() if matrix1 == matrix2: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def solve(): n, m = map(int, input().split()) if n == 1 or m == 1: A = [] B = [] for i in range(n): A.append(list(map(int, input().split()))[:m]) for i in range(n): B.append(list(map(int, input().split()))[:m]) for i in range(n): for j in range(m): if A[i][j] != B[i][j]: return "NO" else: A1, A2, B1, B2 = {}, {}, {}, {} for i in range(n): num = list(map(int, input().split(" ")))[:m] for j in range(m): if i + j & 1 == 0: if num[j] not in A2.keys(): A2[num[j]] = 1 else: A2[num[j]] += 1 elif num[j] not in A1.keys(): A1[num[j]] = 1 else: A1[num[j]] += 1 for i in range(n): num = list(map(int, input().split(" ")))[:m] for j in range(m): if i + j & 1 == 0: if num[j] not in B2.keys(): B2[num[j]] = 1 else: B2[num[j]] += 1 elif num[j] not in B1.keys(): B1[num[j]] = 1 else: B1[num[j]] += 1 for key, val in A1.items(): if key in B1.keys(): if val != B1[key]: return "NO" else: return "NO" for key, val in A2.items(): if key in B2.keys(): if val != B2[key]: return "NO" else: return "NO" return "YES" test = int(input()) for i in range(test): print(solve())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN STRING ASSIGN VAR VAR VAR VAR DICT DICT DICT DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR IF VAR VAR VAR RETURN STRING RETURN STRING FOR VAR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR IF VAR VAR VAR RETURN STRING RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def getoe(mat): o = {} e = {} for i in range(len(mat)): for j in range(len(mat[i])): if (i + j) % 2 == 0: if mat[i][j] in e: e[mat[i][j]] += 1 else: e[mat[i][j]] = 1 elif mat[i][j] in o: o[mat[i][j]] += 1 else: o[mat[i][j]] = 1 return [o, e] def checkoe(A, B): for v in A: if not v in B: return False if A[v] != B[v]: return False return True def checkOneOne(A, B): for i in range(n): for j in range(m): if A[i][j] != B[i][j]: return False return True for _ in range(int(input())): [n, m] = list(map(int, input().split())) A = [] B = [] for i in range(n): A.append(list(map(int, input().split()))) for i in range(n): B.append(list(map(int, input().split()))) if n == 1 or m == 1: ans = checkOneOne(A, B) else: [Ao, Ae] = getoe(A) [Bo, Be] = getoe(B) ans = checkoe(Ao, Bo) and checkoe(Ae, Be) if ans: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN LIST VAR VAR FUNC_DEF FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for _ in range(int(input())): n, m = map(int, input().split()) m1 = [] m2 = [] for i in range(n): m1.append(list(map(int, input().split()))) for i in range(n): m2.append(list(map(int, input().split()))) if m == 1 or n == 1: if m1 == m2: print("YES") else: print("NO") else: s1, s2, f = 0, 0, 0 for i in range(n): for j in range(m): if f == 0 and j % 2 == 0: s1 += m1[i][j] s2 += m2[i][j] elif f == 1 and j % 2 == 1: s1 += m1[i][j] s2 += m2[i][j] if f == 0: f = 1 else: f = 0 if s1 == s2: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for i in range(int(input())): [row, col] = [int(ele) for ele in input().split()] if row == 1 or col == 1: mat1 = [[int(ele) for ele in input().split()] for i in range(row)] mat2 = [[int(ele) for ele in input().split()] for i in range(row)] if mat1 == mat2: print("YES") else: print("NO") else: e1 = {} e2 = {} o1 = {} o2 = {} for i in range(row): line = [int(ele) for ele in input().split()] for j in range(col): if (i + j) % 2 == 0: e1[line[j]] = e1.get(line[j], 0) + 1 else: o1[line[j]] = o1.get(line[j], 0) + 1 for i in range(row): line = [int(ele) for ele in input().split()] for j in range(col): if (i + j) % 2 == 0: e2[line[j]] = e2.get(line[j], 0) + 1 else: o2[line[j]] = o2.get(line[j], 0) + 1 if e1 == e2 and o1 == o2: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
try: for _ in range(int(input())): def f(n, m): lst1 = [] lst2 = [] lst3 = [] lst4 = [] lst5 = [] lst6 = [] for i in range(n): lst1.append(list(map(int, input().split()))) for i in range(n): lst2.append(list(map(int, input().split()))) if n == 1: if m > 1: if lst1 == lst2: return "YES" else: return "NO" if m == 1: if n > 1: for i in range(n): for j in range(m): if lst1[i][j] != lst2[i][j]: return "NO" return "YES" for i in range(0, n, 2): for j in range(0, m, 2): lst3.append(lst1[i][j]) lst5.append(lst2[i][j]) for i in range(1, n, 2): for j in range(1, m, 2): lst3.append(lst1[i][j]) lst5.append(lst2[i][j]) for i in range(0, n, 2): for j in range(1, m, 2): lst4.append(lst1[i][j]) lst6.append(lst2[i][j]) for i in range(1, n, 2): for j in range(0, m, 2): lst4.append(lst1[i][j]) lst6.append(lst2[i][j]) if sorted(lst3) == sorted(lst5) and sorted(lst4) == sorted(lst6): return "YES" else: return "NO" n, m = map(int, input().split()) print(f(n, m)) except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN STRING RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
T = int(input("")) for i in range(T): str_in = input("") l1 = [int(c) for c in str_in.split(" ")] if l1[0] == 1: str_in_1 = input("") str_in_2 = input("") if str_in_1 == str_in_2: print("YES") else: print("NO") elif l1[1] == 1: first = [] second = [] for j in range(l1[0]): l = int(input("")) first.append(l) for j in range(l1[0]): l = int(input("")) second.append(l) if first == second: print("YES") else: print("NO") else: evens_1 = [] odds_1 = [] evens_2 = [] odds_2 = [] for j in range(l1[0]): str_in = input("") l2 = [int(c) for c in str_in.split(" ")] for k in range(len(l2)): if j % 2 == 0: if k % 2 == 0: odds_1.append(l2[k]) else: evens_1.append(l2[k]) elif k % 2 == 1: odds_1.append(l2[k]) else: evens_1.append(l2[k]) for x in range(l1[0]): str_in = input("") l2 = [int(c) for c in str_in.split(" ")] for y in range(len(l2)): if x % 2 == 0: if y % 2 == 0: odds_2.append(l2[y]) else: evens_2.append(l2[y]) elif y % 2 == 1: odds_2.append(l2[y]) else: evens_2.append(l2[y]) odds_1.sort() odds_2.sort() evens_1.sort() evens_2.sort() if odds_1 == odds_2 and evens_2 == evens_1: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for _ in range(int(input())): m, n = map(int, input().split()) matrix1 = [] matrix2 = [] for i in range(m): matrix1.append(list(map(int, input().split()))) for i in range(m): matrix2.append(list(map(int, input().split()))) eve1 = [] odd1 = [] eve2 = [] odd2 = [] if m == 1 or n == 1: if matrix1 == matrix2: print("YES") else: print("NO") continue for i in range(m): for j in range(n): if i % 2 == 0 and j % 2 == 0 or i % 2 == 1 and j % 2 == 1: eve1.append(matrix1[i][j]) eve2.append(matrix2[i][j]) else: odd1.append(matrix1[i][j]) odd2.append(matrix2[i][j]) if sorted(eve1) == sorted(eve2) and sorted(odd1) == sorted(odd2): print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def flip_mat(): n, m = map(int, input().split()) flag = True if n == 1 or m == 1: arr = [] brr = [] for i in range(n): temp = list(map(int, input().split())) arr.append(temp) for i in range(n): temp = list(map(int, input().split())) brr.append(temp) if arr != brr: flag = False else: d1 = {} d2 = {} d3 = {} d4 = {} for i in range(n): temp = list(map(int, input().split())) for k in range(m): element = temp.pop(0) if (i + k) % 2 != 0: if element not in d1: d1[element] = 1 else: d1[element] += 1 elif element not in d2: d2[element] = 1 else: d2[element] += 1 for i in range(n): temp = list(map(int, input().split())) for k in range(m): element = temp.pop(0) if (i + k) % 2 != 0: if element not in d3: d3[element] = 1 else: d3[element] += 1 elif element not in d4: d4[element] = 1 else: d4[element] += 1 if d1 != d3 or d2 != d4: flag = False if flag: print("YES") else: print("NO") for _ in range(int(input())): flip_mat()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for i in range(int(input())): n, m = map(int, input().split()) a = [] b = [] c = [] d = [] if n == 1 or m == 1: arr = [list(map(int, input().split())) for i in range(n)] for i in range(n): for j in range(m): a.append(arr[i][j]) brr = [list(map(int, input().split())) for i in range(n)] for i in range(n): for j in range(m): b.append(brr[i][j]) if a == b: print("YES") else: print("NO") else: arr = [list(map(int, input().split())) for i in range(n)] for i in range(n): for j in range(m): if (i ^ j) % 2: a.append(arr[i][j]) else: b.append(arr[i][j]) brr = [list(map(int, input().split())) for i in range(n)] for i in range(n): for j in range(m): if (i ^ j) % 2: c.append(brr[i][j]) else: d.append(brr[i][j]) a.sort() b.sort() c.sort() d.sort() if a == c and b == d: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
c = 0 t = int(input()) for _ in range(t): a = [] b = [] n, m = map(int, input().split()) for i in range(n): p = [] p = list(map(int, input().split())) a.append(p) for i in range(n): q = [] q = list(map(int, input().split())) b.append(q) c = 0 if n == 1 or m == 1: for i in range(n): for j in range(m): if a[i][j] == b[i][j]: c += 1 else: break if c == n * m: print("YES") else: print("NO") else: l1 = [] l2 = [] l3 = [] l4 = [] for i in range(n): for j in range(m): if (i + j) % 2 == 0: l1.append(a[i][j]) l2.append(b[i][j]) else: l3.append(a[i][j]) l4.append(b[i][j]) if sorted(l1) == sorted(l2) and sorted(l3) == sorted(l4): print("YES") else: print("NO")
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for _ in range(int(input())): try: a, b, c, d = [], [], [], [] n, m = list(map(int, input().split())) if n == 1 or m == 1: for i in range(n): temp = list(map(int, input().split())) for j in range(m): a.append(temp[j]) for i in range(n): temp = list(map(int, input().split())) for j in range(m): b.append(temp[j]) if a == b: print("YES") else: print("NO") continue for i in range(n): temp = list(map(int, input().split())) for j in range(m): if (i ^ j) % 2: a.append(temp[j]) else: b.append(temp[j]) for i in range(n): temp = list(map(int, input().split())) for j in range(m): if (i ^ j) % 2: c.append(temp[j]) else: d.append(temp[j]) a.sort() b.sort() c.sort() d.sort() if a == c and b == d: print("YES") else: print("NO") except: continue
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST LIST LIST LIST ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) while t >= 1: n, m = map(int, input().split()) a = [list(map(int, input().split())) for i in range(n)] b = [list(map(int, input().split())) for i in range(n)] A = [] B = [] for i in a: A += i for i in b: B += i A.sort() B.sort() if A == B and n != 1 and m != 1: print("YES") else: print("NO") t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) def makeArr(grid, n, m): odd = [] even = [] for i in range(0, n): for j in range(0, m): if (i + j) % 2 == 1: odd.append(grid[i][j]) else: even.append(grid[i][j]) return [sorted(odd), sorted(even)] while t != 0: n, m = map(int, input().split()) grid1 = [] grid2 = [] for i in range(0, n): row = list(map(int, input().split())) grid1.append(row) for i in range(0, n): row = list(map(int, input().split())) grid2.append(row) if m == 1 or n == 1: if grid1 == grid2: print("YES") else: print("NO") else: arr1 = makeArr(grid1, n, m) arr2 = makeArr(grid2, n, m) if arr1[0] == arr2[0] and arr1[1] == arr2[1]: print("YES") else: print("NO") t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) for _ in range(t): n, m = map(int, input().split()) A = [] B = [] A1 = {} A2 = {} B1 = {} B2 = {} for i in range(n): A.append(list(map(int, input().split()))) for j in range(m): if (i + j) % 2 == 0: if A[i][j] in A1.keys(): A1.update({A[i][j]: A1[A[i][j]] + 1}) else: A1[A[i][j]] = 1 elif A[i][j] in A2.keys(): A2.update({A[i][j]: A2[A[i][j]] + 1}) else: A2[A[i][j]] = 1 for i in range(n): B.append(list(map(int, input().split()))) for j in range(m): if (i + j) % 2 == 0: if B[i][j] in B1.keys(): B1.update({B[i][j]: B1[B[i][j]] + 1}) else: B1[B[i][j]] = 1 elif B[i][j] in B2.keys(): B2.update({B[i][j]: B2[B[i][j]] + 1}) else: B2[B[i][j]] = 1 flag = "YES" if n == 1 or m == 1: if A != B: flag = "NO" else: for i in A1.keys(): if i in B1.keys(): if A1[i] != B1[i]: flag = "NO" break else: flag = "NO" break for i in A2.keys(): if i in B2.keys(): if A2[i] != B2[i]: flag = "NO" break else: flag = "NO" break print(flag)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR DICT VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR DICT VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR DICT VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR DICT VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def solve(): N, M = map(int, input().split()) if N == 1 or M == 1: A = [] B = [] for i in range(N): A.append(list(map(int, input().split()))) for i in range(N): B.append(list(map(int, input().split()))) if A == B: print("YES") else: print("NO") return even_A, odd_A, even_B, odd_B = [], [], [], [] A = [] B = [] for i in range(N): A.append(list(map(int, input().split()))) for i in range(N): B.append(list(map(int, input().split()))) for i in range(N): for j in range(M): if (i + j) % 2 == 0: even_A.append(A[i][j]) even_B.append(B[i][j]) else: odd_A.append(A[i][j]) odd_B.append(B[i][j]) even_A.sort() odd_A.sort() even_B.sort() odd_B.sort() if even_A != even_B: print("NO") return if odd_A != odd_B: print("NO") return print("YES") for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR VAR LIST LIST LIST LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) for __ in range(t): n, m = map(int, input().split()) a = [] b = [] aeven = [] aodd = [] beven = [] bodd = [] for __ in range(n): row = list(map(int, input().split())) a.append(row) for __ in range(n): row = list(map(int, input().split())) b.append(row) if n == 1 or m == 1: if a == b: print("YES") else: print("NO") else: for i in range(n): for j in range(m): if (i + j) % 2 == 0: aeven.append(a[i][j]) beven.append(b[i][j]) else: aodd.append(a[i][j]) bodd.append(b[i][j]) aeven.sort() beven.sort() aodd.sort() bodd.sort() if aeven == beven and aodd == bodd: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for _ in range(int(input())): N, M = map(int, input().split()) if min(N, M) > 1: A = {} for i in range(N): tmp = list(map(int, input().split())) for j in range(M): A[tmp[j]] = A.get(tmp[j], 0) + (i + j) % 2 B = {} for i in range(N): tmp = list(map(int, input().split())) for j in range(M): B[tmp[j]] = B.get(tmp[j], 0) + (i + j) % 2 print("YES" if A == B else "NO") else: A = [] for i in range(N): A += list(map(int, input().split())) B = [] for i in range(N): B += list(map(int, input().split())) print("YES" if A == B else "NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for _ in range(int(input())): n, m = map(int, input().split()) arr1 = [] arr2 = [] arr3 = [] arr4 = [] for i in range(n): ele = list(map(int, input().split())) arr1.append(ele) arr2.extend(ele) for j in range(n): ele = list(map(int, input().split())) arr3.append(ele) arr4.extend(ele) arr2.sort() arr4.sort() if n == 1 or m == 1: print("NO") elif arr2 == arr4: if n != m: print("YES") else: arr_dia1 = [] arr_non_dia1 = [] arr_dia2 = [] arr_non_dia2 = [] for i in range(n): arr_dia1.append(arr1[i][i]) arr_non_dia1.append(arr1[i][-i - 1]) arr_dia2.append(arr3[i][i]) arr_non_dia2.append(arr3[i][-i - 1]) arr_dia1.sort() arr_non_dia1.sort() arr_dia2.sort() arr_non_dia2.sort() if arr_dia1 == arr_dia2 and arr_non_dia1 == arr_non_dia2: print("YES") elif n % 2 == 0: print("YES") else: print("NO") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def solve(): n, m = list(map(int, input().split())) arr1, arr2 = [], [] for i in range(n): row = list(map(int, input().split())) arr1.append(row) for i in range(n): row = list(map(int, input().split())) arr2.append(row) if n == 1 or m == 1: for i in range(n): for j in range(m): if arr1[i][j] != arr2[i][j]: return "NO" a1, b1, a2, b2 = {}, {}, {}, {} for i in range(n): for j in range(m): if (i + j) % 2: if arr1[i][j] not in a1: a1[arr1[i][j]] = 0 a1[arr1[i][j]] += 1 if arr2[i][j] not in b1: b1[arr2[i][j]] = 0 b1[arr2[i][j]] += 1 else: if arr1[i][j] not in a2: a2[arr1[i][j]] = 0 a2[arr1[i][j]] += 1 if arr2[i][j] not in b2: b2[arr2[i][j]] = 0 b2[arr2[i][j]] += 1 for i in a1: if i not in b1 or a1[i] != b1[i]: return "NO" for i in a2: if i not in b2 or a2[i] != b2[i]: return "NO" return "YES" for _ in range(int(input())): print(solve())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN STRING ASSIGN VAR VAR VAR VAR DICT DICT DICT DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN STRING FOR VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def res(n, m): if n == 1 or m == 1: a = [0] * n b = [0] * n for i in range(n): if m == 1: a[i] = [int(input())] else: a[i] = list(map(int, input().split())) for i in range(n): if m == 1: b[i] = [int(input())] else: b[i] = list(map(int, input().split())) for i in range(n): for j in range(m): if a[i][j] != b[i][j]: return "NO" else: d1 = {} d2 = {} d3 = {} d4 = {} for i in range(n): num2 = list(map(int, input().split())) for j in range(m): num = num2[j] if (i + j) % 2 == 0: if num in d1: d1[num] += 1 else: d1[num] = 1 elif num in d2: d2[num] += 1 else: d2[num] = 1 for i in range(n): num2 = list(map(int, input().split())) for j in range(m): num = num2[j] if (i + j) % 2 == 0: if num in d3: d3[num] += 1 else: d3[num] = 1 elif num in d4: d4[num] += 1 else: d4[num] = 1 for it in list(d1.keys()): if it not in d3: return "NO" for it in list(d2.keys()): if it not in d4: return "NO" return "YES" T = int(input()) for i in range(T): n, m = map(int, input().split()) print(res(n, m))
FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN STRING ASSIGN VAR DICT ASSIGN VAR DICT 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def func(A, B, n, m): if m == 1 or n == 1: if A == B: return "YES" else: return "NO" ab = 0 bb = 0 for i in range(n): if i % 2 == 0: j = 0 while j < m: ab += A[i][j] bb += B[i][j] j += 2 else: j = 1 while j < m: ab += A[i][j] bb += B[i][j] j += 2 if bb == ab: return "YES" return "NO" T = int(input()) for t in range(T): N, M = list(map(int, input().split())) A = [] B = [] for i in range(N): A.append(list(map(int, input().split()))) for j in range(N): B.append(list(map(int, input().split()))) res = func(A, B, N, M) print(res)
FUNC_DEF IF VAR NUMBER VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def check(n, m): l1, l2 = [], [] for i in range(n): a = list(map(int, input().split())) for j in range(m): if (i + j) % 2 == 0: l1.append(a[j]) else: l2.append(a[j]) l1.sort() l2.sort() return l1, l2 for _ in range(int(input())): n, m = map(int, input().split()) if n == 1 or m == 1: l1, l2 = [], [] for i in range(n): a = list(map(int, input().split())) for j in range(m): l1.append(a[j]) for i in range(n): a = list(map(int, input().split())) for j in range(m): l2.append(a[j]) if l1 == l2: print("YES") else: print("NO") else: lst1, lst2 = check(n, m) lst3, lst4 = check(n, m) if lst1 == lst3 and lst2 == lst4: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def solve(arra, arrb, n, m): if n == 1 or m == 1: for i in range(n): for j in range(m): if arra[i][j] != arrb[i][j]: return False else: insta = {} for i in range(n): for j in range(m): if arra[i][j] not in insta: insta[arra[i][j]] = 1 else: insta[arra[i][j]] += 1 instb = {} for i in range(n): for j in range(m): if arrb[i][j] not in instb: instb[arrb[i][j]] = 1 else: instb[arrb[i][j]] += 1 for key in insta: if key not in instb: return False elif insta[key] != instb[key]: return False for key in instb: if key not in insta: return False elif instb[key] != insta[key]: return False return True case = int(input()) for _ in range(case): n, m = map(int, input().split(" ")) arra = [] for i in range(n): arra.append(list(map(int, input().split(" ")))) arrb = [] for i in range(n): arrb.append(list(map(int, input().split(" ")))) ans = solve(arra, arrb, n, m) if ans: print("YES") else: print("NO")
FUNC_DEF IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
for _ in range(int(input())): n, m = [int(x) for x in input().split()] a = [] b = [] if m == 1 or n == 1: a_int = [] b_int = [] for i in range(n): a_int = [int(x) for x in input().split()] a.append(a_int) for i in range(n): b_int = [int(x) for x in input().split()] b.append(b_int) if a == b: print("YES") else: print("NO") else: a_even = [] b_even = [] a_odd = [] b_odd = [] a_int = [] b_int = [] for i in range(n): a_int = [int(x) for x in input().split()] for j in range(len(a_int)): if (i + j) % 2 == 0: a_even.append(a_int[j]) else: a_odd.append(a_int[j]) for i in range(n): b_int = [int(x) for x in input().split()] for j in range(len(b_int)): if (i + j) % 2 == 0: b_even.append(b_int[j]) else: b_odd.append(b_int[j]) a_even.sort() b_even.sort() a_odd.sort() b_odd.sort() if a_even == b_even and a_odd == b_odd: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def is_arr_equal(arr1, arr2): if len(arr1) != len(arr2): return False count = {} for i in arr1: if i in count: count[i] += 1 else: count[i] = 1 for i in arr2: if i not in count or count[i] == 0: return False else: count[i] -= 1 return True t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [] b = [] for i in range(n): arr = list(map(int, input().split())) a.append(arr) for i in range(n): arr = list(map(int, input().split())) b.append(arr) fl = 0 evenarr = [] oddarr = [] evenarr2 = [] oddarr2 = [] if n == 1 or m == 1: fl = 0 for i in range(n): for j in range(m): if a[i][j] != b[i][j]: fl = 1 break if fl == 1: break if fl == 1: print("NO") else: print("YES") continue for i in range(n): for j in range(m): if (i + j) % 2 == 0: evenarr.append(a[i][j]) evenarr2.append(b[i][j]) else: oddarr.append(a[i][j]) oddarr2.append(b[i][j]) if is_arr_equal(evenarr, evenarr2) and is_arr_equal(oddarr, oddarr2): print("YES") else: print("NO")
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) for _ in range(t): x, y = input().split() x = int(x) y = int(y) c1 = dict() c2 = dict() d1 = dict() d2 = dict() f = 0 if x == 1 or y == 1: a = [] b = [] for i in range(x): k = list(map(int, input().split())) a.append(k) for i in range(x): k = list(map(int, input().split())) b.append(k) for i in range(x): for j in range(y): if a[i][j] != b[i][j]: f = 1 break else: for i in range(x): a = list(map(int, input().split())) for j in range(y): if (i + j) % 2 != 0: if a[j] in c1: c1[a[j]] += 1 else: c1[a[j]] = 1 elif a[j] in c2: c2[a[j]] += 1 else: c2[a[j]] = 1 for i in range(x): a = list(map(int, input().split())) for j in range(y): if (i + j) % 2 != 0: if a[j] in d1: d1[a[j]] += 1 else: d1[a[j]] = 1 elif a[j] in d2: d2[a[j]] += 1 else: d2[a[j]] = 1 for k in c1: if k in d1: if c1[k] != d1[k]: f = 1 break else: f = 1 break for k in c2: if k in d2: if c2[k] != d2[k]: f = 1 break else: f = 1 break if f == 0: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) for _ in range(t): n, m = [int(x) for x in input().split()] A = [0] * n B = [0] * n for i in range(n): b = [int(x) for x in input().split()] A[i] = b for i in range(n): b = [int(x) for x in input().split()] B[i] = b flag = True if n == 1 or m == 1: for i in range(n): for j in range(m): if A[i][j] != B[i][j]: flag = False if flag == True: print("YES") else: print("NO") continue d1 = dict() d2 = dict() for i in range(n): for j in range(m): v1 = A[i][j] v2 = B[i][j] if d1.get(v1, -1) == -1: d1[v1] = [(i, j)] else: d1[v1].append((i, j)) if d2.get(v2, -1) == -1: d2[v2] = [(i, j)] else: d2[v2].append((i, j)) for val in d1: if flag == False: break inds1 = d1.get(val) l1 = len(inds1) inds2 = d2.get(val, -1) if inds2 == -1: flag = False break l2 = len(inds2) if l1 != l2: flag = False break samecount1 = 0 samecount2 = 0 for i in range(l1): ind1 = inds1[i] same = False if ind1[0] % 2 == 0: if ind1[1] % 2 == 0: same = True elif ind1[1] % 2 == 1: same = True if same == True: samecount1 += 1 for i in range(l2): ind2 = inds2[i] same = False if ind2[0] % 2 == 0: if ind2[1] % 2 == 0: same = True elif ind2[1] % 2 == 1: same = True if same == True: samecount2 += 1 if samecount1 != samecount2: flag = False break if flag == True: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) for q in range(t): a = [] b = [] c = [] o1 = [] e1 = [] o2 = [] e2 = [] n, m = [int(i) for i in input().split()] for k in range(0, n): b = [int(i) for i in input().split()] a.append(b) for k in range(0, n): b = [int(i) for i in input().split()] c.append(b) for i in range(0, n): for j in range(0, m): if (i + j) % 2 == 0: e1.append(a[i][j]) e2.append(c[i][j]) else: o1.append(a[i][j]) o2.append(c[i][j]) e1.sort() o1.sort() e2.sort() o2.sort() if e2 == e1 and o2 == o1 and n != 1 and m != 1: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
n = int(input()) while n > 0: n = n - 1 A = input() c = [] d = [] e = [] f = [] g = [] h = [] z = 0 y = 0 A = A.split(" ") A = list(map(lambda x: int(x), A)) a = A[0] b = A[1] count = 0 found = 0 for i in range(0, a): x = input() x = x.split() x = list(map(lambda x: int(x), x)) c.append(x) for i in range(0, a): x = input() x = x.split() x = list(map(lambda x: int(x), x)) d.append(x) if a == 1 or b == 1: if c == d: print("YES") else: print("NO") continue for i in range(0, a): for j in range(0, b, 2): if i % 2 == 0: e.append(c[i][j]) g.append(d[i][j]) if j + 1 < b: f.append(c[i][j + 1]) h.append(d[i][j + 1]) else: if j + 1 < b: e.append(c[i][j + 1]) g.append(d[i][j + 1]) f.append(c[i][j]) h.append(d[i][j]) e.sort() f.sort() g.sort() h.sort() for i in range(0, len(e)): if e[i] == g[i]: count = count + 1 for i in range(0, len(f)): if f[i] == h[i]: found = found + 1 if count == len(e) and found == len(f): print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
T = int(input()) for t in range(T): n, m = tuple(map(int, input().split())) a1, a2, b1, b2 = [], [], [], [] for i in range(n): arr = tuple(map(int, input().split())) for j in range(m): if (i + j) % 2 == 0: a1.append(arr[j]) else: a2.append(arr[j]) for i in range(n): arr = tuple(map(int, input().split())) for j in range(m): if (i + j) % 2 == 0: b1.append(arr[j]) else: b2.append(arr[j]) if m == 1 or n == 1: if a1 == b1 and a2 == b2: print("YES") else: print("NO") continue a1.sort() a2.sort() b1.sort() b2.sort() if a1 == b1 and a2 == b2: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) while t > 0: t -= 1 n, m = map(int, input().split()) if n == 1 or m == 1: b = True a1 = [] a2 = [] for i in range(n): a1.append(list(map(int, input().split()))) for i in range(n): a2.append(list(map(int, input().split()))) for i in range(n): for j in range(m): if a1[i][j] != a2[i][j]: b = False if b: print("YES") else: print("NO") continue else: num = 0 e1 = {} o1 = {} o2 = {} e2 = {} a1 = [] a2 = [] for i in range(n): a1.append(list(map(int, input().split()))) for i in range(n): a2.append(list(map(int, input().split()))) for i in range(n): for j in range(m): num = a1[i][j] if i + j & 1 == 0: e1[num] = e1.get(num, 0) + 1 else: o1[num] = o1.get(num, 0) + 1 for i in range(n): for j in range(m): num = a2[i][j] if i + j & 1 == 0: e2[num] = e2.get(num, 0) + 1 else: o2[num] = o2.get(num, 0) + 1 same = True for i, j in e1.items(): if j != e2.get(i, 0): same = False break for i, j in o1.items(): if j != o2.get(i, 0): same = False break if same: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
def angledflip(): n, m = map(int, input().split()) a, b, c, d = [], [], [], [] if n == 1 or m == 1: for i in range(n): t1 = list(map(int, input().split())) a.append(t1) for i in range(n): t1 = list(map(int, input().split())) b.append(t1) if a == b: return "YES" else: return "NO" for i in range(n): t1 = list(map(int, input().split())) for j in range(len(t1)): if (i ^ j) % 2: a.append(t1[j]) else: b.append(t1[j]) for i in range(n): t2 = list(map(int, input().split())) for j in range(len(t2)): if (i ^ j) % 2: c.append(t2[j]) else: d.append(t2[j]) a.sort() b.sort() c.sort() d.sort() if a == c and b == d: return "YES" else: return "NO" t = int(input()) for _ in range(t): print(angledflip())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST LIST LIST LIST IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero): Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal]. For example, suppose you choose the submatrix \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} . It can be converted into either \begin{bmatrix} 1 \;\;\;\; 4 \;\;\;\; 7 \\ 2 \;\;\;\; 5 \;\;\;\; 8 \\ 3 \;\;\;\; 6 \;\;\;\; 9 \end{bmatrix} by flipping about the main diagonal, or \begin{bmatrix} 9 \;\;\;\; 6 \;\;\;\; 3 \\ 8 \;\;\;\; 5 \;\;\;\; 2 \\ 7 \;\;\;\; 4 \;\;\;\; 1 \end{bmatrix} by flipping about the antidiagonal. Is it possible to convert A to B by performing this operation several (possibly, zero) times? Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns. For example, if A = \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \\ 7 \;\;\;\; 8 \;\;\;\; 9 \end{bmatrix} then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*. A square submatrix is a submatrix with the same number of rows and columns. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M β€” the number of rows and columns of the matrices, respectively. - The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}. - The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}. ------ Output Format ------ For each test case, print YES if its possible to convert A to B, else print NO. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N,M ≀ 3 \cdot 10^{5}$ $1 ≀ A_{i, j},B_{i, j} ≀ 10^{9}$ - The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 3 1 2 3 4 5 6 1 4 3 6 5 2 3 3 12 11 8 7 1 1 9 2 4 4 1 8 2 1 11 9 7 12 ----- Sample Output 1 ------ YES YES ----- explanation 1 ------ Test case $1$: $A$ can be converted to $B$ as follows: $$ \begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix} $$
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a, b = [], [] for i in range(n): a.append(list(map(int, input().split()))) for i in range(n): b.append(list(map(int, input().split()))) if n == 1 or m == 1: if a != b: print("NO") continue al1, al2, bl1, bl2 = [], [], [], [] io, jo = True, True for i in range(n): jo = io for j in range(m): if jo: al1.append(a[i][j]) bl1.append(b[i][j]) jo = False else: al2.append(a[i][j]) bl2.append(b[i][j]) jo = True if io: io = False else: io = True al1.sort() al2.sort() bl1.sort() bl2.sort() ans = "YES" if al1 != bl1 or al2 != bl2: ans = "NO" print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR LIST LIST LIST LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING IF VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
n = int(input()) for i in range(n): s = input() l = list(s) t = set(l) d = {} l1 = [] for i in s: if i in d: d[i] += 1 else: d[i] = 1 for i in d.values(): l1.append(i) l1.sort() N = len(l1) if N >= 4 and l1[3] != l1[1] + l1[2]: l1[0], l1[1] = l1[1], l1[0] ok = True if N >= 3: for i in range(2, N): if l1[i] != l1[i - 1] + l1[i - 2]: ok = False if ok: print("Dynamic") else: print("Not")
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
for _ in range(int(input())): d = {} s = input().strip() for i in s: try: d[i] += 1 except: d[i] = 1 d = list(d.values()) d.sort() if len(d) < 3: print("Dynamic") else: for i in range(2, len(d)): flag = 0 if d[i - 1] + d[i - 2] == d[i]: continue else: d[0], d[1] = d[1], d[0] for i in range(2, len(d)): if d[i - 1] + d[i - 2] == d[i]: continue else: print("Not") flag = 1 break if flag == 1: break else: print("Dynamic")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
def fibFound(x): found = True if x[-1] != x[-2] + x[-3]: found = False return found def dyn(s): res = False u = [s.count(x) for x in set(s)] if len(u) < 3 or fibFound(sorted(u)): return True else: return False try: t = int(input()) for i in range(0, t): s = input() if dyn(s): print("Dynamic") else: print("Not") except EOFError as e: pass
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
def dynamic_check(t_case): func_tcase = [] t_case = t_case.replace(" ", "") list_set = list(set(t_case)) for item in list_set: func_tcase.append(t_case.count(item)) print(func(func_tcase)) def func(func_tcase): if len(func_tcase) < 3: return "Dynamic" else: func_tcase.sort() if func_tcase[-1] == func_tcase[-2] + func_tcase[-3]: return "Dynamic" return "Not" n_cases = int(input()) for _ in range(n_cases): t_case = input() dynamic_check(t_case)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
for i in range(int(input())): s = input() ss = set(s) if len(ss) < 3: print("Dynamic") continue l = [] for i in ss: l.append((i, s.count(i))) l = sorted(l, key=lambda x: x[1], reverse=True) f = False if l[0][1] == l[1][1] + l[2][1]: f = True if f: print("Dynamic") else: print("Not")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
T = int(input()) for tj in range(T): s = input() N = len(s) count = [0] * 26 for i in range(N): count[ord(s[i]) - ord("a")] += 1 convert_table = [] for i in range(26): if count[i] != 0: convert_table.append(count[i]) if len(convert_table) < 3: print("Dynamic") else: convert_table.sort() flag = True for i in range(2, len(convert_table)): if i == 3: if ( convert_table[i] != convert_table[i - 1] + convert_table[i - 2] and convert_table[i] != convert_table[i - 1] + convert_table[i - 3] ): flag = False break elif convert_table[i] != convert_table[i - 1] + convert_table[i - 2]: flag = False break if flag: print("Dynamic") else: print("Not")
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 ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
n = int(input()) for i in range(n): s = input() res = [] for i in set(s): res.append(s.count(i)) res.sort() if len(res) < 3 or res[-1] == res[-2] + res[-3]: print("Dynamic") else: print("Not")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
t = int(input()) for _ in range(t): s = input() s_dict = dict() for i in range(len(s)): s_dict[s[i]] = s_dict.get(s[i], 0) + 1 values = list(s_dict.values()) values.sort() if len(values) >= 3: ans = "Dynamic" for i in range(2, len(values)): if i == 3 and ( values[0] + values[2] == values[3] or values[1] + values[2] == values[3] ): continue if values[i] != values[i - 1] + values[i - 2]: ans = "Not" break print(ans) else: print("Dynamic")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
T = int(input()) for _ in range(T): s = list(input()) d = {} for i in s: if i in d: d[i] += 1 else: d[i] = 1 M = [] for i in d: M.append(d[i]) M.sort() if len(M) < 3: print("Dynamic") else: found = True for i in range(2, len(M)): if M[i] == M[i - 1] + M[i - 2]: pass elif i == 3: if M[i] == M[2] + M[0]: pass else: found = False break else: found = False break if found: print("Dynamic") else: print("Not")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
for _ in range(int(input())): a = input() b = [a.count(i) for i in set(a)] b.sort() if len(b) < 3: print("Dynamic") continue if b[-3] + b[-2] != b[-1]: print("Not") continue else: print("Dynamic")
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 VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING