Pagini recente » Cod sursa (job #3039385) | Cod sursa (job #249128) | Cod sursa (job #1668654) | Cod sursa (job #128146) | Cod sursa (job #2512141)
#include <bits/stdc++.h>
using namespace std;
FILE *in, *out;
int N, K, volum_total;
deque<int> saltele;
bool verificare(int volum) {
deque<int> temp_saltele = saltele;
int nr_transporturi = 0;
while(!temp_saltele.empty()) {
++nr_transporturi;
int volum_curent = 0;
while(volum_curent < volum && !temp_saltele.empty()) {
volum_curent += temp_saltele.front();
if(temp_saltele.front() > volum)
return 1;
if(volum_curent <= volum)
temp_saltele.pop_front();
}
}
if(nr_transporturi > K)
return 1;
if(nr_transporturi <= K)
return 0;
}
int main()
{
///CITIRE
in = fopen("transport.in","r");
fscanf(in,"%d%d", &N, &K);
for(int i = 1; i <= N; ++i) {
int temp;
fscanf(in,"%d", &temp);
volum_total += temp;
saltele.push_back(temp);
}
fclose(in);
///CAUTARE BINARA IN [1, volum_total]
int st = 1, dr = volum_total, mid, sol = -1;
while(st <= dr) {
mid = (st+dr)/2;
int temp_verif = verificare(mid);
if(temp_verif == 1) {
st = mid+1;
continue;
}
else {
sol = mid;
dr = mid-1;
continue;
}
}
///AFISARE
out = fopen("transport.out","w");
fprintf(out,"%d", sol);
fclose(out);
return 0;
}