F - More Holidays Editorial by evima

Proposer's Implementation

This is almost the same as the official editorial, but you can reverse the order of the binary search and the full search of the starting point. In this case, if you directly count x when the range starts at the left end, you can move the range one by one and calculate the number of x by just looking at the character entering the range and the one leaving it.

Sample Implementation (Python)

N, M, K = map(int, input().split())
S = input()
lo, hi = K, N * M + 1
while lo + 1 < hi:
    mi = (lo + hi) // 2
    x = S.count('x') * (mi // N) + S[: mi % N].count('x')
    ok = False
    for i in range(N):
        ok |= x <= K
        if mi + i >= N * M:
            break
        x += S[(mi + i) % N] == 'x'
        x -= S[i] == 'x'
    if ok:
        lo = mi
    else:
        hi = mi
print(lo)

posted:
last update: