Cod sursa(job #2794230)

Utilizator dragutamihai1234Draguta Mihai dragutamihai1234 Data 4 noiembrie 2021 15:24:22
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>

using namespace std;

ifstream f("transport.in");
ofstream g("transport.out");

int n, k, i, v[16010], l = 0, r = 256000000, m, sol;

int nrTrans(int c)
{
    int s = v[1], nr = 1;
    for (i = 2; i <= n; i++)
    {
        if (s + v[i] <= c)
            s += v[i];
        else
        {
            nr++;
            s = v[i];
        }
    }
    return nr;
}

int main()
{
    f >> n >> k;
    for (i = 1; i <= n; i++)
    {
        f >> v[i];
        if (v[i] > l)
            l = v[i];
    }
    while (l <= r)
    {
        m = (l + r) / 2;
        if (nrTrans(m) > k)
            l = m + 1;
        else
        {
            sol = m;
            r = m - 1;
        }
    }
    g << sol;
    return 0;
}