Cod sursa(job #2707044)

Utilizator Mihai180315Mihai Smarandache Mihai180315 Data 16 februarie 2021 13:30:59
Problema Transport Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;
const int nmax = 16000;

int v[nmax];
int camion[nmax];


bool valid(int x, int n, int k)
{
	int j = 1;
	memset(camion, 0, sizeof(camion));
	for (int i = 1; i <= n; ++i) {
		camion[j] += v[i];
		if (camion[j] > x) {
			++j;
		}
		if (j > k) {
			return 0;
		}
	}
	return 1;
}

int get_max(int n)
{
	int maxim = 0;
	for (int i = 1; i <= n; ++i) {
		if (v[i] > maxim) {
			maxim = v[i];
		}
	}
	return maxim;
}

int solve(int n, int k)
{
	int l = 1, r = 2560000, last = -1;
	int maxim;
	maxim = get_max(n);
	while (l <= r) {
		int mid = (l + r) / 2;
		if (valid(mid, n, k) == 1 && mid > maxim) {
			last = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	return last;
}


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

	int n, k;
	fin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		fin >> v[i];
	}
	fout << solve(n, k);

}