Cod sursa(job #3121525)

Utilizator daristyleBejan Darius-Ramon daristyle Data 13 aprilie 2023 18:11:38
Problema Transport Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>

using namespace std;

ifstream fin("transport.in");
ofstream fout("transport.out");

const int N_MAX = 16e3;
const int K_MAX = 16e3;
int sp[N_MAX];

int nr_transporturi(int sp[], int n, int carry) {
	int transporturi = 1, ant = 0;
	for(int i = 0; i < n; ++i){
		if(sp[i] - ant > carry){
			transporturi++;
			ant = sp[i];
		}
		if(sp[i] - sp[i - 1] > carry)
			transporturi = K_MAX + 1;
	}

	return transporturi;
}

int main() {
	int n, k;
	fin >> n >> k >> sp[0];
	for(int i = 1; i < n; ++i){
		fin >> sp[i];
		sp[i] += sp[i - 1];
	}

	//cautam binar cea mai mare capacitate cu care se realizeaza mai mult de k transporturi
	int b = 0, e = sp[n - 1], mid;//(b;e]
	while(e - b > 1){
		mid = (b + e) >> 1;
		if(nr_transporturi(sp, n, mid) > k)
			b = mid;
		else
			e = mid;
	}

	fout << e + 1 << '\n';

	fin.close();
	fout.close();
	return 0;
}