Cod sursa(job #2251411)

Utilizator DordeDorde Matei Dorde Data 1 octombrie 2018 16:20:04
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <fstream>

using namespace std;
ifstream f ("transport.in");
ofstream g ("transport.out");
int const NM = 16007;
int v [NM] , n , k;
inline bool check (int nr){
    int S = 0 , step = 1;
    for(int i = 1 ; i <= n ; ++ i){
        if (v [i] > nr)
            return false;
        if (S + v [i] > nr)
        {
            ++ step;
            S = 0;
        }
        S += v [i];

    }
    if (step <= k)
        return true;
    return false;
}

inline int bsearch (){
    int pas = (1 << 27) , found = 0;
    while (pas){
        if (! check (pas + found))
            found += pas;
        pas /= 2;
    }
    return found + 1;
}
int main()
{
    int i;
    f >> n >> k;
    for(i = 1 ; i <= n ; ++ i)
        f >> v [i];
    g << bsearch ();
    return 0;
}