Cod sursa(job #491745)

Utilizator cristian9Cristian Zloteanu cristian9 Data 12 octombrie 2010 11:35:18
Problema Transport Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 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;
	
	scanf("%d %d ", &n, &k);
	
	for(i=1; i<=n; i++)
		scanf("%d ", &v[i]);
		
	min=1;max=16000;
	while(r==0){
		s=0;
		t=0;
		c=(min+max)/2;
		for(i=1; i<=n; i++){
			s+=v[i];
			if(s>c){
				t++;
				s=v[i];
				r=1;
			}
		}
		if(r==1)
			t++;
		r=0;
		//printf("%d ", s);
		if(t==k){
			printf("%d ", c);
			return 0;
		}
		if(t==0)
			max=c;
		else
			if(t>k)
				min=c;
			else
				max=c;
	}
	
	return 0;
}