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 ...
随机推荐
- [USACO]Bovine Genomics
Description 给定两个字符串集合A,B,均包含N个字符串,长度均为M,求一个最短的区间[l,r],使得不存在字符串\(a\in A,b\in B,\)且\(a[l,r]=b[l,r]\) , ...
- PAT Advanced 1001
1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits mu ...
- (HTML)A标签伪元素选择器的继承关系
①如果a:link{}也存在,那么不管a{}放到哪里,a{}和a:link{}冲突的属性都会采用a:link{}的,不冲突的属性若存在a{}中,会被a:link{}. a:visited{} .a:h ...
- oracle 基本函数
1)字符串函数---length()函数 用于返回字符串长度 select t.name,length(t.name) from tb_person t 2)向左补全字符串---LPAD()函数 L ...
- Js中的undefined和not defined
1.undefined 已经声明,但未赋值 2.not defined 未声明,报错
- hdu3366 Count the string
考虑dp[i]代表前缀s[1...i]出现的次数,必定有dp[nxt[i]] += dp[i] 倒着推就是了 #include <iostream> #include <cstrin ...
- 图说不为人知的IT传奇故事-4-王安用一生来跟IBM抗衡
此系列文章为“图说不为人知的IT传奇故事”,各位大忙人可以在一分钟甚至几秒内了解把握整个内容,真可谓“大忙人的福利”呀!!希望各位IT界的朋友在钻研技术的同时,也能在文学.历史上有所把握.了解这些故事 ...
- 使用charles进行https抓包
一.charles电脑端设置 1.在Charles的菜单栏上选择"Proxy"->"Proxy Settings",填入代理端口8888(这个端口不一定填 ...
- 重新安装Linux自带的JDK
1.卸载现有jdk 查看本机已经安装的JDK的版本: [root@mcb ~]# java -version java version "1.6.0" OpenJDK Runtim ...
- 牛客网python试题-错误整理-20180711
######## >>>[3] in [1,2,3,4] False >>>3 in [1,2,3,4] True ######## x = (y = z + 1) ...