Cod sursa(job #2890438)

Utilizator matthriscuMatt . matthriscu Data 15 aprilie 2022 16:22:37
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>
using namespace std;

int count_transports(const int capacity, const vector<int> &v)
{
	int ans = 1, current = 0;
	for (const int &x : v)
		if (current + x <= capacity)
			current += x; 
		else {
			current = x;
			++ans;
		}
	return ans;
}

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

	int n, k;
	fin >> n >> k;
	vector<int> v(n);
	for (int &x : v)
		fin >> x;
	
	int step, i, mn = *max_element(v.begin(), v.end()), mx = accumulate(v.begin(), v.end(), 0);
	for (step = 1; step <= mx; step <<= 1);
	for (i = mx; step; step >>= 1)
		if (i - step >= mn && count_transports(i - step, v) <= k)
			i -= step;

	fout << i << '\n';
}