Codeforces 158B:Taxi

B. Taxi
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
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)?

Input

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.

Output

Print the single number — the minimum number of taxis necessary to drive all children to Polycarpus.

Examples
input
Copy
5
1 2 4 3 3
output
Copy
4
input
Copy
8
2 3 4 4 2 1 3 1
output
Copy
5
Note

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(1);
const int maxn=1e6+10;
using namespace std;
int a[maxn];
int vis[5];
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int n;
	ms(vis);
	cin>>n;
	for(int i=0;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[4];
	if(vis[3]>=vis[1])
	{
		ans+=vis[3];
		ans+=(vis[2]+1)/2;
	}
	else
	{
		ans+=vis[3];
		vis[1]-=vis[3];
		if(vis[2]%2)
		{
			ans+=(vis[2]+1)/2;
			if(vis[1]>2)
			{
				vis[1]-=2;
				if(vis[1]%4)
					ans=ans+vis[1]/4+1;
				else
					ans=ans+vis[1]/4;
			}
		}
		else
		{
			ans+=vis[2]/2;
			ans+=vis[1]/4;
			if(vis[1]%4)
				ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}
原文地址:https://www.cnblogs.com/Friends-A/p/10324460.html