B - Taxi(贪心)
Problem description
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 sifriends (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
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个人的车辆,这样租的车辆将会更少。于是用两个指针指向一头一尾,头指向人数较多的一组,尾指向人数较少的一组,如果当前两者相加比4大,说明头的这一组可以单独租一辆车,即num++(车辆数加1),i++(头指针往后移一位),尾指针不用处理;如果当前两者相加和不大于4,即sum<=4,车辆数先加1,同时尾指针往前移一位,因为移后尾指针指向(原来一组的前一组)当前一组的人数可能还可以凑合成一辆车,于是贪心往前,直到大于4为止。这样当两指针相等时,说明已经实现了贪心策略,此时的num即为最少车辆数。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int n,num=,sum=,a[];//num为车辆数
bool cmp(int a,int b){return a>b;}
int main(){
cin>>n;
for(int i=;i<n;++i)cin>>a[i];
sort(a,a+n,cmp);//降序排列
for(int i=;i<n;++i){//这里注意小于n,即前面往后移,后面往前贪心。
sum=a[i]+a[n-];
if(sum<=){//如果sum不大于4,说明可能还有几个组的人可以容纳进一辆车
num++;n--;//车数先加1,队伍减1,再来看看能否再容纳一个组的人数
while(sum+a[n-]<=){sum+=a[n-];n--;}//如果还可以容纳,那就n--;
}
else num++;//如果sum比4大,说明a[i]这个组的人可以单独坐一辆车
}
cout<<num<<endl;
return ;
}
B - Taxi(贪心)的更多相关文章
- CodeForces - 158B.Taxi (贪心)
CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...
- Codeforces 158 B. Taxi[贪心/模拟/一辆车最多可以坐4人同一个群的小朋友必须坐同一辆车问最少需要多少辆车]
http://codeforces.com/problemset/problem/158/B B. Taxi time limit per test 3 seconds memory limit pe ...
- CodeForces 158B Taxi(贪心)
贪心,注意优先级,4单独,3与1先匹配,2与2匹配(注意判断2有没有剩下),然后2与两个1匹配,最后4个1匹配就可以了. #include<iostream> #include<cs ...
- Codeforces 158B:Taxi
B. Taxi time limit per test 3 seconds memory limit per test 256 megabytes input standard input outpu ...
- P3076 [USACO13FEB]出租车Taxi
题目描述 Bessie is running a taxi service for the other cows on the farm. The cows have been gathering a ...
- bzoj3062[Usaco2013 Feb]Taxi*
bzoj3062[Usaco2013 Feb]Taxi 题意: Bessie在农场上为其他奶牛提供出租车服务,她必须赶到这些奶牛的起始位置,并把他们带到它们的目的地.Bessie的车很小,所以她只能一 ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- Mapreduce代码疑点(1)
一.Hadoop MultipleInputs.addInputPath 读取多个路径 https://blog.csdn.net/t1dmzks/article/details/76473905 M ...
- 逆元Inv(模板+应用)
逆元: 如果满足公式,则有a 是 b的逆元同时b也是a的逆元. 逆元的应用: 设c为b在对m取余的意义下的逆元: 在求解公式 (a / b) % m的时候,如果b可能会非常的大,所以会出现爆精度的问题 ...
- case....when ...多重判断
CASE...WHEN 进行多重判断 CASE WHEN A IS NOT NULL THEN B WHEN C IS NULL THEN CASE WHEN D IS NOT NULL THEN ...
- Vue动态组件&异步组件
在动态组件上使用keep-alive 我们之前曾经在一个多标签的界面中使用is特性来切换不同的组件: Vue.js的动态组件模板 <component v-bind:is="curre ...
- GeoTrust 企业(OV)型 增强版(EV) 多域名(SAN/UC) SSL证书
GeoTrust 企业(OV)型 增强版(EV) 多域名(SAN/UC)SSL证书(GeoTrust True BusinessID With EV Multi-Domain(SAN/UC) SSL ...
- Maven学习总结(4)——Maven核心概念
Maven学习总结(四)--Maven核心概念 一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识 ...
- code vs 3376 符号三角形
3376 符号三角形 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 如下图是由14个“+”和14个“-”组 ...
- PDF在线预览-pdfjs使用
请参考我的开源: https://github.com/wuyechun2018/itools/blob/master/src/main/webapp/WEB-INF/views/pdf/index. ...
- HDU 5392 BC #51
就是求最大公倍数,但要用分解质因子求. 自己写的WA到爆.... #include<iostream> #include<stdio.h> #include<math.h ...
- HDU 4363
这题是记忆化搜索很容易想到,但状态却不好设 dp[i][j][u][d][l][r][k].对于矩形为i*j,它的四周的颜色分别为u,d,l,r,横竖切的状态为k的种数. 其中要注意一个问题是,停止不 ...