CodeForces 158B.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)?
The first line contains integer n (1 ≤ n ≤ 105) — the number of groups of schoolchildren. The second line contains a sequence of integers s1, s2, ..., sn (1 ≤ si ≤ 4). The integers are separated by a space, si is the number of children in the i-th group.
Print the single number — the minimum number of taxis necessary to drive all children to Polycarpus.
5
1 2 4 3 3
4
8
2 3 4 4 2 1 3 1
5
In the first test we can sort the children into four cars like this:
- the third group (consisting of four children),
- the fourth group (consisting of three children),
- the fifth group (consisting of three children),
- the first and the second group (consisting of one and two children, correspondingly).
There are other ways to sort the groups into four cars.
1 #include<stdio.h> 2 int main() 3 { 4 int n,x,a=0; 5 int b=0,c=0,d=0; 6 scanf("%d",&n); 7 while(n--) 8 { 9 scanf("%d",&x); 10 { 11 if (x==4) a++; 12 if (x==3) b++; 13 if (x==2) c++; 14 if (x==1) d++; 15 } 16 } 17 if(b>=d) 18 { 19 a+=b; 20 a+=((c+1)/2); 21 } 22 if (b<d) 23 { 24 a+=b; 25 d=d-b; 26 a+=(c/2); 27 c=c%2; 28 if (c==1) 29 { 30 a+=(d+1)/4+1; 31 } 32 if (c==0) 33 { 34 if (d>0) 35 { 36 a+=(d-1)/4+1; 37 } 38 if (d==0) 39 { 40 a=a+0; 41 } 42 } 43 } 44 printf("%d\n",a); 45 return 0; 46 }