Cod sursa(job #1566558)

Utilizator sulzandreiandrei sulzandrei Data 12 ianuarie 2016 12:16:25
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <iostream>
#include <fstream>
using namespace std;
typedef long long int lli;
ifstream in("transport.in");
ofstream out("transport.out");
lli v[16003],n,k,maxi = 0;
lli noftt( lli c)
{
    lli i = 0, cur = 0, noft = 0;
    while( i < n)
    {
        if ( cur + v[i] <=  c)
            cur += v[i];
        else
        {
            cur = v[i];
            noft ++;
        }
        i++;
    }
    if( cur != 0)
        noft++;
    return noft;
}
lli BS(lli st , lli dr)
{
    lli lo = st, hi = dr;
    while (hi - lo > 1)
    {
        lli mij = lo +(hi - lo)/2;
        lli noft = noftt(mij);
        if (noft <= k)
            hi = mij;
        else
            lo = mij+1;
    }
    if ( noftt(lo) <= k)
        return lo;
    return hi;
    /*while( st<=dr)
    {
        lli mij = (st+dr)/2;
        lli n_o_t = noftt(mij);
        if (n_o_t == k)
        {
            if( noftt(mij-1) >k)
                return mij;
            else
                dr = mij-1;
        }
        if( n_o_t < k )
            dr = mij-1;
        if( n_o_t > k)
            st = mij+1;
    }*/
}
 int main()
{
    in >> n >> k;
    for(lli i = 0 ; i < n ; i++)
    {
        in >>v[i];
        if (v[i]> maxi)
            maxi = v[i];
    }
    lli result = BS(maxi,n*16000);
    out<< result;
    return 0;
}