Cod sursa(job #659021)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 9 ianuarie 2012 22:03:00
Problema Transport Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <cstdio>

using namespace std;

int n, k;
int v[16001];

int binary_search(int left, int right);

bool verif(int drum);

void citire()
{
    freopen("transport.in", "r", stdin);
    scanf("%d %d\n", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d\n", &v[i]);
    }
    fclose(stdin);
}

int maxVector()
{
    int max = 0;
    for(int i = 1; i <= n; i++)
    {
        if(v[i] > max)
            max = v[i];
    }
    return max;
}

int getSume()
{
    int s = 0;
    for(int i = 1; i <= n; i++)
    {
        s += v[i];
    }
    return s;
}

int rezolvare()
{
    int from = maxVector();
    int to = getSume();
    int mid;
    while(from < to)
    {
        mid = binary_search(from, to);
        if(verif(mid))
        {
            if(to != mid)
                to = mid;
            else
                return to;
        }
        else
        {
            if(from != mid)
                from = mid;
            else
                return to;
        }
    }
    return to;
}

bool verif(int drum)
{
    int drumActual = 1;
    int location = 1;
    int sumaPartial;
    while(drumActual <= k && location <= n)
    {
        sumaPartial = 0;
        while(sumaPartial + v[location] <= drum && location <= n)
        {
            sumaPartial += v[location];
            location++;
        }
        drumActual++;
    }
    if(--location == n)
        return true;
    return false;
}

int binary_search(int left, int right)
{
    return (left + right + 1) / 2;
}

void afisare()
{
    freopen("transport.out", "w", stdout);
    printf("%d", rezolvare());
    fclose(stdout);
}

int main()
{
    citire();
    afisare();
    return 0;
}