Cod sursa(job #529744)

Utilizator ariel_roAriel Chelsau ariel_ro Data 5 februarie 2011 21:05:22
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

#define NX 16005

int N, K, a[NX], min_vol = 0, max_vol = 0;
FILE *in = fopen("transport.in", "r");
FILE *out = fopen("transport.out", "w");

void cit()
{
    fscanf(in, "%d %d", &N, &K);
    for (int i = 0; i < N; i++)
    {
        fscanf(in, "%d", &a[i]);
        if (a[i] > min_vol)
            min_vol = a[i];
        max_vol += a[i];
    }
}

int verif(int c)
{
    int i = 0, tran = 0, cc = c;
    while (i < N)
    {
        while (c >= a[i] && i < N)
        {
            c -= a[i];
            i++;
        }

        c = cc;
        tran++;
    }

    if (tran > K)
        return -1;
    return 1;
}

void rez()
{
    int p = min_vol, u = max_vol, m = 0, sol = NX * NX;
    while (p <= u)
    {
        m = (p + u) >> 1;
        if (verif(m) > 0) // capacitate mai mare
        {
            if (m < sol)
                sol = m;
            u = m - 1;
        }
        else
            p = m + 1;
    }
    fprintf(out, "%d", sol);
}

int main()
{
    cit();
    rez();
    return 0;
}