Cod sursa(job #3223227)

Utilizator PescarusTanislav Luca Andrei Pescarus Data 12 aprilie 2024 18:59:00
Problema Transport Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
using namespace std;
ifstream f("transport.in");
ofstream g("transport.out");
const int nmax = 16005;
int a[nmax];
int n, k;
int nrdrumuri(int c){
    int nr = 1;
    int volum = 0;
    for(int i = 1; i <= n; i++){
        if(volum += a[i] <= c){
            volum += a[i];
        }
        else {
            volum = a[i];
            nr++;
        }
    }
    return nr;
}
int main(){
    ios::sync_with_stdio(0);
    f.tie(0);
    f >> n >> k;
    int maxim = -1;
    for(int i = 1; i <= n; i++){
        f >> a[i];
        maxim = max(maxim, a[i]);
    }
    int sol = 0;
    int st = maxim, dr = 16000 * 16000;
    while(st <= dr){
        int m = (st + dr) / 2;
        if(nrdrumuri(m) <= k){
            sol = m;
            dr = m - 1;
        }
        else{
            st = m + 1;
        }
    }
    g << sol << '\n';
}