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. Java基础反射(二)

    原文地址http://blog.csdn.net/sinat_38259539/article/details/71799078 反射是框架设计的灵魂 (使用的前提条件:必须先得到代表的字节码的Cla ...

  2. python16_day38【flask】

    一.简介 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预 ...

  3. Java基础知识陷阱(九)

    本文发表于本人博客. 今天我来说说关于JAVA多线程知识,有错误请指出.大家都知道JAVA在服务端上处理也有很大优势,很多公司也有在服务器跑JAVA进程,这说明JAVA在处理这个多线程以及并发下也有一 ...

  4. 各版本的区别及含义(i386 、x86_64 、ppc )

    1.i386:是指兼容Intel 80386处理器     x86或80x86是英代爾Intel首先开发制造的一种微处理器体系结构的泛称.該系列較早期的處理器名稱是以數字來表示,並以“86”作為結尾, ...

  5. 5700刀打造3卡1080Ti深度学习机器【转】

    本文转载自:https://www.jianshu.com/p/ca2e003bf77e   5700美刀,打造3x1080Ti实验室GPU深度学习机器 最近为公司搭建了一台实验用的深度学习主机,在网 ...

  6. 从0开始学习 GITHUB 系列之「GIT 进阶」【转】

    本文转载自:http://stormzhang.com/github/2016/06/16/learn-github-from-zero5/ 版权声明:本文为 stormzhang 原创文章,可以随意 ...

  7. Java加密代码 转换成Net版

    java版本自己封装base64 package com.qhong; import java.io.UnsupportedEncodingException; import org.apache.c ...

  8. [BZOJ1176]Mokia

    Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000 ...

  9. HDU 3374 String Problem(最大最小表示+KMP)题解

    题意:给你一个字符串,这个字符串可以这样操作:把第一个字符放到最后一个形成一个新的字符串,记原式Rank为1,每操作一步Rank+1,问你这样操作得出的最小字典序的字符串的Rank和这样的字符串有几个 ...

  10. Java字符串分割(转)

    java.lang.String 的 split() 方法, JDK 1.4 or later public String[] split(String regex,int limit) 示例代码 p ...