Cod sursa(job #2076899)

Utilizator lucia.cstCostache Lucia lucia.cst Data 27 noiembrie 2017 13:14:17
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include<fstream>

using namespace std;

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

long long n, k, i, j, maxm=0, m, x, s=0, t, aux, sp, sg, ind, fn, d1=256000001;
int v[16001];

int main(){
    cin>>n>>k;
    for(i=1; i<=n; i++){
        cin>>v[i];
        if(v[i]>maxm)
            maxm=v[i];
        s+=v[i];
    }
    ind=maxm;
    fn=s;
    sp=0;
    do{
        x=fn-ind;
        x/=2;
        if(x==0)
            sp=1;
        x+=ind;
        i=1;
        t=0;
        do{
            sg=0;
            s=0;
            s+=v[i];
            i++;
            do{
                if((s+v[i])<=x){
                    s+=v[i];
                    i++;
                }
                else{
                    sg=1;
                    t++;
                    s=0;
                }
            }
            while((sg==0)&&(i<=n));
            if((s>0)&&(t<=x))
                t++;
        }
        while(i<=n);
        if(t<=k){
            if(x<=d1)
                d1=x;
            fn=x;
        }
        else
        ind=x;
    }
    while(sp==0);
    cout<<d1;
    return 0;
}