Pagini recente » Cod sursa (job #2138702) | Cod sursa (job #1785072) | Cod sursa (job #1787910) | Cod sursa (job #1215365) | Cod sursa (job #2863326)
// 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, min_cap = s+1;
while(lbound <= rbound)
{
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)
// save the smallest capacity found
if(min_cap > capacity)
min_cap = capacity;
// 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-1;
// if we didn't reach the end of the stack in k transports
else
lbound = capacity+1;
}
out<<min_cap;
}
int main()
{
read();
solve();
}