Pagini recente » Cod sursa (job #3153745) | Cod sursa (job #8435) | Cod sursa (job #3202563) | Cod sursa (job #3178425) | Cod sursa (job #3121533)
#include <fstream>
using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");
const int N_MAX = 16e3;
const int K_MAX = 16e3;
int v[N_MAX];
int nr_transporturi(int v[], int n, int carry) {
int transporturi = 1, sum = 0;
for(int i = 0; i < n; ++i)
if(sum + v[i] <= carry)
sum += v[i];
else{
transporturi++;
sum = v[i];
}
return transporturi;
}
int main() {
int n, k, sum = 0;
fin >> n >> k >> v[0];
int mx = v[0];
for(int i = 1; i < n; ++i){
fin >> v[i];
if(mx < v[i]) mx = v[i];
sum += v[i];
}
//cautam binar cea mai mare capacitate cu care se realizeaza mai mult de k transporturi
int b = mx - 1, e = sum, mid;//(b;e]
while(e - b > 1){
mid = (b + e) >> 1;
if(nr_transporturi(v, n, mid) > k)
b = mid;
else
e = mid;
}
fout << e << '\n';
fin.close();
fout.close();
return 0;
}