http://codeforces.com/problemset/problem/158/B
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 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.

Output

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

Examples
Input

Copy
5
1 2 4 3 3
Output
4
Input

Copy
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群小朋友要乘车,每群小朋友的人数最少1人,最多4人,一辆车最多可以坐4人,同一个群的小朋友必须坐同一辆车,问最少需要多少辆车。

[分析]:代码注释

[代码];

/*
读入时统计各团人数,
4人的肯定要一车;
3人的也肯定要一车,且能加1人就多加1人;
2人的两两一车,最后若剩有1组2人的,则其占1车且能加1人就多加1人;
最后1人1人的4个组一车。
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int n,a[N];
int main()
{
while(cin>>n){
int k1=,k2=,k3=,k4=,cnt=;
for(int i=;i<n;i++){
cin>>a[i];
switch(a[i]){
case :k1++;break;
case :k2++;break;
case :k3++;break;
case :k4++;break;
}
}
cnt=k4+k3+k2/; // 判断一人一组的数量和三人一组的数量
if(k1>k3) k1-=k3;//多出的单人
else k1=; //如果一人一组的少于3人一组的k1 = 0; k2%=; //2人一组的互相匹配,看是否会剩下一组2人的
if(k2==){ ////最后若剩有1组2人的,则其占1车且能加1人就多加1人
cnt++;
if(k1>) k1-=; //因为2人一组的可以和两个1人一组的拼车,所以k1减去两个
else k1=;
}
cnt+=k1/; //1个人一组的可以互相拼,就是说4个一人一组的可以拼一个车。
if(k1%!=) cnt++; //多出来的单人
printf("%d\n",cnt);
}
} /*
2组的话直接乘以2对4相除
3,4组直接放入一辆车
然后对于1组 1
比较1组和3组的大小,若大于三组,则说明将其中的1与3合并后还剩下1组,
多余的1我们假设还与3合并,因为2组已经乘以2计算,最后结果对4相除,
相加即可
*/
/*
#include<bits/stdc++.h> using namespace std;
typedef long long ll;
int n,t,x[5];
int main()
{
cin>>n;
int ans=0;
for(int i=0;i<n;i++){
cin>>t;
x[t]++;
}
x[1]=max(x[1]-x[3],0);
cout<< x[3] + x[4] + (x[1] + 2*x[2] + 3) / 4 <<endl;
}
*/

最新文章

  1. 2.SDK目录结构和adb工具及命令介绍
  2. Ubuntu 14.04 下搭建SVN服务器 svn://
  3. Jmeter属性和变量
  4. HDU 4630-No Pain No Game(线段树+离线处理)
  5. PHP学习笔记(5) - 选择一个合格的框架
  6. mac brew 安装包下载失败解决
  7. 二分查找(Java)
  8. 公共 DNS server IP 地址
  9. Trie - leetcode [字典树/前缀树]
  10. 数据库DDL操作
  11. MySQL系列(一)--基础知识大总结
  12. 王垠:我和Google的故事
  13. openlayers4 入门开发系列之聚合图篇(附源码下载)
  14. [Swift]LeetCode528. 按权重随机选择 | Random Pick with Weight
  15. 千万不要随意在网上下载ojdbcjar包来使用,ORA-01461错误解决
  16. python编程之如何在Windows上安装python
  17. cmd快速设置本机ip和dns【转】
  18. IDEA常用配置
  19. windows下安装ElasticSearch 5
  20. ssh文件配置

热门文章

  1. LightOJ - 1341 Aladdin and the Flying Carpet(数论)
  2. 栈及其DFS:B - Parentheses Balance
  3. 全球征集-如何实现回文SQL的查询
  4. json对象数据列数
  5. 收藏网址 jquery学习
  6. [Cocos2dx Bug] [win32] Function CCFileUtils::fullPathFromRelativeFile forget consider the path separated by &#39;\\&#39;
  7. BZOJ 1877:[SDOI2009]晨跑(最小费用最大流)
  8. 【bzoj3944/bzoj4805】Sum/欧拉函数求和 杜教筛
  9. 文本文件txt生成excel
  10. &quot;二进制&quot; 转化为 &quot;十六进制