Cod sursa(job #1086329)

Utilizator The_DisturbedBungiu Alexandru The_Disturbed Data 17 ianuarie 2014 23:36:10
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.54 kb
#include<stdio.h>
int n,k,v[16013],i,j,st,dr,m,max;
inline bool merge(int y)
{
	int x=k-1,s=0;
	int i=0;
	while(i<n)
	{
		if(s+v[i]>y)
		{
			s=0;
			--x;
		}
		s+=v[i];
		++i;
	}
	return x>=0;
}
int main()
{
	freopen("transport.in","r",stdin);
	freopen("transport.out","w",stdout);
	scanf("%d%d",&n,&k);
	max=-1;
	for(i=0;i<n;++i)
	{
		scanf("%d",&v[i]);
		if(v[i]>max)max=v[i];
	}
	st=max;
	dr=(max*n)/k+1;
	while(st<dr)
	{
		m=(st+dr)/2;
		if(merge(m))dr=m;
		else st=m+1;
	}
	printf("%d",st);
	return 0;
}