for _ in range(int(input())):
n, m, k = map(int, input().split())
b = list(map(int, input().split()))
c = list(map(int, input().split()))
res = 0
for bi in b:
for ci in c:
if bi + ci <= k:
res += 1
print(res)
I first explored mathematical approaches, but as I looked at the constraint, I noticed I could just use brute force.
for _ in range(int(input())):
x1, x2, x3 = map(int, input().split())
mi = 30
for i in range(1, 11):
mi = min(mi, abs(x1 - i) + abs(x2 - i) + abs(x3 - i))
print(mi)
However, there is a clever way as Codeforces Round 954 (Div. 3) Editorial points out
Let x1 <= x2 <= x3. Notice that the answer is at least x3 - x1, because |x3 - a| + |x1 - a| >= |x3 - x1| for any numbers a, x1, x3.
The answer is equal to x3 - x1, since we can choose a = x2.
for _ in range(int(input())):
x1, x2, x3 = map(int, input().split())
ma = max(x1, max(x2, x3))
mi = min(x1, min(x2, x3))
print(ma - mi)
It sounds like a common technique, and I better keep that in mind.
Codeforces Round 930 Editorial
couldn't understand it today. will try to digest later.
# i=_: [1, 2, 3, 4, 5, 6, 7, 8]
# i=2: [2, 1, 3, 4, 5, 6, 7, 8]
# i=3: [3, 1, 2, 4, 5, 6, 7, 8]
# i=4: [3, 4, 2, 1, 5, 6, 7, 8]
# i=5: [5, 4, 2, 1, 3, 6, 7, 8]
# i=6: [5, 4, 6, 1, 3, 2, 7, 8]
# i=7: [7, 4, 6, 1, 3, 2, 5, 8]
# observation: largest power of 2 up to n
for _ in range(int(input())):
n = int(input())
res = 1
while True:
if res * 2 > n:
print(res)
break
res *= 2
Protein shake 800 Chicken wings 1000
Total 1800 kcal
TODO: