Cod sursa(job #2656360)

Utilizator david_octavianoctavian david_octavian Data 7 octombrie 2020 15:59:38
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>

using namespace std;

ifstream cin("transport.in");
ofstream cout("transport.out");

const int NMAX=16000;
int v[NMAX+5],n,k;

bool ok(int c)
{
    int i,tr,sc;
    sc=tr=0;
    for(i=1;i<=n;i++)
    {
        if(v[i]>c)
            return 0;
        if(sc+v[i]<=c)
            sc=sc+v[i];
        else
        {
            tr++;
            sc=v[i];
        }
    }
    if(sc>0)
        tr++;
    return tr<=k;
}

int bs_left(int st,int dr)
{
    int med,last;
    while(st<=dr)
    {
        med=(st+dr)/2;
        if(ok(med))
        {
            last=med;
            dr=med-1;
        }
        else
            st=med+1;
    }
    return last;
}

int main()
{
    int i,s;
    cin>>n>>k;
    s=0;
    for(i=1;i<=n;i++)
    {
        cin>>v[i];
        s=s+v[i];
    }
    cout<<bs_left(1,s);
    cin.close();
    cout.close();
    return 0;
}