Cod sursa(job #2904945)

Utilizator Otilia2022Bianca Nicolae Otilia2022 Data 18 mai 2022 18:48:35
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <iostream>
#include <fstream>
#define NMAX 16005

using namespace std;

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

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

int transporturi(int 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 val posibile pt capacitate
    /// caut binar o solutie pt problema
    ///cu cat capacitatea 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;
    return 0;
}