Cod sursa(job #3121533)

Utilizator daristyleBejan Darius-Ramon daristyle Data 13 aprilie 2023 18:33:41
Problema Transport Scor 100
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 v[N_MAX];

int nr_transporturi(int v[], int n, int carry) {
	int transporturi = 1, sum = 0;
	for(int i = 0; i < n; ++i)
		if(sum + v[i] <= carry)
			sum += v[i];
		else{
			transporturi++;
			sum = v[i];
		}

	return transporturi;
}

int main() {
	int n, k, sum = 0;
	fin >> n >> k >> v[0];
	int mx = v[0];
	for(int i = 1; i < n; ++i){
		fin >> v[i];
		if(mx < v[i]) mx = v[i];
		sum += v[i];
	}

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

	fout << e << '\n';

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