MKUHAR - Most Servings Meal - SPOJ Solution C++

  Problem Link : MKUHAR 


👉 Hint : edit please

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
#define ll long long int

pair<int,int> ingr[101];
pair<int,int> small[101];
pair<int,int> large[100];

bool f(ll serve,ll n,ll m)
{
	ll value=0;
	for(int i=1;i<=n;i++)
	{
		ll req=(ingr[i].first*serve)-ingr[i].second;
		ll cost=0;
		while(req>0)
		{
			if(ceil((double)req/small[i].first)*small[i].second < ceil((double)req/large[i].first)*large[i].second)
			{
			    req-=small[i].first;
				cost+=small[i].second;
				value+=small[i].second;
			}
			else
			{
			    req-=large[i].first;
			    cost+=large[i].second;
			    value+=large[i].second;
			}
		}
		if(value>m)
		{
		    if(serve==5)
		        cout<<value;
		        
		
			return false;
		}
	}
	return true;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>ingr[i].first>>ingr[i].second>>small[i].first>>small[i].second>>large[i].first>>large[i].second;
	ll low=0;
	ll high=m;
	ll mid;
	while(low<high)
	{
	  //  cout<<low<<" "<<high<<endl;
		mid=low+(high-low+1)/2;
		if(f(mid,n,m))
			low=mid;
		else
			high=mid-1;
	}
	cout<<high<<endl;

}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!