Cod sursa(job #2904943)

Utilizator cameleonGeorgescu Dan cameleon Data 18 mai 2022 18:40:55
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include<fstream>
#define NMAX 16005
using namespace std;

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

int v[NMAX], c, nrt, s, Vmax, n, tr, s1;

int transporturi(int cap) //calculez nr de transp daca am camioane cu capacitatea cap
{
    int s1 = v[1], nt = 1;
    for(int i = 2; i <= n; i++)
        if(s1 + v[i] <= cap)
            s1 += v[i];
        else
        {
            nt++;
            s1 = v[i];
        }
    return nt;
}
int main()
{
    fin >> n >> nrt;
    for(int i=1;i<=n;i++)
        {
            fin >> v[i];
            if(v[i] > Vmax)
                Vmax = v[i];
            s += v[i];
        }
    //incerc valorile posibile pentru capacitate
    //caut binar o solutie pt probblema
    //cu cat capacitate camionului este mai mare, cu atat nr de transporturi este mai mic
   int st = Vmax, dr = s;
   while(st <= dr)
    {
        int mij =(st+dr)/2;
        //cate transporturi se fac folosind capacitatea mij
        tr = transporturi(mij);
        if(tr > nrt)
            st = mij+1;
        else
        {
             c = mij;
             dr = mij-1;
        }
    }

    fout << c;


}