HZAU 1209 Deadline (hash 贪心 水题不水)

There are N bugs to be repaired and some engineers whose abilities are roughly equal.
And an engineer can repair a bug per day. Each bug has a deadline A[i].
Question: How many engineers can repair all bugs before those deadlines at least?
1<=n<= 1e6. 1<=a[i] <=1e9
Input Description
There are multiply test cases.
In each case, the first line is an integer N , indicates the number of bugs.
The next line is n integers indicates the deadlines of those bugs.
Output Description
There are one number indicates the answer to the question in a line for each case.
Input
4
1 2 3 4
Output
1

这题只要想到思路还是很简单的,假设所有工程师每天都在修复bug,说白了就是把任务量平均分配到每个人身上。然后看当前任务最小需要多少天,不过这里需要注意的是,如果deadline如果是大于1e6的话对结果是不影响的,原因自己想想很简单。。。

按说是直接排序就行,时间是不会超时的,因为复杂度顶多也就O(nlogn)<1e8啊,但是不知道为什么这道题目O(n)复杂度都需要1000+ms,所以一开始没处理大于1e6的时候是超时的。

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1e6

int has[1000005];

int main()
{
    int n,tmp;
    while(~scanf("%d",&n))
    {
        memset(has,0,sizeof(has));
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&tmp);
            if(tmp<=n)
                has[tmp]++;
        }
        
        int sum=0,res=1;
        for(int i=1; i<=n;i++)
        {
            sum+=has[i];
            res=max(ans,(int)ceil(sum*1.0/i));
        }
        printf("%d
",res);
    }
    return 0;
}


之前直接贪心模拟的,可以过,不过不能忘了处理大于1e6的,否则超时(开始用set维护,不过TL,ORZ。。。)

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e6 + 5;

int a[MAXN];
int b[MAXN];
int tot;

int main()
{
    int n;
    int i;
    int j;
    int maxB;
    int lMaxB;

    while (~scanf("%d", &n)) {
        int n2 = n;
        for (i = 0; i < n; ++i) {
            scanf("%d", &a[i]);
            if (a[i] >= n2) {
                --i;
                --n;
            }
        }
        //cout << n << endl;
        sort(a, a + n);

        tot = 0;
        b[tot++] = 1;
        for (i = 1; i < n; ++i) {
            maxB = -1;
            for (j = 0; j < tot; ++j) {
                if (b[j] < a[i] && b[j] > maxB) {
                    maxB = b[j];
                    lMaxB = j;
                    break;
                }
            }

            if (maxB == -1) {
                b[tot++] = 1;
            } else {
                ++b[lMaxB];
            }

        }

        printf("%d
", tot);
    }

    return 0;
}


TL:

#include <bits/stdc++.h>
using namespace std;

#define MAXN 1000005
struct Class
{
    bool operator () (int a,int b)
    {
        return a>b;
    }
};

int arr[MAXN];
multiset<int,Class>mySet;
multiset<int,Class>::iterator iter;

int main()
{
    int T,tmp;
    while(~scanf("%d",&T))
    {
        mySet.clear();
        for(int i=0; i<T; i++)
            scanf("%d",&arr[i]);
        sort(arr,arr+T);
        for(int i=0; i<T; i++)
        {
            iter = mySet.upper_bound(arr[i]);
            if(iter==mySet.end())
            {
                mySet.insert(1);
            }
            else
            {
                tmp=(*iter)+1;
                mySet.erase(iter);
                mySet.insert(tmp);
            }
        }
        printf("%d
",mySet.size());
    }

    return 0;

}



原文地址:https://www.cnblogs.com/zswbky/p/6792892.html