AOJ.850 電纜公司的煩惱 (二分+枚舉)
題意分析
從[1,average]二分枚舉長度即可,由于保留2位小數,可以将資料擴大10^2倍後後枚舉,輸出時除100即可。
代碼總覽
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define INF 0x3f3f3f3f
#define nmax 100000
#define MEM(x) memset(x,0,sizeof(x))
int a[nmax];
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int n,k;
scanf("%d %d",&n,&k);
{
double hh;
int r = -INF;
for(int i = 0; i<n ;++i){
scanf("%lf",&hh);
a[i] = hh* 100;
r = max(r,a[i]);
}
r++;
int l = 0;
int temp = 0,mid = 0;
while(l+1<r){
temp = 0;
mid = (l+r)/2;
//printf("%d %d %d/n",l,r,mid);
for(int i =0; i<n;++i){
temp+= a[i]/mid;
}
if(temp<k) r = mid;
else l = mid;
}
printf("%.2f
",l*1.0/100);
}
return 0;
}