A. Bigger, Stronger
for a sequence to be strictly increasing, no two elements can be equal. Therefore, if there are any duplicate elements in the array, it is impossible to rearrange the array to make it strictly increasing.
Use a set to keep track of unique elements while iterating through the array. If, at any point during the iteration, an element is found to already exist in the set, it means there is a duplicate, and the answer is "NO." If the iteration completes without finding any duplicates, the answer is "YES."
t = int(input())
for _ in range(t):
nums = list(map(int,input().split()))
seen = set()
possible = True
for i in nums:
if i in seen:
possible = False
seen.add(i)
if possible:
print("YES")
else:
print("NO")
B. Application
Consider using a hashmap to store the count of change requests made by a given username.
We can use a hashmap to store the usernames along with their change counts. It also makes it easier to check if a username exists in the database, and to access the count of changes made for a person.
If a person is not registered we store $$$0$$$ as a value for the username. Afterwards we increment this value if a change request is made again with this username.
n = int(input())
database = {}
for _ in range(n):
username = input()
if username in database:
database[username] += 1
print(username + str(database[username]))
else:
database[username] = 0
print('OK')
C. Under Attack!!!
Let's denote the position of the bishop as $$$(i, j)$$$. The cells that the bishop can attack lie along diagonals that either ascend from left to right or descend from right to left. In the case of the left-to-right diagonal, the sum of the column numbers for all cells $$$(i, j)$$$ on the same diagonal is constant $$$( i + j = c )$$$ for some constant $$$c$$$.
For the right-to-left diagonal, the difference of the coordinates remains constant $$$(i - j = c)$$$ for some constant $$$c$$$.
We can store the values of the left-to-right diagonal sums and right-to-left diagonal sums and later calculate the total sum for each cell based on these precomputed values.
from collections import defaultdict
T = int(input())
for _ in range(T):
n,m = map(int,input().split())
mat = [list(map(int,input().split())) for i in range(n)]
front_diagonal = defaultdict(int)
back_diagonal = defaultdict(int)
for r, row in enumerate(mat):
for c, val in enumerate(row):
front_diagonal[r + c] += val
back_diagonal[r - c] += val
ans = 0
for r in range(n):
for c in range(m):
cur_total = front_diagonal[r + c] + back_diagonal[r - c] - mat[r][c]
ans = max(ans,cur_total)
print(ans)
D. Quadruplet Matrix
Let's rotate the grid by 0∘, 90∘, 180∘, and 270∘, and mark all cells that map to each other under these rotations. For example, for 4×4 and 5×5 grids, the grid must have the following patterns, the same letters denoting equal values:
In general, we can rotate the grid by 0∘, 90∘, 180∘, and 270∘ and see which cells need to have equal values by seeing the positions which each cell maps to.
Now to solve the problem, we consider each equal value (each of the letters a, b, c, ... in the above figures) independently, and consider the minimum number of moves to make them all 0 or all 1. The answer is the total across all values.
The time complexity is O($$$ n^2 $$$). And the space complexity is O($$$ 1 $$$).
Let's consider the upper triangular quarter of the grid. If we are at $$$(i, j)$$$, after rotating it by 0∘, 90∘, 180∘, and 270∘, the bit at this position is replaced by the one at $$$(j, n - i - 1)$$$, $$$(n - i - 1, n - j - 1)$$$, $$$(n - j - 1, i)$$$ respectively. From these four positions we count the numbers of zeros and ones and take the minimum. We sum those minimums for all positions in the upper triangular quarter of the grid.
t = int(input())
for _ in range(t):
n = int(input())
grid = []
for i in range(n):
grid.append(input())
min_ops = 0
for row in range(n//2):
for col in range(row, n - row - 1):
zero_one_count = [0, 0]
zero_one_count[int(grid[row][col])] += 1
zero_one_count[int(grid[col][n - row - 1])] += 1
zero_one_count[int(grid[n - row - 1][n - col - 1])] += 1
zero_one_count[int(grid[n - col - 1][row])] += 1
min_ops += min(zero_one_count)
print(min_ops)
E. Commuting Chronicles
It doesn't matter if we start from S or T. How many cases are there?
Mark all the points where you can go horizontally/vertically without any turn starting from both S and T.
Basically, there are three cases: 1. When there is no turn. 2. When the first turn is vertical. 3. When the first turn is horizontal.
We can mark all the horizontal dots that we can reach with out any turn starting from both S and T. If we get S or T while we are marking, it means case 1 (we can reach with no turn). If not, we will check if there are vertically consecutive dots that connect two marked cells (including S and T). If we found one it means case 2. We can check case 3 by transposing the matrix and doing the same technique. If we can't find any of the three cases it means we can't reach there with at most two turns.
rows, cols = map(int, input().split())
grid = []
for _ in range(rows):
row = list(input())
grid.append(row)
def solve(grid, rows, cols):
for r in range(rows):
for c in range(cols):
if grid[r][c] in "TS":
# mark the left until we get '*', 'T' or 'S', or we reach the end
i = c - 1
while i >= 0 and grid[r][i] == ".":
grid[r][i] = 'M'
i -= 1
if i >= 0 and grid[r][i] in "TS": # Case 1: check if we can reach with no turn
return True
# mark the right until we get '*', 'T' or 'S', or we reach the end
i = c + 1
while i < cols and grid[r][i] == ".":
grid[r][i] = 'M'
i += 1
if i < cols and grid[r][i] in "TS": # Case 1: check if we can reach with no turn
return True
for c in range(cols):
r = 0
while r < rows:
if grid[r][c] in "TSM":
r += 1
while r < rows and grid[r][c] == ".":
r += 1
if r < rows and grid[r][c] in "TSM":
return True
r += 1
return False
# transpose the grid for the second case
transposed_grid = [[""]*rows for _ in range(cols)]
for r in range(rows):
for c in range(cols):
transposed_grid[c][r] = grid[r][c]
# Case 2: Vertical turn
is_found = solve(grid, rows, cols)
if is_found:
print("YES")
exit()
# Case 3: Horizontal turn
if solve(transposed_grid, cols, rows):
print("YES")
else:
print("NO")