#include <iostream> #include <vector> #include <algorithm> #include <numeric> using namespace std; const double EPS = 1e-8; int main() { int n, k; cin >> n >> k; // 读入护心毛长度并求出平均值 vector<double> L(n); double sum_L = 0; for (int i = 0; i < n; ++i) { cin >> L[i]; sum_L += L[i]; } double avg = sum_L / k; // 二分查找最大长度 double left = 0, right = avg; double ans = -1; while (right - left >= EPS) { double mid = (left + right) / 2; int cnt = accumulate(L.begin(), L.end(), 0, [&](int s, double l) { return s + static_cast<int>(l / mid); }); if (cnt >= k) { ans = mid; left = mid; } else { right = mid; } } // 输出答案 if (ans != -1) { printf("%.2f\n", ans); } else { cout << 0 << endl; } return 0; }
标签:right,int,double,mid,舰长,include,ans,P1852,礼物 From: https://www.cnblogs.com/lhf123/p/17430812.html