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. tomcat8+idea远程调试

    window下 setenv.bat增加 set JPDA_OPTS=-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n lin ...

  2. [转]Git for windows 下vim解决中文乱码的有关问题

    Git for windows 下vim解决中文乱码的问题 原文链接:Git for windows 下vim解决中文乱码的有关问题 1.右键打开Git bash: 2.cd ~ 3.vim .vim ...

  3. package.json文件特殊符号含义

    package.json文件里的^和~表示什么意思呢 In the simplest terms, the tilde matches the most recent minor version (t ...

  4. 输出1到最大的N位数 【微软面试100题 第六十五题】

    题目要求: 输入数字n,按顺序输出从1到最大的n位10进制数. 例如,输入3,则输出1.2.3....999(最大的3位数). 参考资料:剑指offer第12题. 题目分析: 如果我们在数字前面补0的 ...

  5. OpenStack之Glance源码简析

    Glance简介 OpenStack镜像服务器是一套虚拟机镜像发现.注册.检索. glance架构图: Glance源码结构: glance/api:主要负责接收响应镜像管理命令的Restful请求, ...

  6. Python框架之Django学习笔记(六)

    模板 上篇博文学习了动态视图,但是,视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python 代码之中. def current_datetime(request): now = ...

  7. leetcode 【 Swap Nodes in Pairs 】python 实现

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...

  8. MFC深入浅出读书笔记第一部分

    最近看侯捷的MFC深入浅出,简单总结一下. 第一章首先就是先了解一下windows程序设计的基础知识,包括win32程序开发基础,什么*.lib,*.h,*.cpp的,程序入口点WinMain函数,窗 ...

  9. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛

    拖了好久了 链接:https://www.nowcoder.com/acm/contest/90/A来源:牛客网 跳台阶 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K, ...

  10. hdu 4183 EK最大流算法

    欢迎参加——每周六晚的BestCoder(有米!) Pahom on Water Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327 ...