Cod sursa(job #2863281)

Utilizator caffeineMircea Baston caffeine Data 6 martie 2022 15:54:34
Problema Transport Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.33 kb
// https://infoarena.ro/problema/transport
#include <fstream>
std :: ifstream in("transport.in");
std :: ofstream out("transport.out");

#define N 16000

unsigned v[N+1], n, k, s, mx;

void read()
{
    in >> n >> k;
    for(unsigned i = 0; i < n; i++)
    {
        in >> v[i];
        if (v[i] > mx)
            mx = v[i];
        s += v[i];
    }
}

void solve()
{
    unsigned lbound = mx, rbound = s;
    unsigned capacity;
    while(1)
    {
        capacity = (lbound+rbound)/2;
        unsigned i = 0, j = 0;
        for(i; i < k && j < n; i++)
        {
            unsigned load = 0;
            for(j; j < n; j++)
            {
                if(load + v[j] <= capacity)
                    load+=v[j];
                else
                    break;
            }
        }
        // if we reached the end of the stack and performed k transports
        if(i == k && j == n)
        {
            out<<capacity;
            return;
        }
        // if we reached the end of the stack in less thank k transports
        // ==> the capacity is too big
        if(j == n) // i < k is implied
            rbound = capacity;
        // if we didn't reach the end of the stack in k transports
        else
            lbound = capacity;

    }
}

int main()
{
    read();
    solve();
}