PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: iceknight1093
Tester: raysh07
Editorialist: iceknight1093
DIFFICULTY:
TBD
PREREQUISITES:
Binary search, sliding window
PROBLEM:
You are given a circular array A.
Every second, each element simultaneously takes on the larger of its neighbors’ values; forgetting its own.
Find the first instant of time at which all elements become equal; or decide that this never happens.
EXPLANATION:
Let M = \max(A) be the largest element in A.
Observe that the value M will always be present in the array, since any neighbor of M will surely take up the value M.
So, if all elements are equal in the end, they must all have value M.
Let us now understand what exactly the value at some index will be, after several seconds have passed.
Define A_i^{(t)} to be the value at index i after t seconds.
Then we have:
- A_i^{(0)} = A_i
- A_{i}^{(t)} = \max\left(A_{i-1}^{(t-1)}, A_{i+1}^{(t-1)}\right)
Of course, array indices are considered cyclically here.
If you work this out for a few small values of t, it can be verified that the following holds:
A_i^{(t)} equals the maximum value among A_{i-t}, A_{i-t+2}, A_{i-t+4}, \ldots, A_{i+t-2}, A_{i+t}
That is, after t seconds, the value at index i will equal the maximum value that can be reached by taking a walk of exactly t steps starting at i, with it being allowed to visit indices repeatedly.
This can be proved via the recurrence above for A_i^{(t)}.
In particular, observe that A_i^{(t)} \le A_i^{(t+2)} since, the set of positions available to index i at t+2 includes the positions available to it at t.
This observation, along with the initial one of the final state being all elements equal to M, gives a fairly simple solution.
Let’s consider only even values of t.
Because A_i^{(t)} \le A_i^{(t+2)}, once some element becomes M at an even t it will remain M for all larger even times too.
This allows us to binary search for the smallest even t where everything becomes M.
Performing the check for the binary search requires us to compute \max(A_{i-t}, A_{i-t+2}, \ldots, A_{i+t}) for each i, which is just a sliding window maximum problem on the circle.
In fact, we don’t even need the full power of the sliding window maximum deque; we only need to know whether each window like this contains at least one occurrence of M or not so just keeping a counter for M is enough to solve the problem, rather than a monotonic deque and whatnot.
This simplifies implementation a fair bit.
We can do the same for odd t too, and take the best answer among them.
It’s also possible to solve the problem in linear time, if you wish to try.
TIME COMPLEXITY:
\mathcal{O}(N\log N) per testcase.
CODE:
Editorialist's code (PyPy3)
def check(a, k): # k elements
n, M = len(a), max(a)
for st in [0, 1]:
ct, l, r = 0, st, st
for i in range(k):
ct += a[r] == M
r = (r + 2) % n
if ct == 0: return False
for i in range(n):
ct -= a[l] == M
ct += a[r] == M
l = (l + 2) % n
r = (r + 2) % n
if ct == 0: return False
return True
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if min(a) == max(a):
print(0)
continue
M = max(a)
ans = 10**9
# even time
lo, hi = 1, n+5
while lo < hi:
mid = (lo + hi)//2
# check length 2*mid -> 2*mid+1 elements
if check(a, 2*mid+1): hi = mid
else: lo = mid+1
if check(a, 2*lo+1): ans = min(ans, 2*lo)
# odd time
lo, hi = 1, n+5
while lo < hi:
mid = (lo + hi)//2
# check length 2*mid-1 -> 2*mid elements
if check(a, 2*mid): hi = mid
else: lo = mid+1
if check(a, 2*lo): ans = min(ans, 2*lo-1)
if ans == 10**9: print(-1)
else: print(ans)