Pagini recente » Cod sursa (job #2560851) | Cod sursa (job #2559175) | Cod sursa (job #460284) | Cod sursa (job #253900) | Cod sursa (job #3121515)
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int howMany(int cost,vector<int> g)
{
int boats=0,sum=0;
vector<int>::iterator it;
for(it=g.begin();it!=g.end();it++)
{
if(sum+*it>cost)
{
boats++;
sum=0;
}
sum+=*it;
}
return ++boats;
}
long long binaryS(vector<int> g,int ferriesCnt, long long uB, long long lB)
{
long long minC=0;
while(lB<uB)
{
long long mid=(uB+lB);
if(mid%2==0)
mid/=2;
else mid/=2,mid++;
if(howMany(mid,g)<=ferriesCnt)
minC=uB=mid;
else lB=mid+1;
}
return minC;
}
int main()
{
vector<int> g;
int n,k;
long long upperBound=0,lowerBound=0;
ifstream fin("date.in");
fin>>n>>k;
int x;
for(int i=0;i<n;i++)
{
fin>>x,g.push_back(x);
upperBound+=x;
if(lowerBound>x)
lowerBound=x;
}
cout<<binaryS(g,k,upperBound,lowerBound)<<endl;
return 0;
}