车辆安排

车队分配算法
链接: https://www.nowcoder.com/acm/contest/112/B
来源:牛客网

题目描述

有n个队伍,每个队伍的人数小于等于5,每辆车最多坐5个人,要求一个队伍的人都在一辆车上,求最少的车数

输入描述:

第一行n
第二行n个数,表示每个队伍的人数

输出描述:

输出最少车数
示例1

输入

3
3 4 5

输出

3

备注:

n≤1e5
每个数小于等于5



#include<bits/stdc++.h>
using namespace std;

const int MAX = 1e5+10; 

int main()
{
	int n;
	int s[6];
	int cnt = 0;
	
	memset(s,0,sizeof(s));
	cin >> n;
	
	for(int i = 0; i < n; i++){
		int t;
		cin >> t;
		s[t]++;
	}
	
	cnt += s[5]; 
	while(s[4]){
		cnt++;
		s[4]--;
		s[1]--;
		
		if (s[1] < 0)
			s[1] = 0;
	}
	
	while(s[3] && s[2]){
		cnt++;
		s[3]--;
		s[2]--;
		
	}
	
	while(s[3]){
		cnt++;
		
		s[3]--;
		s[1] -= 2;
		if (s[1] < 0)
			s[1] = 0;
	}
	
	while(s[2]){
		cnt++;
		
		if (s[2] == 1){
			s[2]--;
			s[1] -= 3;
			
			if (s[1] < 0)
				s[1] = 0;
		}
		else{
			s[2] -= 2;
			s[1]--;
			
			if (s[1] < 0)
				s[1] = 0;
		}
	}
	
	cnt += (s[1]+4)/5; 
	
	cout << cnt << endl;
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值