from collections import Counter
def main():
n = int(input())
nums = [int(i) for i in input().split()]
counter = Counter(nums)
occurs = [[v, k] for k, v in counter.items()]
occurs.sort(reverse=True)
l, r = 0, len(occurs) - 1
groups = 0
while l <= r:
if l == r:
return groups + 1
leader_occur = occurs[l][0]
to_consume = leader_occur - 1
while to_consume > 0:
to_eat = occurs[r][0]
if to_consume >= to_eat:
to_consume -= to_eat
occurs[r][0] = 0
r -= 1
if l == r:
return groups + 1
else:
occurs[r][0] -= to_consume
to_consume = 0
groups += 1
l += 1
if __name__ == "__main__":
result = main()
print(result)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | from collections import Counter def main(): n = int(input()) nums = [int(i) for i in input().split()] counter = Counter(nums) occurs = [[v, k] for k, v in counter.items()] occurs.sort(reverse=True) l, r = 0, len(occurs) - 1 groups = 0 while l <= r: if l == r: return groups + 1 leader_occur = occurs[l][0] to_consume = leader_occur - 1 while to_consume > 0: to_eat = occurs[r][0] if to_consume >= to_eat: to_consume -= to_eat occurs[r][0] = 0 r -= 1 if l == r: return groups + 1 else: occurs[r][0] -= to_consume to_consume = 0 groups += 1 l += 1 if __name__ == "__main__": result = main() print(result) |
English