PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author:
Tester: sushil2006
Editorialist: iceknight1093
DIFFICULTY:
Simple
PREREQUISITES:
None
PROBLEM:
There are N lottery tickets, all with distinct numbers A_1, A_2, \ldots, A_N.
Chef bought ticket A_1.
The lottery number is chosen to be an integer X in [1, 10^6], and anyone whose ticket is closest to X wins the lottery.
How many lottery numbers result in a win for Chef?
EXPLANATION:
Let’s look at only lottery numbers with values \leq A_1 first.
Note that for such a winning number, the only thing that really matters is the closest A_i to A_1 that’s smaller than it.
Let L \lt A_1 be the closest lottery ticket to A_1 that’s smaller than it.
That is, L is the maximum of all lottery tickets with numbers smaller than A_1.
For any X \leq A_1, if X is closer to A_1 than it is to L (or the distance is tied), Chef will win with this X.
This is because:
- Since X \leq A_1, A_1 is certainly closer to X than any A_i \gt A_1 can be.
- L is the closest value to A_1 that’s smaller than it. This means that for any other A_i \lt A_1, we also have A_i \lt L.
But then X is closer to A_1 than L, so X is certainly closer to A_1 than anything smaller than L too.
So, once L is known, we only need to count the number of X \leq A_1 that are closer to A_1 than L.
This is quite simple: we want A_1 - X \leq X - L to hold, meaning X \geq \frac{A_1 + L}{2}.
Since X must be an integer, we take the ceiling to obtain X \geq \left\lceil \frac{A_1 + L}{2} \right\rceil.
The number of valid X is thus the length of the range [\left\lceil \frac{A_1 + L}{2} \right\rceil, A_1].
Note that there’s one edge case: it’s possible that A_1 is the smallest among all the A_i, i.e., no valid value of L exists.
In this case, every X from 1 to A_1 is valid instead.
The above discussion was for X \leq A_1.
X \gt A_1 is similar, just that we use the nearest value to the right of A_1 instead.
Again, if no other A_i is \gt A_1, every X till 10^6 is valid.
Add up the results of both sides to get the overall answer.
TIME COMPLEXITY:
\mathcal{O}(N) per testcase.
CODE:
Editorialist's code (PyPy3)
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
left, right = 1, 10**6
for y in a[1:]:
if y < a[0]: left = max(left, a[0] - (a[0] - y)//2)
else: right = min(right, a[0] + (y - a[0])//2)
print(right - left + 1)