Cod sursa(job #1488862)

Utilizator al.mocanuAlexandru Mocanu al.mocanu Data 19 septembrie 2015 23:04:57
Problema Transport Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.56 kb
#include <stdio.h>
#define MAX 16005

int n, k, i, a[MAX], s, max, st, dr, mij;

int main(){
	freopen("transport.in", "r", stdin);
	freopen("transport.out", "w", stdout);
	scanf("%d%d", &n, &k);
	for(i = 1; i <= n; i++){
		scanf("%d", &a[i]);
		s += a[i];
		max = max < a[i] ? a[i] : max;
	}

	st = max;
	dr = s;

	while(st <= dr){
		int t = 0, nrtrans = 0;
		while(t < n){
			mij = (st + dr) / 2;
			while(mij >= a[t + 1])
				mij -= a[++t];
			nrtrans++;
		}
		if(nrtrans > k)
			st = (st + dr) / 2 + 1;
		else
			dr = (st + dr) / 2 - 1;
	}

	printf("%d\n", st);
	return 0;
}