思路
直接找最大长度 –> 判断某个长度是否符合且最大:浮点数二分
拓展
#include <iostream>
using namespace std;
const int N = 100010;
const double eps = 1e-4;
int n, m;
int w[N];
bool check(double mid)
{
int cnt = 0;
for(int i = 0; i < n; i ++ )
cnt += w[i] / mid;
return cnt >= m;
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++ ) cin >> w[i];
double l = 0, r = 1e9;
while(r - l > eps)
{
double mid = (r + l) / 2;
if(check(mid)) l = mid;
else r = mid;
}
printf("%.2lf\n", r);
return 0;
}