记住这一题

链接:https://www.nowcoder.com/acm/contest/112/B
来源:牛客网
题目描述
有n个队伍,每个队伍的人数小于等于5,每辆车最多坐5个人,要求一个队伍的人都在一辆车上,求最少的车数
输入描述:
第一行n
第二行n个数,表示每个队伍的人数
输出描述:
输出最少车数
示例1
输入
3
3 4 5
输出
3

做了一个晚上,签到题也没做出来,而且这道题好像在cf上做过的…手动黑脸
看别人的代码看到这一个比较简单的思路,觉得很不错,了解一下
用while来代替if(写了好多的if写到最后贼乱…),然后运用了贪心的思想?

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=1e5+50;
int a[6];
int main(void){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        int num;
        scanf("%d",&num);
        a[num]++;
    }
    int ans=0;
    ans+=a[5];
    while(a[4]){
        ans++;
        a[4]--;
        if(a[1]){
            a[1]--;
        }
    }
    while(a[3]){
        ans++;
        a[3]--;
        if(a[2]){
            a[2]--;
        }
        else if(a[1]>=2){
            a[1]-=2;
        }
        else{
            a[1]=0;
        }
    }
    while(a[2]>=2){
        ans++;
        a[2]-=2;
        if(a[1]){
            a[1]--;
        }
    }
    while(a[2]){
        ans++;
        a[2]=0;
        if(a[1]){
            a[1]-=3;
        }
    }
    while(a[1]>0){
        ans++;
        a[1]-=5;
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/westbrook1998/article/details/80287531