Cod sursa(job #2446796)

Utilizator uvIanisUrsu Ianis Vlad uvIanis Data 10 august 2019 18:51:20
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <iostream>
std::ifstream fin{"transport.in"};
std::ofstream fout{"transport.out"};

int N, K, v[16001];

bool test(int capacity){
    int counter{1}, current_capacity{capacity};

    for(int i = 1; i <= N; i++){
        if(current_capacity < v[i]){
            current_capacity = capacity;
            counter++;
            if(counter > K) return false;
        }

        current_capacity -= v[i];
    }

    return true;
}

int binarySearch(int first, int last){
    int capacity, step;

    for(step = 1; step < last; step = (step << 1));

    for(capacity = last; step; step = (step >> 1))
        if(capacity - step >= first)
            if(test(capacity - step)) capacity -= step;


    return capacity;

}

int main()
{
    fin >> N >> K;

    int max{1}, sum{0};

    for(int i = 1; i <= N; i++){
        fin >> v[i];
        sum += v[i];
        max = std::max(max, v[i]);
    }

    fout << binarySearch(max, sum);
}