Codeforces 158 B. Taxi[贪心/模拟/一辆车最多可以坐4人同一个群的小朋友必须坐同一辆车问最少需要多少辆车]
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群小朋友要乘车,每群小朋友的人数最少1人,最多4人,一辆车最多可以坐4人,同一个群的小朋友必须坐同一辆车,问最少需要多少辆车。
[分析]:代码注释
[代码];
/*
读入时统计各团人数,
4人的肯定要一车;
3人的也肯定要一车,且能加1人就多加1人;
2人的两两一车,最后若剩有1组2人的,则其占1车且能加1人就多加1人;
最后1人1人的4个组一车。
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int n,a[N];
int main()
{
while(cin>>n){
int k1=,k2=,k3=,k4=,cnt=;
for(int i=;i<n;i++){
cin>>a[i];
switch(a[i]){
case :k1++;break;
case :k2++;break;
case :k3++;break;
case :k4++;break;
}
}
cnt=k4+k3+k2/; // 判断一人一组的数量和三人一组的数量
if(k1>k3) k1-=k3;//多出的单人
else k1=; //如果一人一组的少于3人一组的k1 = 0; k2%=; //2人一组的互相匹配,看是否会剩下一组2人的
if(k2==){ ////最后若剩有1组2人的,则其占1车且能加1人就多加1人
cnt++;
if(k1>) k1-=; //因为2人一组的可以和两个1人一组的拼车,所以k1减去两个
else k1=;
}
cnt+=k1/; //1个人一组的可以互相拼,就是说4个一人一组的可以拼一个车。
if(k1%!=) cnt++; //多出来的单人
printf("%d\n",cnt);
}
} /*
2组的话直接乘以2对4相除
3,4组直接放入一辆车
然后对于1组 1
比较1组和3组的大小,若大于三组,则说明将其中的1与3合并后还剩下1组,
多余的1我们假设还与3合并,因为2组已经乘以2计算,最后结果对4相除,
相加即可
*/
/*
#include<bits/stdc++.h> using namespace std;
typedef long long ll;
int n,t,x[5];
int main()
{
cin>>n;
int ans=0;
for(int i=0;i<n;i++){
cin>>t;
x[t]++;
}
x[1]=max(x[1]-x[3],0);
cout<< x[3] + x[4] + (x[1] + 2*x[2] + 3) / 4 <<endl;
}
*/
Codeforces 158 B. Taxi[贪心/模拟/一辆车最多可以坐4人同一个群的小朋友必须坐同一辆车问最少需要多少辆车]的更多相关文章
- CodeForces 158 B. Taxi(模拟)
[题目链接]click here~~ [题目大意]n组团体去包车,每组团体的人数<=4,一辆车最多容纳4人,求所求车的数目最小 [解题思路]:思路见代码~~ // C #ifndef _GLIB ...
- CodeForces ---596B--Wilbur and Array(贪心模拟)
Wilbur and Array Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Su ...
- 【Codeforces 158B】Taxi
[链接] 我是链接,点我呀:) [题意] 每辆车可以载重4个人. 一共有n个组,每个组分别有s[i]个人. 要求每个组的人都在同一辆车里面. 问最少需要多少辆车 [题解] 将每个组的人数从小到大排序. ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- CodeForces - 158B.Taxi (贪心)
CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...
- Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- Codeforces 158B:Taxi
B. Taxi time limit per test 3 seconds memory limit per test 256 megabytes input standard input outpu ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- CodeForces - 50A Domino piling (贪心+递归)
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...
随机推荐
- UVA11825 Hacker's Crackdown 二进制集合+关于子集的动态规划
题意:有N台服务器,全部服务器都直接运行着完全相同的N个任务.对于每台电脑,你都可以进行“一次”操作,使得某(自己选定)一种任务停止,且同时会使得其他和这台服务器直接相连的电脑上面相同的服务完全终止. ...
- wget常用下载命令
wget wget是一个从网络上自动下载文件的自由工具,支持通过HTTP.HTTPS.FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理.wget名称的由来是“World Wide Web ...
- heap&stack的区别
1. heap (堆)是一个可动态申请的内存空间,一般所有创建的对象和数组都放在这里.stack (栈)是一个先进后出的数据结构,通常用于保存方法(函数)中的参数,局部变量.stack (栈)的空间小 ...
- C#定时器,定时做什么事情
http://www.cnblogs.com/bobositlife/archive/2015/09/29/aspnet-mvc-csharp-quartz-net-timer-task-schedu ...
- 68、TSPL指令集(标签打印机)
0.开发者pdf.sdk等资料详见: http://download.csdn.net/detail/kunyashaw/9376694 1.测试代码 包含文字打印.条形码打印.二维码打印 packa ...
- 【Gray Code】cpp
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- leetcode NO.171 Excel表列序号 (python实现)
来源 https://leetcode-cn.com/problems/excel-sheet-column-number/description/ 题目描述 给定一个Excel表格中的列名称,返回其 ...
- Android数据储存之File
openFileOutStream 和 openFileInStream FileInputStream fileInputStream = openFileInput(name); 打开应用下文件 ...
- maven学习(十四)——Eclipse中使用Maven插件
1.导入Maven项目 File→import
- 一个 Java 的 Socket 服务器和客户端通信的例子
一个 HelloWord 级别的 Java Socket 通信的例子.通讯过程: 先启动 Server 端,进入一个死循环以便一直监听某端口是否有连接请求.然后运行 Client 端,客户端发出连接请 ...