question link: https://www.codechef.com/MARCH18B/problems/MINEAT
my code is not giving the correct outputs, bt i didn't find any logical error. Please tell me where should i m make the corrections ?
My Code:
#include<bits/stdc++.h> using namespace std; int main() { int t; long long int n,h,arr[100005],i,k,sum=0,mid; cin>>t; while(t--) { cin>>n>>h; sum=0; for(i=0;i<n;i++) {cin>>arr[i]; sum+=arr[i];} int r=sizeof(arr)/sizeof(arr[0]); sort(arr,arr+r); if(h==n) k=arr[n-1]; else { mid=sum/h; for(i=0;i<n;i++) { if(arr[i]<=mid) sum-=arr[i]; else break; } while(1) { if((h-i)*mid>=sum) break; else mid+=1; } k=mid; } cout<<k<<"\n"; } return 0; }
Asked by: Samrat_De on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.
Firstly you are declaring a fixed sized array arr with 100005 elements and then sorting using sort funtion in STL.
int r=sizeof(arr)/sizeof(arr[0]);
sort(arr,arr+r);
Suppose the value of n for a particular testcase is 10,but the above code sorts all 100005 elements so the resultant array contains garbage values.
first parameter to sort function must be the starting address of the array and second parameter ,the end address upto which sorting is to be performed on the array.
sort(arr,arr+n);
Before using any STL function google and read about it don't just blindly copy code without understanding.
Coming to the Binary search implementation of yours ,i don't think you are clear with the concept.
read this article first : https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/
you can view my solution here: https://www.codechef.com/viewsolution/17845125
I have added comments for better understanding.