首页 > 代码库 > rwkj 1430 二分查找

rwkj 1430 二分查找

#include<iostream>
using namespace std;
int n,k,a[10000];

int binsearch(int low,int high)
{
int i,len,s;
while(low<high)
{
len=(high+low)/2;
for(s=0,i=0;i<n;i++) s+=a[i]/len;
if(s>k) low=len+1;
else if(s<k) high=len-1;
else return len;
}
}
int main()
{ int i,m,len;
while(cin>>n>>k)
{ for(m=0,i=0;i<n;i++)
{ cin>>a[i];
if(a[i]>m) m=a[i];
}
len=binsearch(0,m);
if(len<1) cout<<0<<endl; else cout<<len+1<<endl;
}
return 0;
}