Cod sursa(job #2036187)

Utilizator dahaandreiDaha Andrei Codrin dahaandrei Data 10 octombrie 2017 14:31:56
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <queue>

using namespace std;

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

queue<int> v;

int n, k, nr;
long long int step, verificationNr, nr_formed, best, total;
const int limit = 256000000;

bool verify(int volume){
	queue <int>aux = v;
	int cnt = 0;
	long long int sum = 0;

	for (int i = 1; i <= n; ++ i){
		if (aux.front() > volume)
			return true;
		if (sum + aux.front() <= volume){
      sum += aux.front();
		}
		else{
      cnt ++;
      sum = aux.front();
		}

		aux.pop();
	}

	if (sum > 0)
    cnt ++;

	return cnt > k;
}

int main(){
	best = 256000000;

	in >> n;
	in >> k;

	for (int i = 0; i < n; ++ i){
		in >> nr;
		total += nr;
		v.push(nr);
}

	step = 1;
	step = step << 32 - __builtin_clz(total) - 1;

	while (step > 0){
    verificationNr = nr_formed + step;

    if (verificationNr <= total && verify(verificationNr)){
    	nr_formed += step;
      best = verificationNr;
    }

    step = step >> 1;
	}

	best += 1;
	out << best;

	return 0;
}