Searching 60

QUESTION

The hero of this story is a toddler named BooBoo. Inspired by the legendary competitive coder Gena, BooBoo has also started preparing to race to the top of the ranks.\n\nBooBoo is going to practice \nN\nN different problems in the exact given order over the next \nM\nM days. For each problem, he writes down the amount of time \nq\ni\nqi he will take to think and code the \ni\nt\nh\nith problem (He is quite good at estimating!). Before starting on the problems, he took advice from experienced competitive programmers on his practice routine and almost all of them advised him to keep his daily load at the minimum possible and avoid over training.\n\nSince BooBoo already has \nN\nN problems to solve, he asks you to find the minimum time \nT\nT such that training everyday for a time \nt\ni\n\nT\ntiT is sufficient to solve all the \nN\nN problems in \nM\nM days.\n\nNote : Unlike in real world, you cannot think on a problem on one day and solve it on the other day. You need to do it on the very same day!\n\nInput Format:\n\nThe first line contains two space separated integers \nN\nN and \nM\nM. The next line contains \nN\nN space separated integers denoting the time \nq\ni\nqi required to solve the \ni\nt\nh\nith problem.\n\nOutput Format:\n\nThe output consists of one integer, the minimum time \nT\nT as described in the problem statement.

“TESTCASE_1”: “5 3\n1 2 2 1 3\n###—###SEPERATOR—###—\n3”, “TESTCASE_2”: “10 2\n5 8 2 9 20 54 1 7 4 5 \n###—###SEPERATOR—###—\n71”, “TESTCASE_3”: “7 4\n7 3 1 9 3 6 5 \n###—###SEPERATOR—###—\n10”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<stdio.h>
 
int main(){
	int i,n,m;
	long long int sum=0,a[1000005];
	scanf("%d %d",&n,&m);
	long long int max=-1;
	for(i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum+=a[i];
		if(a[i]>max)
		max=a[i];
	}
	
	long long int l,r,mid;
	int j,k;
	l=max;
	r=sum;
    int flag=0;
    long long int pmid;
	long long int psum;
    while(l<=r){
    	mid=(l+r)/2;
    	j=0;
    	flag=0;
		k=0;
    	while(k!=m){
    		psum=0;
    		while(psum<=mid && j<n)
			{
    			psum+=a[j];
    			j=j+1;
    			
			}
			if(psum>mid)
				j=j-1;
				k=k+1;
			
		}
		if(j<n)
		flag=-1;
		if(flag==-1)
		l=mid+1;
		else
		r=mid;
		if(pmid==mid)
		break;
		pmid=mid;
	}
	printf("%lld\n",mid);
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro