Cod sursa(job #1009194)

Utilizator torckySuciu Victor torcky Data 12 octombrie 2013 16:51:18
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <cstdio>
using namespace std;
int v[16002],N,K;
bool verificare(int x){
    int s=0,camion=0;
    for(int i=1;i<=N;i++){
        if(v[i]>x){
            return 0;
        }
        if(s+v[i]>x){
            camion++;
            s=v[i];
        }
        else{
            s+=v[i];
        }
    }
    camion++;
    if(camion<=K){
        return 1;
    }
    else{
        return 0;
    }
}
int binary(int st,int dr){
    while(st<=dr){
        if(verificare((st+dr)/2)==1){
            dr=(st+dr)/2-1;
        }
        else{
            st=(st+dr)/2+1;
        }
    }
    return st;
}
int main()
{
    freopen("transport.in","r",stdin);
    freopen("transport.out","w",stdout);
    scanf("%d%d",&N,&K);
    for(int i=1;i<=N;i++){
        scanf("%d",&v[i]);
    }
    printf("%d",binary(1,256000000));
    return 0;
}