Official

A - DEGwer's Doctoral Dissertation Editorial by hitonanode


The following greedy algorithm solves this problem:

  • Sort all typos in ascending order on the pages where they exist.
  • Starting with the first element after sorting, decide whether to correct each typo. If the current typo is less than \(T\) pages away from the last typo that was decided not to be corrected, then correct this typo. Otherwise, do not correct it.

This algorithm can be implemented as \(O(K \log K)\) using a comparison sort for the initial sorting, and as \(O(N + K)\) using a bucket sort.

Implementation Example (Python)

N, K, T = map(int, input().split())

A = sorted(map(int, input().split()))

last_typo_pos = -T
ret = 0

for a in A:
    if a < last_typo_pos + T:
        ret += 1
    else:
        last_typo_pos = a

print(ret)

posted:
last update: