1209: Deadline
Time Limit: 2 Sec Memory Limit: 1280 MBSubmit: 1046 Solved: 102
[ Submit][ Status][ Web Board]
Description
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
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
There are one number indicates the answer to the question in a line for each case.
Sample Input
4
1 2 3 4
Sample Output
1
HINT
题意:
有n(1<=n<=1000000)个bug需要程序猿来调试,程序员每天可以调试出1个bug,每个bug都必须在xi天前(包括xi)调试出来,求出所需最小程序猿的个数。
思路:
我们首先可以把最后期限xi大于n的不进行统计,因为xi大于n时最多也就只需要1个程序员,那么我们枚举天数1到n一共有多少个bug,bug的数量除于天数可得出到该天时所需要的程序员,如果还有余数那么我们还得再增加一个程序员。
举个例子:
5
1 2 2 3 4
第1天:1/1=1,num=1
第2天:3/2=1......1,num=2
第3天:4/3=1......1,num=2
第4天:5/4=1......1,num=2
第5天:5/5=1,num=1
所需最少的程序员就是2个。
Source
示例程序
#include <cstdio>
#include <cstring>
#include <algorithm>
int a[1000001]; //n最大为1000000,因此数组开到1000000即可
using namespace std;
int main()
{
int n,i,x,maxx,num,t;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
maxx=1;
num=0;
for(i=1;n>=i;i++)
{
scanf("%d",&x);
if(x<=n)
{
a[x]++;
}
}
for(i=1;n>=i;i++)
{
num=num+a[i];
t=num/i;
if(num%i!=0)
{
t++;
}
maxx=max(maxx,t);
}
printf("%d\n",maxx);
}
return 0;
}
/**************************************************************
Problem: 1209
Code Length: 740 B
Language: C++
Result: Accepted
Time:1088 ms
Memory:4940 kb
****************************************************************/