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
****************************************************************/