Cod sursa(job #1002984)

Utilizator narcis_vsGemene Narcis - Gabriel narcis_vs Data 29 septembrie 2013 14:58:51
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>

#define In "transport.in"
#define Out "transport.out"
#define MAX 2000000000
#define Nmax 16004

using namespace std;
int  n, k, sol, a[Nmax], _max;

inline void Read()
{
    ifstream f(In);
    f >> n >> k;
    for(int i = 1;i <= n; ++i)
    {
        f >> a[i];
        _max = max(_max,a[i]);
    }
    f.close();
}

inline bool Verif(const int capacity)
{
    int s,i,cnt;
    for(s = 0,i = cnt = 1; i <= n && cnt <= k ;++i)
    {
        s += a[i];
        if(s>capacity)
        {
            s = a[i];
            ++cnt;
        }
    }
    return cnt <= k;
}

inline void Binary_search()
{
    int Left = _max,Right = MAX, Middle;
    while(Left <= Right)
    {
        Middle = (Left+Right)>>1;
        if(Verif(Middle))
        {
            sol = Middle;
            Right = Middle - 1;
        }
        else
            Left = Middle + 1;
    }
}

inline void Write()
{
    ofstream g(Out);
    g<<sol<<"\n";
    g.close();
}

int main()
{
    Read();
    Binary_search();
    Write();
    return 0;
}