Cod sursa(job #1312898)

Utilizator Vladut-Vlad Panait Vladut- Data 10 ianuarie 2015 02:34:12
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>

using namespace std;

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

int n, k, c, a[16005], i, nr;
int high = 256000005;

bool verifica (int m)
{
    int suma=0, t=0, j;
    for (j=1; j<=n; j++)
    {
        if (a[j] > m)
            return 0;
        if (a[j]+suma<=m)
            suma=suma+a[j];
            else
            {
                t++;
                j--;
                suma=0;
            }
    }
    if (t<k)
        return 1;
    return 0;
}

void cautBin (int left, int right)
{
    while (left <= right)
    {
        int m=(left+right)>>1;
        if (verifica(m))
        {
            if (high>m)
                high=m;
            right = m-1;

        }
        else left = m+1;
    }
    if (left < high && verifica(left))
        high=left;
}

int main()
{
    fin >> n >> k;
    for (i=1; i<=n; i++)
        fin >> a[i];
    cautBin (0, high);
    fout << high;
    return 0;
}