zoukankan      html  css  js  c++  java
  • 牛客网 Wannafly挑战赛15 B-车辆安排

    传送门:https://www.nowcoder.com/acm/contest/112/B

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


    贪心思想:

    4 要么自己一车 ,要么和1一车

    3要么和2一车     要么和1一车 ,要么自己一车

    剩下的就只有2和 1了

     (1 ,1)可以代替2,反之不能。即优先把大的安排了

    思路:

    1.       5的凑够一车走

    2.       4的只能和1凑 也就是匹配的1最多为a[4]个

    3.       3先和2凑一车,然后剩下的3只能与1凑一车,匹配的1最多有2*a[3]个

    接下来就只剩 2  和1 了

    4.    首先考虑(2 ,2,1)的情况  (2 ,2)共有d组 那么匹配的1最多有d个, 接下来只有一个车里只有一个2的情况(此时2要么为0个要么只有1个),2最多和3个1配对 ,则匹配的1最多有3*a[3]个

    剩下的就只有1了。

    5.1自行挤一车

    注意: 

    代码:

    #include<stdio.h>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<iostream>
    #include<math.h>
    using namespace std;
    const int maxn=150;
    const int INF=0x3f3f3f3f;
    typedef long long ll;
    int a[10];
    int main()
    {
        int n,ans;
        while(~scanf("%d",&n))
        {
            ans=0;
            for(int i=0; i<=5; i++)
                a[i]=0;
            int val;
            while(n--)
            {
                scanf("%d",&val);
                ++a[val];
            }
            int d;
            ans=a[5];
            a[5]=0;
            if(a[1]>a[4])
                a[1]-=a[4];
            else
                a[1]=0;
            ans+=a[4];//4用完了
            a[4]=0;
            d=min(a[2],a[3]);// 2 3
            ans+=d;
            a[2]-=d;
            a[3]-=d;
            ans+=a[3];//3 1 1 or 3 1 or 3
            if(a[1]>2*a[3])
                a[1]-=2*a[3];
            else
                a[1]=0;
            //只剩下a[1] 和a[2]
            d=a[2]/2;//2 2 1 or 2 2
            if(a[1]>d)
                a[1]-=d;
            else
                a[1]=0;
            ans+=d;
            a[2]-=2*d;
            if(a[1]>3*a[2])
                a[1]-=3*a[2];
            else
                a[1]=0;
            ans+=a[2];
            a[2]=0;
            d=a[1]/5;
            ans+=d;
            if(a[1]>5*d)
                ans++;
            printf("%d
    ",ans);
        }
    }
    

  • 相关阅读:
    HDU 1058 Humble Numbers
    HDU 1421 搬寝室
    HDU 1176 免费馅饼
    七种排序算法的实现和总结
    算法纲要
    UVa401 回文词
    UVa 10361 Automatic Poetry
    UVa 537 Artificial Intelligence?
    UVa 409 Excuses, Excuses!
    UVa 10878 Decode the tape
  • 原文地址:https://www.cnblogs.com/dchnzlh/p/9780062.html
Copyright © 2011-2022 走看看