本人ACM菜鸟一枚,偶然做到这(水)题,发现师兄和网上给代码大多都是用数组做的,本人是直接算的,都是贪心的思想。但就这题而言,个人认为直接用代数方法来算的代码效率更高,上题:

题目大意说,有n组人,每组1~4个人,他们要坐出租车去别的地方,每辆出租车最多坐四人。 题目限定同一组的人必须坐在同一辆车里。问最少需要几辆的士?

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 thei-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 integerss1, s2, ..., sn (1 ≤ si ≤ 4). The integers are separated by a space,si is the number of children in thei-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的组坐一辆车,人数为3的一组和人数为1的一组坐一辆车,人数为2的组两组一辆车。这样之后,2人组剩下1组或0组,而1人组和3人组只会剩下一种组(一开始组数多的那种组)或者1人组和3人组 组数相等刚好匹配完。

如果剩下的是3人组,那每组3人组坐一辆车(因为同组人员必须一辆车),加上是否有2人组剩下需要单独一辆车,就是答案。

如果剩下的是1人组,且2人组没剩,则1人组四人一辆车走,有余下的再加一辆。如果2人组剩一组,则看剩下的1人组是否大于2,不大于就和2人组一辆车走了。若剩下1人组组数大于2,则两个1人组和剩下的2人组一辆车。然后,就又是只剩下1人组的情况了。

如果一开始1人组和3人组就匹配完了,那么再看看2人组有没有剩余就可以出答案了

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;
int n,a[100005];
long long sum,s1,s2,s3;main()
{cin>>n;for(int i=1;i<=n;i++){cin>>a[i];if(a[i]==1) s1++;//统计每组人数,4人组存在时答案直接+1 if(a[i]==2) s2++;if(a[i]==3) s3++;if(a[i]==4) sum++;}sum=sum+min(s1,s3)+s2/2;s2=s2%2;//2人组组数变更 ,非0即1 if(s3>s1)sum=sum+s2+s3-s1;//剩余的3人组每组单独一辆车,加上2人组 是否有余 else if(s1>s3){s1=s1-s3; //1人组 组数变更 if(s2==0){sum=sum+s1/4;if(s1%4!=0)sum=sum+1;}else{if(s1<=2)sum=sum+1;//余下的1人组小于2,和余下的2人组一辆车 else{sum=sum+1+(s1-2)/4;if((s1-2)%4!=0)sum=sum+1;}}}else  sum=sum+s2;//1人组和3人组人数相同,直接加上剩下二人组的一或零辆车  cout<<sum;
}

萌新欢迎各路大佬的指教~~

CodeForces 158B Taxi(代数算式解题)相关推荐

  1. Codeforces #158B Taxi

    一. 问题描述 After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to ...

  2. Codeforces Round #702 (Div. 3)解题报告

    Codeforces Round #702 (Div. 3) 全部题解 读错题意,写了半天真是心态爆炸,总的来看这次题目不难的. A. Dense Array http://codeforces.co ...

  3. codeforces A. Jeff and Digits 解题报告

    题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...

  4. codeforces B. Eight Point Sets 解题报告

    题目链接:http://codeforces.com/problemset/problem/334/B 一开始看到题目,有点怯,理解了题目后,其实并不难.这句话是突破口 three distinct ...

  5. codeforces 450B. Jzzhu and Sequences 解题报告

    题目链接:http://codeforces.com/problemset/problem/450/B 题目意思:给出 f1 和 f2 的值,以及n,根据公式:fi = fi-1 + fi+1,求出f ...

  6. codeforces 435 B. Pasha Maximizes 解题报告

    题目链接:http://codeforces.com/problemset/problem/435/B 题目意思:给出一个最多为18位的数,可以通过对相邻两个数字进行交换,最多交换 k 次,问交换 k ...

  7. codeforces B. Fox and Cross 解题报告

    题目链接:http://codeforces.com/problemset/problem/389/B 题目意思:给出一个由n行n列组成的board,其中'#'表示的一片地方恰好能画满十字架,画满的意 ...

  8. codeforces 483B Friends and Presents 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/B 题目意思:有两个 friends,需要将 cnt1 个不能整除 x 的数分给第一个friend, ...

  9. codeforces 476B.Dreamoon and WiFi 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/B 题目意思:给出两个字符串str1, str2,其中,str1 只由 '+' 和 '-' 组成,而 ...

最新文章

  1. delphi xe6 android ListView增加 Header或Footer 的方法
  2. 信号与系统 chapter1 常见信号及其变换
  3. 【汇编语言】(王爽)实验4解答
  4. linux touch权限不够,Linux下的Access、Modify、Change , touch的使用以及权限问题
  5. 做了几年Java开发,你必须重视这一点
  6. 多线程的等待唤醒机制
  7. Direct2D教程(十二)图层
  8. php导入导出xls表,TP5.0 PHPExcel 数据表格导出导入
  9. python顺序查找的递归算法_Python实现查找算法
  10. oracle创建用户和角色、管理授权以及表空间操作
  11. Discuz! X3.0/X3.1/X3.2通用 Apache伪静态规则
  12. 最新app源码下载:200款优秀Android项目源码
  13. 5-前置处理器:Pre Processors
  14. python中jieba库的作用_Python jieba库用法及实例解析
  15. TJA1403状态模式
  16. 5G 当自强,根系不能忘
  17. 6.6.5 住房公积金参缴记录
  18. Github图片无法显示解决(Mac版)
  19. html map插件,jQuery谷歌地图插件maplace.js
  20. 72个免费学习网站,涵盖所有,你值得拥有!【非推广】

热门文章

  1. JS Object 对象中删除属性
  2. YOLO V1 实时的目标检测 论文翻译
  3. 管理:身先士卒是必要的手段
  4. 神马VS百度,移动互联网战争全面升级!
  5. 计算机xp重装无声音怎么办,WinXP电脑声音不正常的处理方法
  6. 鲨鱼抓包(Wireshark)简易操作说明
  7. 安卓最牛教材!2021大厂Android面试经历,吐血整理
  8. ALV清缓存_SAP刘梦_新浪博客
  9. DAY SIX -- ospf的不规则区域问题和链路状态通告LSA
  10. 组建无线网络的六条思路