Cod sursa(job #256516)

Utilizator Addy.Adrian Draghici Addy. Data 11 februarie 2009 20:51:46
Problema Transport Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
# include <stdio.h>

int S[16002],maxS,sumS,p,u,c,nt,tc,ok,cok,i,n,k,m;

int main() {

  FILE *f = fopen("transport.in","r");
  FILE *g = fopen("transport.out","w");

  fscanf(f,"%d %d",&n,&k);

  for (i=1;i<=n;i++) {
    fscanf(f,"%d",&S[i]);
    if (S[i]>maxS)
      maxS=S[i];
    sumS+=S[i];
  }

  p=maxS;
  u=sumS;

  while (p<=u) {
    c=p+(u-p)/2;
    nt=1;
    tc=0;
    ok=1;
    for (i=1;i<=n;i++)
      if (tc+S[i]<=c)
	tc+=S[i];
      else {
	tc=S[i];
	nt++;
      }
    if (nt>k)
      ok=0;
    if (ok) {
      cok=c;
      u=m-1;
    }
    else
      p=m+1;
  }

  fprintf(g,"%d",cok);


  fclose(f);
  fclose(g);

  return 0;
}