Table of Contents

ExplanationImplementation

Official Analysis (Java)

Explanation

For each diamond xx, we check how many other diamonds can be displayed alongside it in the case, i.e. how many other diamonds falls in the range [x,x+k][x, x + k]. Our answer will be the maximum count we find for any diamond.

Implementation

Time Complexity: O(N2)\mathcal{O}(N^2)

with open("diamond.in") as fin:
n, k = map(int, fin.readline().split())
diamonds = [int(fin.readline()) for _ in range(n)]
most = 0
"""
Iterate through all diamonds and test setting them
as the smallest diamond in the case.
"""
for x in diamonds:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!