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) ...
 
随机推荐
- 【第四课】kaggle案例分析四
			
Evernote Export 比赛题目介绍 facebook想要准确的知道用户登录的地点,从而可以为用户提供更准确的服务 为了比赛,facebook创建了一个虚拟世界地图,地图面积为100km2,其 ...
 - 为什么Java中的密码优先使用 char[] 而不是String?
			
可以看下壁虎的回答:https://www.zhihu.com/question/36734157 String是常量(即创建之后就无法更改),会保存到常量池中,如果有其他进程可以dump这个进程的内 ...
 - Shell 脚本编程 基本语法:
			
Shell 脚本编程语法: 注: 文章来源 http://www.cnblogs.com/yunquan/p/6821850.html 视频来源:https://www.bilibili.com/vi ...
 - 10.多shard场景下relevence score可能不准确
			
主要知识点 多shard场景下relevence score可能不准确的原因 多shard场景下relevence score可能不准确解决方式 一.多shard场景下relevance sc ...
 - Linux下几种文件传输命令
			
Linux下几种文件传输命令 sz rz sftp scp 最近在部署系统时接触了一些文件传输命令,分别做一下简单记录: 1.sftp Secure Ftp 是一个基于SSH安全协议的文件传输管理工具 ...
 - Spring MVC-表单(Form)标签-列表框(Listbox)示例(转载实践)
			
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_listbox.htm 说明:示例基于Spring MVC 4.1.6. 以下示例 ...
 - 2015 测试赛 同构 hihoCoder
			
题目1 : 同构 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定2个树A和B,保证A的节点个数>=B的节点个数. 现在你需要对树A的边进行二染色. 一个好的染色 ...
 - pthread_rwlock pthread读写锁
			
原文: http://www.cnblogs.com/diegodu/p/3890450.html 使用读写锁 配置读写锁的属性之后,即可初始化读写锁.以下函数用于初始化或销毁读写锁.锁定或解除锁定读 ...
 - [RxJS] Throttling vs Debouncing
			
DebounceTime: It's like delay, but passes only the most recent value from each burst of emissions. T ...
 - 在Cocos2d-X中玩转精灵
			
创建一个Cocos2d-Xproject,project的文件夹例如以下图所看到的: 在Resourcees目录中加入一张png格式的图片 在HelloWorldScene.cpp文件里的bool H ...