Codeforces 158B:Taxi
3 seconds
256 megabytes
standard input
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)?
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.
Print the single number — the minimum number of taxis necessary to drive all children to Polycarpus.
5
1 2 4 3 3
4
8
2 3 4 4 2 1 3 1
5
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 ;
}
最新文章
- 从零自学Hadoop(12):Hadoop命令中
- java 27 - 10 反射之 动态代理的代码实现
- noi 2728 摘花生
- PIC32MZ tutorial -- Output Compare
- 【Visual Lisp】变体与安全数组
- ecshop添加商品选择品牌时如何按拼音排序
- Container With Most Water
- Be quiet
- Python时间戳和日期
- 在Magento产品页面的使用jqZoom
- python操作excel常用的方法
- 直接删除undo及temp表空间文件后的数据库恢复一例
- CentOS环境下使用GIT基于Nginx的私服搭建全过程
- SpriteBuilder中音频波长超过Timeline结尾的情况
- apriori &;&; fpgrowth:频繁模式与关联规则挖掘
- JSX的替代方案(译文)
- Tomcat中的Connector配置
- Leetcode_两数相加_python
- ERRORS: ?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
- 关于在Fragment中设置toolbar及菜单的方法
热门文章
- 2.7 The Object Model -- Bindings, Observers, Computed Properties:What do I use when?
- (25)HttpClient session
- MySQL从删库到跑路(二)——MySQL字符集与乱码解析
- spring AOP的注解实例
- extern--C#调用C++等其他非托管代码
- 高可用Mysql架构_Haproxy+keepalived+mycat集群的配置
- P1136 迎接仪式
- LINUX实践--程序破解
- stm32 Flash读写独立函数[库函数]
- 解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题