A. Matrix Rotation
Where should be the minimum and the maximum element of the array in order for the matrix to be beautiful.
If a matrix is beautiful, then its minimum is in the upper left corner, and its maximum is in the lower right corner (and vice versa). If you rotate it, the element from the upper left corner goes to the upper right corner, and the element from the lower right corner goes to the lower left corner — so these elements are still in the opposite corners. No matter how many times we rotate a beautiful matrix, its minimum and maximum elements will be in the opposite corners — and the opposite is true as well; if you have a 2×2 matrix with minimum and maximum elements in opposite corners, it can be rotated in such a way that it becomes beautiful.
So, all we need to check is that the minimum and the maximum elements are in the opposite corners.
def solve():
ROWS, COLS = 2, 2
matrix = []
for i in range(ROWS):
matrix.append(list(map(int,input().split())))
x1, y1 = 0, 0
x2, y2 = 0, 0
for i in range(ROWS):
for j in range(COLS):
if matrix[i][j] < matrix[x1][y1]:
x1, y1 = i, j
if matrix[i][j] > matrix[x2][y2]:
x2, y2 = i, j
dx = abs(x1 - x2)
dy = abs(y1 - y2)
if dx + dy > 1:
print("YES")
else:
print("NO")
def main():
num_tests = int(input())
for test in range(num_tests):
solve()
main()
B. Kefa and First Steps
C. Double Strings
Try brute force.
For each string s, brute force all strings x and y such that s=x+y. There are at most 7 such strings, because s has length at most 8. Then check if both x and y appear in the array using some data structure.
def solve():
num_strings = int(input())
strings = []
for i in range(num_strings):
string = input()
strings.append(string)
indices = set(strings)
ans = ['0' for i in range(num_strings)]
for i in range(num_strings):
for j in range(1, len(strings[i])):
prefix = strings[i][: j]
suffix = strings[i][j :]
if prefix in indices and suffix in indices:
ans[i] = '1'
return ''.join(ans)
def main():
num_tests = int(input())
for test in range(num_tests):
print(solve())
main()
D. Balanced Team
May be sorting might help.
In order to create a team with the maximum number of students, it is important to ensure that all members of the team have similar levels of programming skill. A way to do this is by taking the list of students and sorting them in increasing order according to their programming skills. After this, a sliding window technique can then be employed to find the maximum size team window that satisfies the given constraint; i.e. that there is no more than 5 unit difference in skill level between each pair of members. The resulting window size will indicate how many students can be placed together in a team without breaking their specified constraint.
length = int(input())
nums = list(map(int, input().split()))
nums.sort()
ans = 0
left = 0
for right in range(length):
while (nums[right] - nums[left]) > 5:
left += 1
ans = max(ans, right - left + 1)
print(ans)