Pagini recente » Cod sursa (job #28432) | Cod sursa (job #6749) | Cod sursa (job #704946) | Cod sursa (job #1057104) | Cod sursa (job #2904943)
#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;
}