Cod sursa(job #256257)

Utilizator n3msizN3msiz n3msiz Data 11 februarie 2009 13:49:22
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
//C
//maxS<=C<=sumS

/*
for (c=maxS;c<=sumS;c++){
  verific daca se poate face tyransportul cu camion de capacitate c
  if (ok)
    break;
}
afisare c

*/


#include <stdio.h>
#define DIM 16002

int maxS, sumS, n, i, j, k, max, tc, nt, cok, p, u, c, ok;
int S[DIM];

int main(){
  FILE *f = fopen("transport.in","r");
  fscanf(f,"%d %d",&n, &k);
  for (i=1;i<=n;i++){
    fscanf(f,"%d",&S[i]);
    if (S[i]>max) {
      maxS = S[i];
    }
    sumS+=S[i];
  }

  fclose(f);


  p=maxS;
  u=sumS;
  while (p<=u) {
    c = (p+u)/2;
//    testez daca capacitatea c e buna
    nt = 1;
    tc = 0;
    ok = 1;

    for (i=1;i<=n;i++) {
      if(tc+S[i]<=c) {
	tc+=S[i];
      } else {
	nt++;
	tc = S[i];
      }
      if (nt>k) {
	ok = 0;
	break;
      }
    }


    if (ok) {
      cok = c;
      u = c-1;
    } else
      p = c+1;
  }

  FILE *g = fopen("transport.out","w");
  fprintf(g,"%d",cok);
  fclose(g);
  return 0;
}