Cod sursa(job #491953)

Utilizator cristian9Cristian Zloteanu cristian9 Data 12 octombrie 2010 22:34:00
Problema Transport Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include<stdio.h>
int main(){
	freopen ("transport.in", "r", stdin);
	freopen ("transport.out", "w", stdout);

	int n, i, k, v[16001], min, max, s, c;

	scanf("%d %d ", &n, &k);

	for(i=1; i<=n; i++)
		scanf("%d ", &v[i]);

    min=1; max=16000;
	while(min < max){
        c=(min+max-1)/2;
	    s=0;
	    int t=0;
	    for(i=1; i<=n; i++){
	        s+=v[i];
	        if(s>c){
	            t++;
	            s=v[i];
	        }
	    }
	    if(s>0 && s<=c)
            t++;
        if(t==k){
            printf("%d ", c);
            return 0;
        }
        if(t==0)
            max=c-1;
        else
            if(t>k)
                min=c-1;
            else
                max=c+1;
	}

	return 0;
}