二分经典模板题,L=0,R=inf,mid=(L+R)如果以mid为长度,能剪出至少k段绳子,L=mid否则R=mid
最坑的是输出,不能四舍五入,而是截断取小数
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
double a[10005],inf=1000000;
int k, n;
bool judge(double len)
{
int num = 0, i, t;
for (i = 1;i <= n;i++)
{
t = int(a[i] / len);
num += t;//能剪出的绳子的段数,向下取整
}
if (num < k)return 0;
else return 1;
}
int main()
{
int i, j;
double L=0, R, mid;
while (scanf("%d%d", &n, &k) == 2)
{
L = 0;R = inf;
for (i = 1;i <= n;i++)
scanf("%lf", &a[i]);
while ((R - L) >= 1e-5)
{
mid = (L + R) / 2;
if (judge(mid))L = mid;
else R = mid;
}
printf("%.2lf\n", floor(R * 100) / 100);
//必须截断地取小数,而不是四舍五入
//比如R=3.7745,不能直接输出R(3.78)而要输出3.77
}
return 0;
}