Pagini recente » Cod sursa (job #68477) | Cod sursa (job #2952539) | Cod sursa (job #296237) | Cod sursa (job #1730000) | Cod sursa (job #1007037)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin ("transport.in");
ifstream fout("transport.out");
int n,k;
int minV = -2,maxV = 0;
queue<int> s;
void scan(){
fin >> n >> k;
for(int i =0 ; i < n ; i++){
int x;
fin >> x;
if(x > minV)
minV = x;
maxV += x;
s.push(x);
}
}
bool isValueOk(int value){
int currentCap =0;
int currentK = k;
queue<int> auxS = s;
int currentItem;
while(!auxS.empty()){
if (currentK == 0)
break;
currentItem = auxS.front();
auxS.pop();
if(currentCap + currentItem <= value)
currentCap+= currentItem;
else
{
s.push(currentItem);
currentK --;
currentCap = 0;
}
}
if(auxS.empty() == true){
return true;
}
return false;
}
int rightValue;
int binarySearch(int min,int max){
if(min == max)
return min;
int val = (min+max) / 2;
if(isValueOk(val) == true){
rightValue = val;
return binarySearch(min,rightValue);
}
else
{
return binarySearch(val+1,max);
}
}
int main(){
scan();
fout << binarySearch(minV,maxV) << endl;
return 0;
}