P1852 舰长的礼物

复制代码
#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;
}
复制代码

 

posted @   刘海烽  阅读(166)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示