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;
}
*/

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

  1. CodeForces 158 B. Taxi(模拟)

    [题目链接]click here~~ [题目大意]n组团体去包车,每组团体的人数<=4,一辆车最多容纳4人,求所求车的数目最小 [解题思路]:思路见代码~~ // C #ifndef _GLIB ...

  2. CodeForces ---596B--Wilbur and Array(贪心模拟)

    Wilbur and Array Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Su ...

  3. 【Codeforces 158B】Taxi

    [链接] 我是链接,点我呀:) [题意] 每辆车可以载重4个人. 一共有n个组,每个组分别有s[i]个人. 要求每个组的人都在同一辆车里面. 问最少需要多少辆车 [题解] 将每个组的人数从小到大排序. ...

  4. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

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

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

  6. Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  7. Codeforces 158B:Taxi

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

  8. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  9. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

随机推荐

  1. Android开发——HandlerThread以及IntentService详解

    .HandlerThread Android API提供了HandlerThread来创建线程.官网的解释是: //Handy class for starting a new thread that ...

  2. Linux设置运行core dump

    系统配置vim /etc/sysctl.conf kernel.core_uses_pid = kernel.core_pattern = %e-core-%p-%t sysctl -p检查有没有生效 ...

  3. loj2051 「HNOI2016」序列

    ref #include <algorithm> #include <iostream> #include <cstdio> #include <cmath& ...

  4. flex布局之flex-grow和flex-shrink如何计算

    此文已由作者张含会授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 关于盒模型布局 如何实现两栏布局? (表格) 流式, 浮动, 定位 如何选择? 流式 > 浮动 > ...

  5. 学习python3之路的第一个小代码-----------9*9乘法表

    这个编写的简单,用两个循环迭代就行.下面就是我写的编码以及输出的结果 1 #!/usr/bin/env python 2 # encoding: utf-8 3 4 i = 1 5 6 while i ...

  6. 判断 js 数组是否为 empty

    function isEmpty(obj) { for(var key in obj) { if(obj.hasOwnProperty(key)) return false; } return tru ...

  7. CSU-1974 神奇药水

    CSU-1974 神奇药水 Description ​ 对于csuxushu来说,能够在CSU(California State University)组织2017年的ACM暑期集训让他感到十分荣幸. ...

  8. Leetcode 645.最长数对链

    最长数对链 给出 n 个数对. 在每一个数对中,第一个数字总是比第二个数字小. 现在,我们定义一种跟随关系,当且仅当 b < c 时,数对(c, d) 才可以跟在 (a, b) 后面.我们用这种 ...

  9. 【两种方式 Service References和 web References 】手把手教你引入webservice 服务

    1.对于一个webservie服务我们如何引入到自己的项目中去呢 第一种方法[Service References]:鼠标移到属性上 右键添加服务引用 然后在地址栏输入webservice 地址 点击 ...

  10. var和function定义方法的区别

    在JS中有两种定义函数的方式,1是var aaa=function(){...}2是function aaa(){...}var 方式定义的函数,不能先调用函数,后声明,只能先声明函数,然后调用.fu ...