from collections import Counter n = input() seq = [int(x) for x in input().strip().split(' ')] counts = sorted(Counter(seq).values()) total_elements = sum(counts) used_elements = 0 i = 0 j = len(counts) - 1 subseq = 0 while used_elements < total_elements: leader_elements = counts[j] used_elements += leader_elements counts[j] -= leader_elements subseq += 1 j -= 1 taken_elements = 0 while True: if counts[i] == 0: break elif counts[i] + taken_elements < leader_elements: used_elements += counts[i] taken_elements += counts[i] counts[i] = 0 i += 1 else: take = leader_elements - taken_elements - 1 used_elements += take taken_elements += take counts[i] -= take break print(subseq)
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 40 41 42 | from collections import Counter n = input() seq = [int(x) for x in input().strip().split(' ')] counts = sorted(Counter(seq).values()) total_elements = sum(counts) used_elements = 0 i = 0 j = len(counts) - 1 subseq = 0 while used_elements < total_elements: leader_elements = counts[j] used_elements += leader_elements counts[j] -= leader_elements subseq += 1 j -= 1 taken_elements = 0 while True: if counts[i] == 0: break elif counts[i] + taken_elements < leader_elements: used_elements += counts[i] taken_elements += counts[i] counts[i] = 0 i += 1 else: take = leader_elements - taken_elements - 1 used_elements += take taken_elements += take counts[i] -= take break print(subseq) |