< Back to forum

MINEAT March Challenge

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.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar

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.

Shubham_Kumar_Gupta last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.