A. Mood
Solution
$$$Result = Max(0, Y-X)$$$
Python Code
def read():
return int(input())
def read_list():
return [int(i) for i in input().split()]
def solve():
x, y = read_list()
print(max(0, y-x))
for i in range(read()):
solve()
B. Hungry
Solution
$$$QueryResult = Min(Max(X, \sum_{i = 1}^N A_i), \sum_{i = 1}^M A_i + Y)$$$
we can optimized this by pre-calculating a prefix sums arrays of $$$A$$$.
Python Code
def read():
return int(input())
def read_list():
return [int(i) for i in input().split()]
def solve():
n = read()
a = read_list()
prefix_sum = [0]*(n+1)
for i in range(1, n+1):
prefix_sum[i] = prefix_sum[i-1] + a[i-1]
q = read()
for i in range(q):
x, y, m = read_list()
result = min(max(x, prefix_sum[n]), prefix_sum[m]+y)
print(result)
solve()
C. Ice Coffee
Solution
we can pre-caculate $$$next(x)$$$ for all possible $$$x$$$ using a modified version of the Sieve of Eratosthenes.
Python Code
def read():
return int(input())
def read_list():
return [int(i) for i in input().split()]
m = int(1e7)
nxt = [1] * (m+1)
for i in range(2, m+1):
if nxt[i] == 1:
for j in range(i*i, m+1, i):
nxt[j] = max(nxt[j], j//i)
def solve():
n = read()
a = read_list()
b = read_list()
result = 0
for i in range(n):
x = a[i]
y = b[i]
while x != y:
if x > y:
x = nxt[x]
else:
y = nxt[y]
result += 1
print(result)
for i in range(read()):
solve()
D. Beautiful decrease
Solution
using a mono stack we can find the intervals of all the possible beautiful decreases, then sort them by length.
Python Code
def read():
return int(input())
def read_list():
return [int(i) for i in input().split()]
def solve():
n, q = read_list()
a = read_list()
total = sum(a)
b_decreases = [] # (length, width)
stack = [(0, -1)]
def mono_push(value, index):
if value > stack[-1][0]:
stack.append((value, index))
elif value < stack[-1][0]:
while value < stack[-1][0]:
if len(stack) > 1:
b_decreases.append([index-stack[-2][1]-1, min(stack[-1][0]-value, stack[-1][0]-stack[-2][0])])
stack.pop()
if value != stack[-1][0]:
stack.append((value, index))
for i in range(n):
mono_push(a[i], i)
mono_push(0, n)
b_decreases.sort()
for i in range(q):
k = read()
while len(b_decreases) > 0 and k > 0:
decrease_count = min(k, b_decreases[-1][1])
total -= b_decreases[-1][0] * decrease_count
k -= decrease_count
b_decreases[-1][1] -= decrease_count
if b_decreases[-1][1] == 0:
b_decreases.pop()
print(total)
solve()
E. The Detective Game
Python Code
def read():
return int(input())
def read_list():
return [int(i) for i in input().split()]
def solve():
n = read()
count = [0] * (n+1)
for i in range(n):
for j in read_list()[1:]:
count[j] += 1
result = []
for i in range(1, n+1):
if count[i] > n//2:
result.append(i)
print(len(result))
print(' '.join(map(str, result)))
for i in range(read()):
solve()
F. Distinct
Solution
Python Code
```python
```
G. String Rotation
Python Code
def read():
return int(input())
def read_list():
return [int(i) for i in input().split()]
def solve():
n, x = read_list()
s = input()
t = input()
result = 0
for i in range(n):
j = (i+n-x%n)%n
if t[i] != s[j]:
result += 1
print(result)
solve()
H. Cookies
Python Code
def read():
return int(input())
def read_list():
return [int(i) for i in input().split()]
def solve():
n, m, a = read_list()
print(min(n-m, a))
for i in range(read()):
solve()
J. Hide and Seek
Python Code
def read():
return int(input())
def read_list():
return [int(i) for i in input().split()]
def solve():
n, x = read_list()
h = read_list()
result = [0] * n
for i in range(n):
if h[i] < x:
result[i] = 1
print(' '.join(map(str, result)))
for i in range(read()):
solve()
###
Solution
Python Code
```python
```