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

Copy
4
input

Copy
8
2 3 4 4 2 1 3 1
output

Copy
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个小组出去坐车,一辆车最多坐四个人,第i个小组有si个人(0<si<=4),每辆车可以做多个小组,但是每个小组里的人必须坐在一起,求最少需要多少辆车

思路:分别记录下小组人数为4,3,2,1的个数,然后直接算就可以了(本来以为是贪心,但是仔细看了看题,暴力就可以了)

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp();
const int maxn=1e6+;
using namespace std;
int a[maxn];
int vis[];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int n;
ms(vis);
cin>>n;
for(int i=;i<n;i++)
{
cin>>a[i];
vis[a[i]]++;
}
//看来大佬的代码,发现就两行就可以了,差距好大(但是不知道为什么,这个代码比我写的要慢一点)
// vis[1]=max(vis[1]-vis[3],0);
// int ans=vis[3]+vis[4]+(vis[1]+2*vis[2]+3)/4;
int ans=vis[];
if(vis[]>=vis[])
{
ans+=vis[];
ans+=(vis[]+)/;
}
else
{
ans+=vis[];
vis[]-=vis[];
if(vis[]%)
{
ans+=(vis[]+)/;
if(vis[]>)
{
vis[]-=;
if(vis[]%)
ans=ans+vis[]/+;
else
ans=ans+vis[]/;
}
}
else
{
ans+=vis[]/;
ans+=vis[]/;
if(vis[]%)
ans++;
}
}
cout<<ans<<endl;
return ;
}

Codeforces 158B:Taxi的更多相关文章

  1. 【Codeforces 158B】Taxi

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

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

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

  3. CodeForces 158B Taxi(贪心)

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

  4. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  5. Codeforces 747D:Winter Is Coming(贪心)

    http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...

  6. Codeforces 747C:Servers(模拟)

    http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...

  7. Codeforces 749D:Leaving Auction(set+二分)

    http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...

  8. Codeforces 749B:Parallelogram is Back(计算几何)

    http://codeforces.com/problemset/problem/749/B 题意:已知平行四边形三个顶点,求另外一个顶点可能的位置. 思路:用向量来做. #include <c ...

  9. Codeforces 749C:Voting(暴力模拟)

    http://codeforces.com/problemset/problem/749/C 题意:有n个人投票,分为 D 和 R 两派,从1~n的顺序投票,轮到某人投票的时候,他可以将对方的一个人K ...

随机推荐

  1. eclipse导入Java源码

    eclipse导入Java源码 下载源码包(一般jdk都自带了, 我的没有) src.zip eclipse -> window -> preferences -> JAVA -&g ...

  2. 13信号signal

    信号 传送给进程的事件通知,完成异步通信 信号的产生 1.程序错误:硬件异常,除数为0,等 2.外部事件:定时器事件,按键中断(ctrl+c)等 3.显示请求:调用 kill,  raise 等信号发 ...

  3. Java Exception 和Error有什么区别?

    ① Exception 和Error 都是继承了Throwable类,在Java中只有Throwable类型的实例才可以被抛出或者捕获,它是异常处理机制的基本类型. ② Exception和Error ...

  4. HTTP从入门到入土(4)——URI、URL和URN

    URI URI全称:Uniform Resource Identifier,中文名为统一资源标识符.用来标识唯一标识互联网上的信息资源. Web上可用的所有资源,比如html.图像.视频等,都是由UR ...

  5. nw.js node-webkit系列(18)怎么对.js进行编译以防你的代码暴露出来

    原文链接:http://blog.csdn.net/zeping891103/article/details/50819102 参考:https://segmentfault.com/a/119000 ...

  6. 20155201 2016-2017-2 《Java程序设计》第十周学习总结

    20155201 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 Java密码技术 安全的三个属性 机密性 完整性 可用性 密码学: 主要是研究保密通信和信息 ...

  7. Windows10系统远程桌面连接出现卡顿如何解决

    最新的windows10系统下,用户只要开启远程桌面连接,就能够轻松地操控其他电脑.但是,最近部分用户在win10中启用远程连接时,发现电脑窗口变得非常缓慢卡顿,这是怎么回事呢?其实,该问题与系统的设 ...

  8. git clone时提示(gnome-ssh-askpass:29288): Gtk-WARNING **: cannot open display:

    一.背景 在服务器上克隆源码 二.解决 unset SSH_ASKPSS

  9. 51Nod 1737 配对(树的重心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1737 题意: 思路: 树的重心. 树的重心就是其所以子树的最大的子树结点 ...

  10. UVa 11021 麻球繁衍

    https://vjudge.net/problem/UVA-11021 题意:有k只麻球,每只活一天就会死亡,临死之前可能会生出一些新的麻球.具体来说,生i个麻球的概率为Pi.给定m,求m天后所有麻 ...