12分求调

P2440 木材加工

BDMsx @ 2024-12-14 13:53:01

#include <bits/stdc++.h>
using namespace std;
int n, k;

int main()
{
    cin >> n >> k;
    int L[n];
    for (int i = 0; i < n; i++)
    {
        cin >> L[i];
    }
    sort(L, L + n);
    int rt = 1, lt = L[n - 1], mid, nk;
    while (rt + 1 < lt)
    {
        nk = 0;
        mid = (rt + lt) / 2;
        for (int i = 0; i < n; i++)
        {
            nk += L[i] / mid;
        }
        if (nk == k)
        {
            do
            {
                nk = 0;
                mid += 1;
                for (int i = 0; i < n; i++)
                {
                    nk += L[i] / mid;
                }
            } while (nk != k);
            cout << mid - 1;
            return 0;
        }
        else if (nk > k)
            lt = mid;
        else
            rt = mid;
    }
    cout << 0;
}

|