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. (1)搜索广告CTR预估

    https://www.cnblogs.com/futurehau/p/6181008.html 一.广告投放系统 广告系统包含多个子系统.除了上图所示的广告投放系统外,还包含商业系统(广告库的获得) ...

  2. 【剑指Offer】66、机器人的运动范围

      题目描述:   地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时 ...

  3. SWING界面

    import java.awt.FlowLayout;import javax.swing.*;import java.awt.Container; public class kk extends J ...

  4. Pyspider爬虫简单框架——链家网

    pyspider 目录 pyspider简单介绍 pyspider的使用 实战 pyspider简单介绍 一个国人编写的强大的网络爬虫系统并带有强大的WebUI.采用Python语言编写,分布式架构, ...

  5. java中的redis工具类

    1.redis基础类 package com.qlchat.component.redis.template; import javax.annotation.PostConstruct; impor ...

  6. AndroidTreeView:Android树形节点View

     AndroidTreeView:Android树形节点View AndroidTreeView是github上的一个第三方开源项目,其在github上的项目主页是:https://github. ...

  7. springCloud学习-高可用的分布式配置中心(Spring Cloud Config)

    1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...

  8. hdu_1856_More is better_201403091720

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  9. BZOJ——T 1800: [Ahoi2009]fly 飞行棋

    Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1767  Solved: 1395[Submit][Status][Discuss] Descripti ...

  10. 《从零開始学Swift》学习笔记(Day60)——Core Foundation框架

    创文章,欢迎转载.转载请注明:关东升的博客   Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.尽管在Swift中调用这样的C语 ...