Cod sursa(job #530057)

Utilizator tudorsTudor Siminic tudors Data 6 februarie 2011 19:18:07
Problema Transport Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <stdio.h>
using namespace std;
long long  n,i,k,A[16001];
long long maxim,s,ok,mij,st,dr,rez;
FILE *f,*g;

int test(int mij)
{
	int i=1,c=0,sum=0,bine=1;
	while (i<=n)
	{
		if (bine)
		{
			c++;
			bine=0;
		}
		if (sum+A[i]<=mij)
		{
			sum+=A[i];
			i++;
		}
		else
		{
			bine=1;
			sum=0;
		}
	}
	if (c<=k)
		return 1;
	else 
		return 0;
}

int main()
{
	f=fopen("transport.in","r");
	g=fopen("transport.out","w");
	
	fscanf(f,"%d %d",&n,&k);
	maxim=s=0;
	for (i=1;i<=n;i++)
	{
		fscanf(f,"%d",&A[i]);
		if (A[i]>maxim)
			maxim=A[i];
		s+=A[i];
	}
	st=maxim;
	dr=s;
	ok=1;
	rez=20000;
	while (st<dr)
	{
		mij=(st+dr)/2;
		if (test(mij))
		{
			dr=mij;
			if (mij<rez)
				rez=mij;
		}
		else 
			st=mij+1;
	}
	fprintf(g,"%d",rez);
	fclose(f);
	fclose(g);
	return 0;
}