Cod sursa(job #525858)

Utilizator valentin.harsanValentin Harsan valentin.harsan Data 26 ianuarie 2011 16:31:47
Problema Transport Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include<iostream>
#include<fstream>
using namespace std;

ifstream aa("transport.in");
ofstream ss("transport.out");
int sum,n,k,i,j,pas,x[16001],smax,ssmax;
bool test(int y);
int main() {
	aa >> n >> k;
	for (i=1;i<=n;++i) {
		aa >> x[i];
		sum+=x[i];
		if (x[i]>smax) smax=x[i];
	}
	pas=1;
	while (pas<=sum-smax) pas*=2;
	pas/=2;
	while (pas>0) {
		if (test(smax+pas)) ssmax=smax+pas;
		else smax+=pas;
		pas/=2;
	}
	ss << ssmax;
	aa.close();
	ss.close();
	return 0; 
}
bool test(int y) {
	int nr=1,cap=0;
	for (int kk=1;kk<=n;++kk) {
		if (cap+x[kk]>y) {
			cap=0;
			++nr;
		}
		cap+=x[kk];
	}
	if (nr<=k) return true;
	return false;
}