Cod sursa(job #2625196)

Utilizator marian222200Dimofte Marian marian222200 Data 5 iunie 2020 19:48:24
Problema Transport Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
// tema_sd_mare.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <fstream>
#include <algorithm>
#define N 16000
using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");
int n, k, saltele[N];
void citire() {
	fin >> n >> k;
	for (int i = 0; i < n; i++)fin >> saltele[i];
}
bool verif(int x) {
	int nr_transp = 0, rest = 0, i;
	for (i = 0; i < n; i++) {
		if (rest < saltele[i]) {
			nr_transp++;
			rest = x;
		}
		rest -= saltele[i];
	}
	return (nr_transp <= k);
}
long long cb(long long st, long long dr) {
	while (st < dr) {
		long long m = (st + dr) / 2;
		if (verif(m))dr = m-1;
		else st = m;
	}
	return st+1;
}
int maxim() {
	int maxi = -1, i;
	for (i = 0; i < n; i++)
		if (maxi < saltele[i])maxi = saltele[i];
	return maxi;
}
int main() {
	citire();
	fout << cb(1, 16000* 16000);
	return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file