Cod sursa(job #2805930)

Utilizator dfettiDaniel Fetti dfetti Data 22 noiembrie 2021 09:34:05
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, k;
int a[16001];

int check(int c)
{
	int aux = 0;
	int t = 1;

	for (int i = 0; i < n; ++i)
	{
		if (aux + a[i] <= c)
		{
			aux += a[i];
		}
		else
		{
			t++;
			aux = a[i];
		}
	}

	return t;
}

int main()
{
	fin >> n >> k;
	for (int i = 0; i < n; ++i)
		fin >> a[i];

	int c = 0;
	int a_max = 0;
	for (int i = 0; i < n; ++i)
	{
		c += a[i];
		if (a[i] > a_max)
			a_max = a[i];
	}

	int lo = a_max;
	int hi = c;
	int mid = 0;
	int t = 0;

	while (lo < hi)
	{
		//cout << "lo: " << lo << " hi: " << hi << endl;
		mid = (lo + hi) / 2;

		t = check(mid);
		//cout << "MID: " << mid << " t: " << t << endl;

		if (t <= k)
		{
			hi = mid;
		}
		else
		{
			lo = mid + 1;
		}
	}

	fout << lo;

	return 0;
}