codeforces_158_B Python练习
codeforces158B Taxi
After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 ≤ si ≤ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)?
原意:n组小朋友,每组有1-4小朋友,上车,车子最多载4人,问最少的车子。
题解:1个与3个一车,2个与2个一车,1个与1个与2个为一车,四个1个为一车。
代码:
n = input() s = input().split() s = list(map(int,s)) a = list(range(0,4)) for i in range(0,4): a[i] = 0 taxi = 0 for i in range(0,len(s)): if (s[i] == 1): if (a[1] > 0): a[1] -= 1 else: a[3] += 1 taxi += 1 elif (s[i] == 2): if (a[2] > 0): a[2] -= 1 else: a[2] += 1 taxi += 1 elif (s[i] == 3): if (a[3] > 0): a[3] -= 1 else: a[1] += 1 taxi += 1 elif (s[i] == 4): taxi += 1 # a[2]==1 or a[2] ==0 if (a[3] > a[2]): taxi -= (a[2]*2) a[3] -= (a[2]*2) taxi -= (a[3] // 4) * 3 a[3] = a[3]%4 if (a[3] < 4 and a[3] > 1): taxi -= a[3]-1 else: taxi -= a[3] print(taxi)
许久没练,甚是手生,重学为习~