Cod sursa(job #2607652)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 29 aprilie 2020 22:48:21
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

int n, k, i, s, t, st, dr, mij, max1, min1, a[16005];

int main()
{
    f >> n >> k;
    s = max1 = 0;
    for(i = 0; i < n; i++)
    {
        f >> a[i];
        s += a[i];
        max1 = max(max1, a[i]);
    }
    f.close();

    t = 0;
    st = max1;
    dr = s;
    min1 = s + 1;
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        s = 0;
        t = 1;
        for(i = 0; i < n; i++)
            if(s + a[i] <= mij)
                s += a[i];
            else
            {
                s = a[i];
                t++;
            }
        if(t <= k)
        {
            min1 = min(min1, mij);
            dr = mij - 1;
        }
        else
            st = mij + 1;
    }

    g << min1 << '\n';
    g.close();

    return 0;
}