Cod sursa(job #530051)

Utilizator tudorsTudor Siminic tudors Data 6 februarie 2011 19:03:43
Problema Transport Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <stdio.h>
using namespace std;
int n,i,k,A[16001];
int maxim,s,ok,mij,st,dr;
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;
	while (st<=dr)
	{
		mij=(st+dr)/2;
		if (test(mij))
			dr=mij-1;
		else 
			st=mij+1;
	}
	fprintf(g,"%d",mij);
	fclose(f);
	fclose(g);
	return 0;
}