Skip to content

Instantly share code, notes, and snippets.

@maestroviktorin
Created April 14, 2023 20:57
Show Gist options
  • Select an option

  • Save maestroviktorin/916c7471bacebc2c4a4e3eb8be466b2c to your computer and use it in GitHub Desktop.

Select an option

Save maestroviktorin/916c7471bacebc2c4a4e3eb8be466b2c to your computer and use it in GitHub Desktop.
Solution to the problem #6092 from the website of K. Polyakov.
with open('26-101.txt') as file:
rows = file.readlines()
N, K = tuple(int(x) for x in rows[0].split())
crates = sorted((int(x) for x in rows[1:]), reverse=True)
blocks = list()
while crates:
block = [crates.pop(0)]
_crates = crates.copy() # Copy of `crates` in the state when the biggest crate
# is already in `block`.
# All the crates that are added to `block`
# becomes `0` here, but not in the original `crates`
# as we iterate "over it".
for i in range(len(crates)):
if block[-1] - crates[i] >= K:
block.append(crates[i])
_crates[i] = 0
blocks.append(block)
# All the crates in `block` are filtered out.
crates = [crate for crate in _crates if crate]
print(len(blocks), max(len(block) for block in blocks))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment