Pagini recente » Cod sursa (job #57412) | Cod sursa (job #1525346) | Cod sursa (job #2081598) | Cod sursa (job #2387500) | Cod sursa (job #2863281)
// https://infoarena.ro/problema/transport
#include <fstream>
std :: ifstream in("transport.in");
std :: ofstream out("transport.out");
#define N 16000
unsigned v[N+1], n, k, s, mx;
void read()
{
in >> n >> k;
for(unsigned i = 0; i < n; i++)
{
in >> v[i];
if (v[i] > mx)
mx = v[i];
s += v[i];
}
}
void solve()
{
unsigned lbound = mx, rbound = s;
unsigned capacity;
while(1)
{
capacity = (lbound+rbound)/2;
unsigned i = 0, j = 0;
for(i; i < k && j < n; i++)
{
unsigned load = 0;
for(j; j < n; j++)
{
if(load + v[j] <= capacity)
load+=v[j];
else
break;
}
}
// if we reached the end of the stack and performed k transports
if(i == k && j == n)
{
out<<capacity;
return;
}
// if we reached the end of the stack in less thank k transports
// ==> the capacity is too big
if(j == n) // i < k is implied
rbound = capacity;
// if we didn't reach the end of the stack in k transports
else
lbound = capacity;
}
}
int main()
{
read();
solve();
}