PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: notsoloud
Tester: mexomerf
Editorialist: iceknight1093
DIFFICULTY:
TBD
PREREQUISITES:
None
PROBLEM:
Alice, Bob, and Cameron have scores of A, B, C respectively.
After M more matches, each of which exactly one person can get a point in, is it possible for some two of them to have the same score?
EXPLANATION:
Let’s see if Alice and Bob can be made to have the same score.
Let d_{AB} = |A-B| be the initial difference between their scores.
In each match, d_{AB} can decrease by at most 1. We want it to become 0.
So,
- If d_{AB} \gt M, it’s impossible to make it 0, so Alice and Bob can’t have equal scores.
- Otherwise, we can use d_{AB} matches to make Alice and Bob have equal scores (make the one with a lower score win each one).
All the remaining M - d_{AB} matches can be played between say Alice and Cameron, and Cameron can win so as to not change the scores of Alice and Bob.
So, quite simply, if d_{AB} \leq M then Alice and Bob can have equal scores after all M matches, otherwise they cannot.
The same logic applies to the other pairs: if d_{AC} \leq M or d_{BC} \leq M, those two players can be made to have equal scores, otherwise they cannot.
This makes the answer Yes if \min(d_{AB}, d_{AC}, d_{BC}) \leq M, and No otherwise.
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (Python)
for _ in range(int(input())):
a, b, c, m = map(int, input().split())
mndif = min(abs(a-b), abs(a-c), abs(b-c))
print('Yes' if mndif <= m else 'No')