It took me some time to solve.
At first glance, I thought of generating all the patterns such as
[1, 2]
->
[+1, +2]
, [-1, +2]
, [+1, -2]
,
[-1, -2]
but it's impossible to calculate all the patterns due to its time complexity.
I noticed that, luckily, the order of 1s and 2s does not matter. I can focus on the counts of 1s and 2s instead.
def possible(a: int, b: int) -> bool:
for apc in range(a + 1):
for bpc in range(b + 1):
if apc - (a - apc) + 2 * bpc - 2 * (b - bpc) == 0:
return True
return False
for _ in range(int(input())):
a, b = map(int, input().split())
if possible(a, b):
print("YES")
else:
print("NO")
Codeforces Round 970 (Div. 3) Editorial
I considered this approach, too, but could not come up with the solution. I should retry this later.
First of all, this task is the same as: “divide an array into two arrays with equal sum”.
So, obviously, we need to check if the sum of all elements is even which implies that the number of ones is even.
!
Then, we can out half of 2s in one array and the other half in another, but if number of 2s is odd, then one array will have a greater sum then another, so we need to put two 1s there.
! distributing 2s (and 1s) evenly!
So, if we don’t have two ones while the number of 2s is odd then the answer is “NO”. Also, if sum is odd, the answer is also “NO”. In all other cases, the answer is “YES”.
Rice 1700 Yogurt 900 Salad 600 Honey 200
Total 3400 kcal :cry:
9k run + 2k walk
but I didn't eat junk food. This should be fine, I hope.
MUST:
TODO: