Cod sursa(job #3368027)

Utilizator seful_stefanstefan andrei seful_stefan Data 12 octombrie 2026 21:25:27
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>

using namespace std;

bool verifica(int capacitate, int numar_transporturi_maxime, const vector<int>& volume) {
    int transporturi_folosite = 1;
    int volum_curent = 0;

    for (int volum : volume) {
        if (volum > capacitate) {
            return false;
        }
        if (volum_curent + volum <= capacitate) {
            volum_curent += volum;
        } else {
            transporturi_folosite++;
            volum_curent = volum;
        }
    }

    return transporturi_folosite <= numar_transporturi_maxime;
}

int main() {
    ifstream fin("transport.in");
    ofstream fout("transport.out");

    int N, K;
    if (!(fin >> N >> K)) return 0;

    vector<int> volume(N);
    int volum_maxim = 0;
    int suma_volume = 0;

    for (int i = 0; i < N; ++i) {
        fin >> volume[i];
        if (volume[i] > volum_maxim) {
            volum_maxim = volume[i];
        }
        suma_volume += volume[i];
    }

    int stanga = volum_maxim, dreapta = suma_volume;
    int capacitate_minima = dreapta;

    while (stanga <= dreapta) {
        int mijloc = stanga + (dreapta - stanga) / 2;
        if (verifica(mijloc, K, volume)) {
            capacitate_minima = mijloc;
            dreapta = mijloc - 1;
        } else {
            stanga = mijloc + 1;
        }
    }

    fout << capacitate_minima;
    return 0;
}