Cod sursa(job #1332111)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 1 februarie 2015 18:30:20
Problema Transport Scor 80
Compilator c Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <stdio.h>
#define MAXN 16000
int v[MAXN], n;
inline int nrz(int x){
    int s=0, i, z=1;
    for(i=0; i<n; i++){
        if(s+v[i]>x){
            z++;
            s=v[i];
        }else{
            s+=v[i];
        }
    }
    return z;
}
int main(){
    int k, rez, pas, i;
    FILE *fin, *fout;
    fin=fopen("transport.in", "r");
    fout=fopen("transport.out", "w");
    fscanf(fin, "%d%d", &n, &k);
    rez=0;
    for(i=0; i<n; i++){
        fscanf(fin, "%d", &v[i]);
        if(rez<v[i]){
            rez=v[i];
        }
    }
    if(n==1){
        fprintf(fout, "%d\n", rez);
    }else{
        for(pas=1<<27; pas!=0; pas>>=1){
            if(nrz(rez+pas)>k){
                rez+=pas;
            }
        }
        fprintf(fout, "%d\n", rez+1);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}