Cod sursa(job #463739)

Utilizator deneoAdrian Craciun deneo Data 17 iunie 2010 12:58:21
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <cstdio>
#include <cstdlib>
using namespace std;

#define MAXN 16000
#define max(a,b) (((a)>(b))?(a):(b))
int n,k,val[MAXN],max = 0;

inline bool check(int c){
	if(c < max)
		return false;

	int ctrans = 0, nt = 1;

	for(int i = n - 1; i >= 0; --i){
		if( ctrans + val[i] <= c){
			ctrans += val[i];
		}else{
			ctrans = val[i];
			nt++;
		}
	}
	return nt <= k;
}

inline int solve(){
	int beg = 0, end = 256000000,mdl,last;

	while(beg <= end){
		mdl = beg + ((end - beg)>>1);
		if(check(mdl)){
			last = mdl,end = mdl - 1;
		}else{
			beg = mdl + 1;
		}
	}
	
	return last;
}

int main(){
	FILE* fin = fopen("transport.in", "r");
	FILE* fout = fopen("transport.out", "w");
	
	fscanf(fin, "%d %d\n", &n, &k);
	for(int i = 0; i < n; ++i){
		fscanf(fin,"%d\n", &val[i]);
		max = max(val[i], max);
	}

	fprintf(fout, "%d\n", solve());
	
	fclose(fin);
	fclose(fout);
	return EXIT_SUCCESS;
}