• Codeforces 158 B. Taxi[贪心/模拟/一辆车最多可以坐4人同一个群的小朋友必须坐同一辆车问最少需要多少辆车]


    http://codeforces.com/problemset/problem/158/B
    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
    4
    Input
    Copy
    8
    2 3 4 4 2 1 3 1
    Output
    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群小朋友要乘车,每群小朋友的人数最少1人,最多4人,一辆车最多可以坐4人,同一个群的小朋友必须坐同一辆车,问最少需要多少辆车。

    [分析]:代码注释

    [代码];

    /*
    读入时统计各团人数,
    4人的肯定要一车;
    3人的也肯定要一车,且能加1人就多加1人;
    2人的两两一车,最后若剩有1组2人的,则其占1车且能加1人就多加1人;
    最后1人1人的4个组一车。
    */
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    int n,a[N];
    int main()
    {
        while(cin>>n){
            int k1=0,k2=0,k3=0,k4=0,cnt=0;
            for(int i=0;i<n;i++){
                cin>>a[i];
                switch(a[i]){
                    case 1:k1++;break;
                    case 2:k2++;break;
                    case 3:k3++;break;
                    case 4:k4++;break;
                }
            }
            cnt=k4+k3+k2/2;  // 判断一人一组的数量和三人一组的数量
            if(k1>k3) k1-=k3;//多出的单人 
            else k1=0;   //如果一人一组的少于3人一组的k1 = 0;
    
            k2%=2;  //2人一组的互相匹配,看是否会剩下一组2人的
            if(k2==1){ ////最后若剩有1组2人的,则其占1车且能加1人就多加1人
                cnt++;
                if(k1>2) k1-=2; //因为2人一组的可以和两个1人一组的拼车,所以k1减去两个
                else k1=0;
            }
            cnt+=k1/4; //1个人一组的可以互相拼,就是说4个一人一组的可以拼一个车。
            if(k1%4!=0) cnt++;   //多出来的单人
            printf("%d
    ",cnt);
        }
    }

    /* 2组的话直接乘以2对4相除 3,4组直接放入一辆车 然后对于1组 1 比较1组和3组的大小,若大于三组,则说明将其中的1与3合并后还剩下1组, 多余的1我们假设还与3合并,因为2组已经乘以2计算,最后结果对4相除, 相加即可 */ /* #include<bits/stdc++.h> using namespace std; typedef long long ll; int n,t,x[5]; int main() { cin>>n; int ans=0; for(int i=0;i<n;i++){ cin>>t; x[t]++; } x[1]=max(x[1]-x[3],0); cout<< x[3] + x[4] + (x[1] + 2*x[2] + 3) / 4 <<endl; } */
  • 相关阅读:
    iOS 9 新特性 UIStackView
    自定义 URL Scheme 完全指南
    使用NSURLCache缓存
    swift 3.0 新特征
    《转之微信移动团队微信公众号》iOS 事件处理机制与图像渲染过程
    《转》使用NSURLSession发送GET和POST请求
    《转》IOS 扩展 (Extension)
    《转》__block修饰符
    《转》Objective-C Runtime(4)- 成员变量与属性
    《转》Objective-C Runtime(3)- 消息 和 Category
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8494680.html
Copyright © 2020-2023  润新知