这道题目和1969题意差不多,连思路都差不多,具体代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=500001;
int p[maxn];
int mid;
int n,b;
bool ff(int k)
{
int i,sum=0;
for(i=0; i<n; i++)
{
sum+=p[i]/k;
if(p[i]%k)sum++;
if(sum>b)
return false;
}
return true;
}
int main()
{
int i;
while(scanf("%d%d",&n,&b)&&(n!=-1||b!=-1))
{
for(i=0; i<n; i++)
{
scanf("%d",&p[i]);
}
sort(p,p+n);
int min1=0;
int max1;
max1=p[n-1];
while(min1<max1)
{
mid=(min1+max1)/2;
if(ff(mid))
{
max1=mid;
}
else
{
min1=mid+1;//这里为什么要+1呢?因为那个mid是取整来的!自己想想
}
}
cout<<max1<<endl;
}
return 0;
}