ACwing 680 cut rope

Title description:

There are N ropes, and the length of the i-th rope is LiLi. Now you need M ropes of equal length. You can cut the N ropes at will (not spliced). Please help to calculate the longest length of the M ropes. How many.

Input format

The first line contains 2 positive integers N and M, which represent the number of original ropes and the number of required ropes.

The second line contains N integers, where the i-th integer LiLi represents the length of the i-th rope.

Output format

Output a number, indicating the longest length after cutting, with two decimal places.

data range

1≤N,M≤1000001≤N,M≤100000,
0<Li<1090<Li<109

Input sample:

3 4
3 5 4

Sample output:

2.50

Sample explanation

The first and third ropes were cut into a 2.50-length rope, and the second rope was cut into two 2.50-length ropes, exactly 4.

 Dichotomous

#include <iostream>
#include <cstdio>

using namespace std;
const int MAX = 100009;

int n, m;
int a[MAX];

bool check(double len)
{
    int sum = 0;
    for(int i = 0; i < n; i++)
        sum += a[i] / len;

    return sum >= m;
}
int main()
{
    scanf("%d%d", &n, &m);

    int mmax = 0;
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        mmax = max(a[i], mmax);
    }

    double l = 0, r = (double)mmax;
    while( r - l >= 1e-4)
    {
        double mid = (l + r) / 2;
        if(check(mid))
            l = mid;
        else
            r = mid;
    }
    printf("%.2f\n", r);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113036145