题目链接:http://poj.org/problem?id=1064
题意:
给出n个木棒的长度,需要获得m根长度相等的木棒,可以将木棒简短,问:可以得到的最长的木棍长度是多少?
思路:
直接二分求解吧,需要注意一下精度,题目要求保留两位小数,可以直接将数*100来控制精度。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=10010;
int T,n,m;
int a[maxn];
int main(){
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
while(~scanf("%d%d",&n,&m)){
int low=1,high=-1;
double x;
for(int i=0;i<n;i++){
scanf("%lf",&x);
a[i]=(int)(x*100);
if(a[i]>high) high=a[i];
}
int ans=0;
while(low<=high){
int count=0;
int mid=(low+high)/2;
for(int i=0;i<n;i++){
count+=a[i]/mid;
}
if(count<m){
high=mid-1;
}
else if(count>=m){
low=mid+1;
ans=max(ans,mid);
}
}
printf("%.2lf\n",(double)ans/100.0);
}
return 0;
}