Cod sursa(job #1503014)

Utilizator radu_uniculeu sunt radu radu_unicul Data 15 octombrie 2015 13:44:42
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <stdio.h>
int i,n,v[16001],c,k,dr,t,s,st,x;
int main()
{
    FILE *f=fopen("transport.in","r");
    FILE *g=fopen("transport.out","w");
    fscanf(f,"%d%d",&n,&c);
    for(i=1; i<=n; i++)
    {
        fscanf(f,"%d",&v[i]);
        if(v[i]>st)
            st=v[i];
        dr+=v[i];
    }
    while(st<=dr)
    {
        k=(st+dr)/2;
        t=0;
        x=c;
        for(i=1; i<=n; i++)
            if(t+v[i]<=k)
                t+=v[i];
            else
            {
                t=0;
                x--;
                i--;
            }
        x--;
        if(x<0)
            st=k+1;
        else
        {
            s=k;
            dr=k-1;
        }
    }
    fprintf(g,"%d",s);
    return 0;
}