Pagini recente » Cod sursa (job #2057291) | Cod sursa (job #1730629) | Cod sursa (job #2641543) | Cod sursa (job #3215966) | Cod sursa (job #2481864)
#include <iostream>
#include <algorithm>
#include <fstream>
ifstream fin("transport.in");
ofstream fout("transport.out");
using namespace std;
int const N = 16005;
int const inf = 16005;
int NrTransporturi(int NrSaltele, int v[], int CapacitateCamion)
{
int sol = 1;
int CantitateTransportCurent = 0;
for (int i = 0; i < NrSaltele; i++)
{
if (v[i] > CapacitateCamion)
{
return inf;
}
if (CantitateTransportCurent + v[i] <= CapacitateCamion)
{
CantitateTransportCurent += v[i];
}
else
{
CantitateTransportCurent = v[i];
sol++;
}
}
return sol;
}
int bs(int NrSaltele, int v[], int NrTransporturiDisponibile)
{
int sol = 0;
int pas = 1 << 30;
while (pas > 0)
{
if (NrTransporturi(NrSaltele, v, sol + pas) > NrTransporturiDisponibile)
{
sol += pas;
}
pas /= 2;
}
return sol + 1;
}
int main()
{
int NrSaltele, NrTranporturiDisponibile, v[N];
fin >> NrSaltele >> NrTranporturiDisponibile;
for (int i = 0; i < NrSaltele; i++)
{
fin >> v[i];
}
fout << bs(NrSaltele, v, NrTranporturiDisponibile);
}