Cod sursa(job #480905)

Utilizator costyv87Vlad Costin costyv87 Data 30 august 2010 01:38:03
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <stdio.h>
int n,k,v[17000],i;
long max,sum;
void caut(int i,int j)
{
if (i<=j) {
            int m=(i+j)/2;
            int ok=0,nr=0,r=0;
            while (r<=n-1) {
            sum=0;

            while (r<=n-1 && sum<=m) { sum+=v[r]; r++;}
            if (r<=n-1 || (r==n && sum>m)) r--;
            if (v[r]>m) {ok=1; break;}
            nr++;
            }
            if (ok==1) caut(m+1,j);
                          else {
             if (nr<=k && (m<max || max==-1))  {max=m; caut(i,m-1); }
               else if (nr>k) caut(m+1,j);
                                else caut(i,m-1); }
            }
}
int main() {
FILE *f,*g;
f=fopen("transport.in","r");
g=fopen("transport.out","w");
fscanf(f,"%d%d",&n,&k);
for (i=0;i<=n-1;i++) fscanf(f,"%d",&v[i]);
max=-1;
caut(1,256000000);
fprintf(g,"%ld",max);
fclose(g);
return 0;
}