Pagini recente » Cod sursa (job #2435190) | Cod sursa (job #1235137) | Cod sursa (job #695058) | Cod sursa (job #538075) | Cod sursa (job #1049846)
#include <fstream>
using namespace std;
int main()
{
ifstream fin ("transport.in");
ofstream fout ("transport.out");
int n,k,end=0,start=0,count,p=0,temp,mid=0;
fin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
fin>>a[i];
end+=a[i];
if(start<a[i]) start=a[i];
}
while(1)
{
mid=(start+end)/2;
temp=mid;
count=0;
for(int i=0;i<n;i++)
{
temp-=a[i];
if(temp<0)
{
temp=mid-a[i];
count++;
}
else if(temp==0)
{
temp=mid;
count++;
}
if(temp>=0&&i==n-1) count++;
}
if(count>k)
{
start=mid+1;
}
else if(count==k)
{
p=mid;
end=mid;
}
else if(count<k)
{
end=mid;
}
if(start>=end) break;
}
fout<<p;
}