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;
        }
        
    }
  • 相关阅读:
    解决Windows 2000无法访问Windows XP共享目录的问题
    手动清除后门程序Iexplores.exe
    超级天使投资网访谈
    中國web2.0現狀與趨勢調查報告
    google AdSense的佣金政策 (GOOGLE 研究 → Google 服务 → Adsense )
    分类信息和搜索引擎
    web 2.0是生产关系:说徐博客赚钱
    赛门铁克联手八笛众和推在线安全服务 狼人:
    2009年六大网络安全威胁:SQL注入攻击位列榜首 狼人:
    网民关注iPhone、Google、微软和安全 狼人:
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12902786.html
Copyright © 2011-2022 走看看