-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.py
56 lines (52 loc) · 1.45 KB
/
flatten.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import sys
sys.stdin = open("inputs/flatten_input.txt")
def find_min(counts):
for idx, c in enumerate(counts):
if c != 0:
return idx
def find_max(counts):
for idx, c in enumerate(counts[::-1]):
if c != 0:
return len(counts) - idx - 1
def dump(count, n, m, temp):
count[n] -= temp
count[m] -= temp
count[n + 1] += temp
count[m - 1] += temp
def solution(dump_num, boxes):
count = [0] * 101
for i in boxes:
count[i] += 1
n = find_min(count)
m = find_max(count)
while dump_num > 0:
if count[n] > count[m]:
temp = count[m]
if dump_num - temp < 0:
temp = dump_num
dump(count, n, m, temp)
dump_num -= temp
m -= 1
elif count[n] < count[m]:
temp = count[n]
if dump_num - temp < 0:
temp = dump_num
dump_num -= temp
dump(count, n, m, temp)
n += 1
else:
temp = count[n]
if dump_num - temp < 0:
temp = dump_num
dump(count, n, m, temp)
dump_num -= temp
m -= 1
n += 1
return find_max(count) - find_min(count)
def main():
for test_case in range(10):
dump_num = int(input())
boxes = list(map(int, input().split()))
print(f"#{test_case + 1} {solution(dump_num, boxes)}")
if __name__ == '__main__':
main()