Cod sursa(job #2637794)

Utilizator Joke2111Pricop Tudor Joke2111 Data 24 iulie 2020 23:20:56
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

int n, k, V[16005], maxim, rez;

void fast ()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
}

bool check (int x)
{
    int cx=x;
    int nr=1;
    int i=1;
    while (i<=n)
    {
        if (x>=V[i])
        {
            x=x-V[i];
            i++;
        }
        else
        {
            nr++;
            x=cx;
        }
    }
    if (nr<=k)
        return 1;
    else return 0;
}

int cautbin (int st, int dr)
{
    while (st<=dr)
    {
        int mij=(st+dr)/2;
        if (check(mij)==1)
        {
            rez=mij;
            dr=mij-1;
        }
        else
            st=mij+1;
    }
    return rez;
}

int main()
{
    fast();
    freopen("transport.in","r", stdin);
    freopen("transport.out","w", stdout);

    cin >> n >> k;
    for (int i=1; i<=n; i++)
    {
        cin >> V[i];
        if (V[i]>maxim)
            maxim=V[i];
    }

    cout << cautbin (maxim, 256000000);

    return 0;
}