Problem description

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 sifriends (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

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.

解题思路:简单贪心。先排列,可以按升序排,也可以按降序排,无论按哪种方式,思路都一样。这里采用降序排。我们希望的是有更多组的人数(不分离每组里的人数)刚好凑成可以容纳4个人的车辆,这样租的车辆将会更少。于是用两个指针指向一头一尾,头指向人数较多的一组,尾指向人数较少的一组,如果当前两者相加比4大,说明头的这一组可以单独租一辆车,即num++(车辆数加1),i++(头指针往后移一位),尾指针不用处理;如果当前两者相加和不大于4,即sum<=4,车辆数先加1,同时尾指针往前移一位,因为移后尾指针指向(原来一组的前一组)当前一组的人数可能还可以凑合成一辆车,于是贪心往前,直到大于4为止。这样当两指针相等时,说明已经实现了贪心策略,此时的num即为最少车辆数。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
int n,num=,sum=,a[];//num为车辆数
bool cmp(int a,int b){return a>b;}
int main(){
cin>>n;
for(int i=;i<n;++i)cin>>a[i];
sort(a,a+n,cmp);//降序排列
for(int i=;i<n;++i){//这里注意小于n,即前面往后移,后面往前贪心。
sum=a[i]+a[n-];
if(sum<=){//如果sum不大于4,说明可能还有几个组的人可以容纳进一辆车
num++;n--;//车数先加1,队伍减1,再来看看能否再容纳一个组的人数
while(sum+a[n-]<=){sum+=a[n-];n--;}//如果还可以容纳,那就n--;
}
else num++;//如果sum比4大,说明a[i]这个组的人可以单独坐一辆车
}
cout<<num<<endl;
return ;
}

B - Taxi(贪心)的更多相关文章

  1. CodeForces - 158B.Taxi (贪心)

    CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...

  2. Codeforces 158 B. Taxi[贪心/模拟/一辆车最多可以坐4人同一个群的小朋友必须坐同一辆车问最少需要多少辆车]

    http://codeforces.com/problemset/problem/158/B B. Taxi time limit per test 3 seconds memory limit pe ...

  3. CodeForces 158B Taxi(贪心)

    贪心,注意优先级,4单独,3与1先匹配,2与2匹配(注意判断2有没有剩下),然后2与两个1匹配,最后4个1匹配就可以了. #include<iostream> #include<cs ...

  4. Codeforces 158B:Taxi

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

  5. P3076 [USACO13FEB]出租车Taxi

    题目描述 Bessie is running a taxi service for the other cows on the farm. The cows have been gathering a ...

  6. bzoj3062[Usaco2013 Feb]Taxi*

    bzoj3062[Usaco2013 Feb]Taxi 题意: Bessie在农场上为其他奶牛提供出租车服务,她必须赶到这些奶牛的起始位置,并把他们带到它们的目的地.Bessie的车很小,所以她只能一 ...

  7. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  8. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  9. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. java中关于数组的初始化

  2. Django - 获取表单数据的三种方式

    1.query set 对象 2.字典 3.query set 元组 备注:对象通过 ”对象.列名"方式访问,元组通过“对象.索引”方式访问.

  3. c/c++排坑(5) -- c语言中的申明

    C语言的申明总是令人头大,对于这块内容也一直让我头疼.希望通过这篇博客能够稍微梳理一下.材料和例子来源于<C专家编程> 一.C语言的申明的优先级规则 先来个例子,看看下面这行C代码到底是个 ...

  4. G. I love Codeforces

    G. I love Codeforces 题目大意:给你n个字符串,以及m个喜欢关系,如果u喜欢v,这时候u会把它的用户名改为 I_love_ 加上v当时的用户名 Examples input 5an ...

  5. BZOJ 4285 使者 (CDQ分治+dfs序)

    题目传送门 题目大意:给你一棵树,有三种操作,在两个点之间连一个传送门,拆毁一个已有的传送门,询问两个点之间的合法路径数量.一条合法路径满足 1.经过且仅经过一个传送门 2.不经过起点终点简单路径上的 ...

  6. Python 元组和列表

    Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 ...

  7. python--(十步代码学会线程)

    python--(十步代码学会线程) 一.线程的创建 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getname(): 返回线程名. # setName(): 设置 ...

  8. Linux 实用指令(4)

    目录 实用指令 1.指定运行级别 2.切换到指定运行级别的指令 3.帮助指令 3.1man获得帮助信息 3.2help指令 4.文件目录类 4.1pwd指令 4.2 ls指令 4.3 cd指令 4.4 ...

  9. Python与常见加密方式

    前言 我们所说的加密方式,都是对二进制编码的格式进行加密的,对应到Python中,则是我们的Bytes. 所以当我们在Python中进行加密操作的时候,要确保我们操作的是Bytes,否则就会报错. 将 ...

  10. noip模拟赛 时之终末

    题目背景 圣乔治:不用拘泥,剩下的时间已不多…… 圣乔治:直呼我的真名—— 丝佩碧雅:圣乔治大人 圣乔治:如今,已无法维持结界,或是抑制深渊的前进 圣乔治:既然如此,我将献上这副身躯,期望最后的战斗 ...