Cod sursa(job #1197398)

Utilizator dalv_1337Pasita Vlad dalv_1337 Data 11 iunie 2014 20:36:13
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <fstream>
using namespace std;

const int nMax = 16001;
const int ValMax = 16000;

int v[nMax], n, k;

inline bool valid(int C)
{
	int sum=0, count=1, i;
	for (i=1; i<=n; ++i) {
		if (sum+v[i]>C) {
			++count, sum=0, --i;
			if (count>k) return false;
		}
		else sum+=v[i];
	}
	return true;
}

int main()
{
	ifstream fIn("transport.in");
	ofstream fOut("transport.out");
	
	fIn>>n>>k;
	for (int i=1; i<=n; ++i) fIn>>v[i], v[0]+=v[i];
	
	int st=1, dr=v[0], mij, sol=dr;
	while (st<=dr) {
		mij=(st+dr)>>1;
		if (valid(mij)) {
			dr=mij-1;
			if (mij<sol) sol=mij;
		}
		else st=mij+1;
	}
	
	fOut<<sol;
	
	return 0;
}