I stuck at a problem Minion chef and Bananas .Which is a binary search based problem
I execute the below program on following test cases; And it successfully passed all the test cases
Test case 1:
3 3
1 2 3
Output:
3
Test case 2:
3 4
1 2 3
Output:
2
Test case 3:
4 5
4 3 2 7
Output:
4
But I am when I am submitting the Below program I am getting the error as "SIGTSTP" Please help.
Here is my code:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int getHours(vector<int> piles,int k){
int sum = 0;
for(int i = 0;i<piles.size();i++){
if(piles[i]%k==0)
sum+=(piles[i]/k);
else
sum+=(1+piles[i]/k);
}
return sum;
}
int main(){
int t;
cin>>t;
while(t--){
int n;
int h;
cin>>n>>h;
vector <int> piles;
int tempmax = 0;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
tempmax = max(temp,tempmax);
piles.push_back(temp);
}
int low = 0;
int high = tempmax;
if(tempmax==1){
cout<<h<<endl;
continue;
}
while(low<high){
int mid = (low + high)/2;
int p=getHours(piles,mid);
if(p<=h)
high = mid;
else
low = mid + 1;
}
cout<<low<<endl;
piles.clear();
}
}