Cod sursa(job #1522035)

Utilizator narcis_vsGemene Narcis - Gabriel narcis_vs Data 11 noiembrie 2015 08:48:17
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 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;
}