Codeforces 158B Taxi 题解

题目链接:

http://codeforces.com/problemset/problem/158/B

Description

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 ≤ 10^5) — 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.

题目大意:

有n伙小孩,每伙小孩有1~4人,大家拼出租车,出租车有4个座位,每伙小孩可以拼车,同一伙小孩不能分开,问最小要几辆出租车。

思路:

4人的团伙肯定要先坐车,1人团伙与3人团伙拼车,2个2人团伙也可以拼车,剩下的就可以另作打算,较为简单了。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int n,a[5],ans=0,k;
int main()
{
    memset(a,0,sizeof(a));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&k);
        a[k]++;
    } 
    ans+=a[4]+a[2]/2;
    a[2]%=2;
    k=min(a[1],a[3]);
    ans+=k;
    a[1]-=k;
    a[3]-=k;
    if(a[3]!=0) ans+=a[3];
    if(a[1]!=0)
    {
        ans+=a[1]/4;
        a[1]%=4;
    }
    k=a[1]+a[2]*2;
    if(k<=4&&k!=0) ans++;
    else if(k!=0) ans+=2;
    cout<<ans<<endl;
    return 0;
}

相关链接:

Codeforces 题解小全:
https://blog.csdn.net/ZJ_MRZ/article/details/81530091

Codeforces 1016A Death Note 题解:
https://blog.csdn.net/ZJ_MRZ/article/details/81474520

Codeforces 1017A The Rank 题解:
https://blog.csdn.net/ZJ_MRZ/article/details/81529980

Codeforces 1017B The Bits 题解:
https://blog.csdn.net/ZJ_MRZ/article/details/81530892

posted @ 2018-08-10 09:21  ZJ_MRZ  阅读(323)  评论(0编辑  收藏  举报