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 ;
}
Codeforces 158B:Taxi的更多相关文章
- 【Codeforces 158B】Taxi
[链接] 我是链接,点我呀:) [题意] 每辆车可以载重4个人. 一共有n个组,每个组分别有s[i]个人. 要求每个组的人都在同一辆车里面. 问最少需要多少辆车 [题解] 将每个组的人数从小到大排序. ...
- CodeForces - 158B.Taxi (贪心)
CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...
- CodeForces 158B Taxi(贪心)
贪心,注意优先级,4单独,3与1先匹配,2与2匹配(注意判断2有没有剩下),然后2与两个1匹配,最后4个1匹配就可以了. #include<iostream> #include<cs ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- Codeforces 747D:Winter Is Coming(贪心)
http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...
- Codeforces 747C:Servers(模拟)
http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...
- Codeforces 749D:Leaving Auction(set+二分)
http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...
- Codeforces 749B:Parallelogram is Back(计算几何)
http://codeforces.com/problemset/problem/749/B 题意:已知平行四边形三个顶点,求另外一个顶点可能的位置. 思路:用向量来做. #include <c ...
- Codeforces 749C:Voting(暴力模拟)
http://codeforces.com/problemset/problem/749/C 题意:有n个人投票,分为 D 和 R 两派,从1~n的顺序投票,轮到某人投票的时候,他可以将对方的一个人K ...
随机推荐
- cocos代码研究(8)持续动作子类学习笔记
理论部分 时间间隔动作(ActionInterval)是一个在一段时间内执行的动作. 它有一个开始时间和完成时间.完成时间等于起始时间加上持续时间. ActionInterval的子类与位置有关的动作 ...
- http之请求报文request
https://blog.csdn.net/blueheart20/article/details/45174399 户端发送一个HTTP请求到服务器的请求消息包括以下格式: 请求行(request ...
- sql server 将时间中的时分秒改为00:00:00
select convert(varchar(10),getdate(),120
- uva1146 2-SAT问题
题意大白书 二分答案,然后对于每个可能的答案,跑一遍2-SAT就好了. #include <iostream> #include <string.h> #include < ...
- CentOS7 忘了root密码怎么办
今天打开很久没开机的另2台虚拟机,发现不记得root密码了. 于是参考了google搜索到的第一个答案,https://www.unixmen.com/reset-root-password-cent ...
- Python笔记 #08# NumPy: Statistic Basis
数据分析的基本步骤: 了解你的数据(get to know your data), 做一些统计学处理(像僵尸一样盯着数字不会带给你任何灵感!) 实现可视化(get a better feeling f ...
- oracle数据库中导入Excel表格中的数据
1.点击[工具]-->[ODBC 导入器],如图: 2.在导入器里选择第一个[来自ODBC的数据],用户名/系统DSN-->填写[Excel Files],输入用户名和密码,点击 [连接] ...
- java 的异常处理
一.异常的概念: java 中的异常通常指的是在运行期出现的错误,这样的错误也是比较难以调试的,解决问题的时候注意观察出现错误的名字和行号最重要 下面看这个例子: import java.io.*; ...
- 解决 "OperationalError: (sqlite3.OperationalError) no such table: ..."问题
参考:flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table 在用Flask写一个简单的py文件,做 ...
- QtTcp_资料
1.百度搜索关键字“Qt TCP” 2. 2.1.Qt学习之路_5(Qt TCP的初步使用) http://www.cnblogs.com/tornadomeet/archive/2012/06/30 ...