Cod sursa(job #3299518)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 7 iunie 2025 17:41:52
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <fstream>

using namespace std;

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

const int nmax = 16000;
int n, k, a[nmax + 2], sum, maxi;

int check(int capacity){
    int noww = 0, paths = 1;
    for(int i = 1; i <= n; i++){
        if(noww + a[i] <= capacity){
            noww += a[i];
        }else{ paths++; noww = a[i]; }
    }
    return paths;
}

int main(){

    in>>n>>k;
    for(int i = 1; i <= n; i++){
        in>>a[i], sum += a[i];
        maxi = max(maxi, a[i]);
    }

    int st = maxi, dr = sum + 1, mij, pos = -1;
    for(; st <= dr; ){
        mij = (st + dr) >> 1;
        if(check(mij) <= k){
            dr = mij - 1; pos = mij;
        }else{ st = mij + 1; }
    }
    out<<pos<<"\n";

    return 0;
}