Cod sursa(job #3121513)

Utilizator daristyleBejan Darius-Ramon daristyle Data 13 aprilie 2023 16:14:15
Problema Transport Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>

using namespace std;

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

const int N_MAX = 16e3;
const int VAL_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];
		}

	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];
	}

	int b = 1, e = N_MAX * VAL_MAX + 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 << b - 1 << '\n';

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