Pagini recente » Cod sursa (job #980424) | Cod sursa (job #926548) | Cod sursa (job #1929902) | Cod sursa (job #463198) | Cod sursa (job #2286182)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
fstream fin("transport.in");
ofstream fout("transport.out");
int transport( int c, int k, const vector<int>& v ){
int ct = 0, nt = 0;
for( size_t idx = 0; idx < v.size(); ){
if( v[idx] > c )
return 1;
if( ct + v[idx] == c ){
if( nt == k ){
if( (idx + 1) == (idx < v.size()) )
return 0;
}
nt++;
ct = 0;
idx++;
}
else if( ct + v[idx] > c ){
nt++;
ct = 0;
}
else {
ct = ct + v[idx];
idx++;
}
if( nt > k )
return 1;
}
if( ( ct > 0 ) && ( nt == (k-1) ) )
return 0;
return -1;
}
int main()
{
vector<int> v;
int n = 0, k = 0, s = 0, maxx = 0;
fin >> n >> k;
for( int i = 0; i < n; i++ ){
int aux;
fin >> aux;
v.push_back(aux);
s = s + aux;
if( aux > maxx)
maxx = aux;
}
int stg = maxx , drp = s, mij = 0;
bool notFound = true;
while( ( stg <= drp ) && notFound ) {
mij = (stg + drp) / 2;
int t = transport( mij, k, v );
if( t == 0 )
notFound = false;
else{
if( t > 0 ){
stg = mij+1;
}
else {
drp = mij-1;
}
}
}
if( notFound )
fout << -1;
else
fout << mij;
return 0;
}