Pagini recente » Cod sursa (job #1227942) | Monitorul de evaluare | Cod sursa (job #2247416) | Cod sursa (job #1027409) | Cod sursa (job #2863431)
// 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 = lbound, min_cap = s+1;
while(lbound <= rbound)
{
unsigned i = 0, j = 0;
for(;i < k && j < n; i++)
{
unsigned load = 0;
for(;j < n;)
{
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) // i < k is implied
rbound = capacity-1;
// if we didn't reach the end of the stack in k transports
else
lbound = capacity+1;
capacity = (lbound+rbound)/2;
}
out<<min_cap;
}
int main()
{
read();
solve();
}