zoukankan      html  css  js  c++  java
  • Codeforces Round #643 (Div. 2) B. Young Explorers(贪心)

    Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...

    Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter eiei  — his inexperience. Russell decided that an explorer with inexperience ee can only join the group of ee or more people.

    Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.

    Input

    The first line contains the number of independent test cases TT (1T21051≤T≤2⋅105 ). Next 2T2T lines contain description of test cases.

    The first line of description of each test case contains the number of young explorers NN (1N21051≤N≤2⋅105 ).

    The second line contains NN integers e1,e2,,eNe1,e2,…,eN (1eiN1≤ei≤N ), where eiei is the inexperience of the ii -th explorer.

    It's guaranteed that sum of all NN doesn't exceed 31053⋅105 .

    Output

    Print TT numbers, each number on a separate line.

    In ii -th line print the maximum number of groups Russell can form in ii -th test case.

    Example
    Input
    Copy
    2
    3
    1 1 1
    5
    2 3 1 2 2
    
    Output
    Copy
    3
    2
    首先把数组从小到大sort一遍,然后从头开始遍历。
    每到一个数先++cnt,如果这时cnt==当前的a[i]说明能凑成一组了,直接ans++,同时cnt=0置零。

    贪心策略就是用尽可能少的数凑出来一组,因此要从小到大遍历。
    比如 1 1 3,从小到大能凑出两组:1和1,从大到小只能凑出一组3 1 1。
    #include <bits/stdc++.h>
    using namespace std;
    int n,a[200005];
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            cin>>n;
            int i;
            for(i=1;i<=n;i++)scanf("%d",&a[i]);
            sort(a+1,a+n+1);
            int ans=0;
            int cnt=0;
            a[n+1]=0x3f3f3f3f;
            for(i=1;i<=n+1;i++)
            {
                cnt++;
                if(a[i]==cnt)
                {
                    ans++;
                    cnt=0;
                }
            }
            cout<<ans<<endl;
        }
        
    }
  • 相关阅读:
    ASP.net:Literal控件用法
    css如何自动换行对于div,p等块级元素(转)
    java ftp操作类
    java文件操作类
    geoserver图层显示
    java csv读取
    geoserver 源码编译(转)
    ArcGIS Engine 空间运算
    ArcMap操作技巧
    geoserver开发资料收集
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12902786.html
Copyright © 2011-2022 走看看