http://acm.hdu.edu.cn/showproblem.php?pid=4190
题目大意:n个城市b个投票箱,每个城市的票分摊在投票箱里,问怎么放投票箱使得所有城市中最多的投票箱的票数最少。
解题思路:二分答案
代码:
#include <math.h>
#include <stdio.h>
#define MAX(x,y) ((x)>(y)? (x):(y))
using namespace std;
int city[500100];
int main()
{
int n,b;
while(scanf("%d%d",&n,&b))
{
if(n==-1 && b==-1) break;
int ans=-1;
for(int i=1;i<=n;i++)
{
scanf("%d",&city[i]);
ans=MAX(ans,city[i]);
}
int l=1,r=ans;
while(l<r)
{
int temp=0;
int mid=(l+r)/2;
for(int i=1;i<=n;i++)
{
if(city[i]%mid) temp+=((int)((double)city[i]/(double)mid)+1);
else temp+=city[i]/mid;
}
if(temp>b) l=mid+1;
else r=mid;
}
printf("%d\n",l);
}
return 0;
}