Cod sursa(job #1789712)

Utilizator Vally77FMI Calinescu Valentin Gelu Vally77 Data 27 octombrie 2016 14:26:29
Problema Transport Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
//#include <iostream>
#include <fstream>
using namespace std;
ifstream ka("transport.in");
ofstream ki("transport.out");

const int N_MAX = 16000;

int n, k, v[N_MAX + 1];

int binara()
{
	int inc = 1, sf = N_MAX, mij;
	while(inc < sf)
	{
		mij = (inc + sf) / 2;		
		int trans = 0, curent = 0;		
		for(int i = 1; i <= n; i++)
		{
			while(i <= n && curent + v[i] <= mij)
			{
				curent += v[i];
				i++;
			}
			trans++;
			if(trans > k)
				break;
			i--;
			curent = 0;
		}
        //cout << mij << " " << trans << '\n';
		if(trans <= k)
			sf = mij;
		else
			inc = mij + 1;
	}
	return inc;
}

int main()
{
	ka >> n >> k;
    for(int i = 1; i <= n; i++)
       ka >> v[i];
	ki << binara();
}