Codeforces 158B:Taxi(贪心)

     题意:一辆车最多坐4人,同一组要坐在同一辆车上,不同组可以坐在同一辆车上,求最少需要的车辆数。

     解析:WA了4次,因为我贪心贪错了,对组员为1 的来说,应该先往组员=3的车上补,而不是先往2的车上补。因为组员=2的组数可能会把几辆车塞满,这个时候没法补。而对于组员=3的,一定可以往上补。

#include<iostream>
#include<cmath>
#include<map>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int a[maxn];
map<int,int>mp;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        mp[x]++;
    }
    int sum=0;
    sum+=mp[4];
    sum+=mp[3];
    if(mp[1]<=mp[3])
    {
        mp[1]=0;
    }
    if(mp[1]>mp[3])
    {
        mp[1]=mp[1]-mp[3];
    }
    int md=mp[2]*2;
    if(md%4==0)
    {
        sum+=md/4;
    }
    else
    {
        sum+=md/4;
        int kk=mp[2]*2%4;
        sum++;
        if(mp[1]<=(4-kk))
            mp[1]=0;
        else
            mp[1]=mp[1]-(4-kk);
    }
    sum+=mp[1]/4;
    if(mp[1]%4!=0)
        sum++;
    cout<<sum<<endl;
}
原文地址:https://www.cnblogs.com/liyexin/p/12757266.html