Cod sursa(job #491943)

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

	int n, i, k, v[16001], min, max, c, r=0, s, t, ct=0, ctmax=0;

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

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

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

	return 0;
}