zoukankan      html  css  js  c++  java
  • Codeforces 158B:Taxi

    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
    Copy
    4
    
    input
    Copy
    8
    2 3 4 4 2 1 3 1
    
    output
    Copy
    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个小组出去坐车,一辆车最多坐四个人,第i个小组有si个人(0<si<=4),每辆车可以做多个小组,但是每个小组里的人必须坐在一起,求最少需要多少辆车

    思路:分别记录下小组人数为4,3,2,1的个数,然后直接算就可以了(本来以为是贪心,但是仔细看了看题,暴力就可以了)

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <math.h>
     6 #include <limits.h>
     7 #include <map>
     8 #include <stack>
     9 #include <queue>
    10 #include <vector>
    11 #define ll long long
    12 #define ms(a) memset(a,0,sizeof(a))
    13 #define pi acos(-1.0)
    14 #define INF 0x3f3f3f3f
    15 const double E=exp(1);
    16 const int maxn=1e6+10;
    17 using namespace std;
    18 int a[maxn];
    19 int vis[5];
    20 int main(int argc, char const *argv[])
    21 {
    22     ios::sync_with_stdio(false);
    23     int n;
    24     ms(vis);
    25     cin>>n;
    26     for(int i=0;i<n;i++)
    27     {
    28         cin>>a[i];
    29         vis[a[i]]++;
    30     }
    31     //看来大佬的代码,发现就两行就可以了,差距好大(但是不知道为什么,这个代码比我写的要慢一点)
    32     // vis[1]=max(vis[1]-vis[3],0);
    33     // int ans=vis[3]+vis[4]+(vis[1]+2*vis[2]+3)/4;
    34     int ans=vis[4];
    35     if(vis[3]>=vis[1])
    36     {
    37         ans+=vis[3];
    38         ans+=(vis[2]+1)/2;
    39     }
    40     else
    41     {
    42         ans+=vis[3];
    43         vis[1]-=vis[3];
    44         if(vis[2]%2)
    45         {
    46             ans+=(vis[2]+1)/2;
    47             if(vis[1]>2)
    48             {
    49                 vis[1]-=2;
    50                 if(vis[1]%4)
    51                     ans=ans+vis[1]/4+1;
    52                 else
    53                     ans=ans+vis[1]/4;
    54             }
    55         }
    56         else
    57         {
    58             ans+=vis[2]/2;
    59             ans+=vis[1]/4;
    60             if(vis[1]%4)
    61                 ans++;
    62         }
    63     }
    64     cout<<ans<<endl;
    65     return 0;
    66 }
  • 相关阅读:
    7A
    map最最最基本用法
    cccc超级酱油心得
    scu-4445
    初学算法之广搜
    初学算法之最基础的stl队列
    初学算法之筛选素数法
    go 虎牙爬取
    php使用xpath爬取内容
    go xpath
  • 原文地址:https://www.cnblogs.com/Friends-A/p/9308983.html
Copyright © 2011-2022 走看看