Codeforces 158B(贪心问题)

B. Taxi
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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)?

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of groups of schoolchildren. The second line contains a sequence of integerss1, s2, ..., sn (1 ≤ si ≤ 4). The integers are separated by a space, si is the number of children in the i-th group.

Output

Print the single number — the minimum number of taxis necessary to drive all children to Polycarpus.

Examples
input
5
1 2 4 3 3
output
4
input
8
2 3 4 4 2 1 3 1
output
5
Note

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.


题目大意:

输入一个数n表示有n个小组,第二行表示n个小组各自的人数(人数不大于4)。每辆出租车最多可以乘坐4人,求要n各小组的人都坐上出租车,且所需出租车的最少数量是多少。

解题思路:

共有五种打车的方法:

4人一车;2+2人一车;3+1人一车;2+1+1人一车;1+1+1+1人一车。(为了简单起见,先用sort函数按从大到小排序,也可以不用)很容易想到我们应该要将小数加到大数上,尽可能的凑到4。这里用到了贪心算法,一个数想要加到4,并且使要加的组数最大,那么尽可能的加一些小一点的数,是最好的。比如2要加到4,肯定2+1+1比2+2要好。


代码实现:

  1.   #include<cstdio>  
  2.   #include<iostream>  
  3.   #include<algorithm>  
  4.   #include<cstring>  
  5.   using namespace std; 

  6.   int a[5];  
  7.   int main()  
  8.   {  
  9.    
  10.      int i,n,x,sum;  
  11.      while(~scanf("%d",&n))  
  12.      {  
  13.          memset(a,0,sizeof(a));  
  14.          sum=0;  
  15.          for(i=0; i<n; i++)  
  16.          {  
  17.              scanf("%d",&x);  
  18.              a[x]++;  
  19.          }  
  20.          sum+=a[4]+a[3];  
  21.          x=a[1]-a[3];  
  22.          if(a[2]%2==0)  
  23.              sum+=a[2]/2;  
  24.          else  
  25.          {  
  26.              sum+=(a[2]+1)/2;  
  27.             x-=2;  
  28.          }  
  29.          if(x>0)  
  30.          {  
  31.              sum+=x/4;  
  32.              x=x%4;  
  33.              if(x)  
  34.              sum++;  
  35.          }  
  36.          printf("%d\n",sum);
  37.      }  
  38.      return 0;  
  39.  } 
     



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值