Pagini recente » Cod sursa (job #2417939) | Cod sursa (job #1564733) | Cod sursa (job #3040759) | Cod sursa (job #2632867) | Cod sursa (job #1525372)
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ifstream f("transport.in");
int n,k;
f>>n>>k;
int v[16000];
int upper_bound;
for(int i=0;i<n;i++){
f>>v[i];
upper_bound=max(upper_bound,v[i]);
}
int solution;
int lower_bound=upper_bound;
upper_bound*=n/k;
while(lower_bound<=upper_bound){
int i;
int sum=(lower_bound+upper_bound)/2;
int nrtr=k;
for(i=0;i<n;i++)
if(sum-v[i]<0 && nrtr>0){
sum=(lower_bound+upper_bound)/2;
nrtr--;
sum-=v[i];
}
else if(sum-v[i]<=0 && nrtr==0){
lower_bound= (lower_bound+upper_bound)/2+1;
break;
}
else if(sum-v[i]>=0) sum-=v[i];
if(i==n && nrtr>0 && sum>=0)
solution=(lower_bound+upper_bound)/2;
if(i==n && (nrtr!=0 || sum!=0))
upper_bound=(lower_bound+upper_bound)/2-1;
}
cout<<solution;
system("pause");
return 0;
}