Gym freak TDS

TDS is a gym freak. He goes to the gym twice a day, his trainer suggested him to take some protein shake. He refuses to take the protein shake since he doubts that the protein shake may have some synthetic fibers which may damage his body, so he decides to take a diet with maximum possible protein content in it. TDS is a bit busy doing gym, help him plan his diet.

You are given N food items and two arrays containing the wt of protein ai (1<=i<=n), and the other containing the weight of the food item bi (1<=i<=n). TDS can eat  W gm. Output the maximum amount of protein he can take form the given food items.

NOTE: HE can eat any no. of food items in any order, the main motive is to maximize the protein intake without eating more than W gm of food.

Example:

Input:  n = 5, w = 10, a = [1,3,2,1,3], b = [2,4,5,7,8]
Output: 5

Approach

C++

#include <bits/stdc++.h>
using namespace std;

bool cmp(pair<intpair<intdouble>> apair<int
pair<intdouble>> b)
{
    return a.second.second > b.second.second;
}

int gymFreakTDS(int nint wint a[], int b[])
{
    vector<pair<intpair<intdouble>>> v;
    for (int i = 0i < ni++)
    {
        double x = (double)a[i] / b[i];
        v.push_back(make_pair(a[i], make_pair(b[i], x)));
    }
    sort(v.begin(), v.end(), cmp);
    int ans = 0;
    for (int i = 0i < ni++)
    {
        if (w >= v[i].second.first)
        {
            ans += v[i].first;
            w -= v[i].second.first;
        }
        else
        {
            int x = v[i].first * w;
            ans += x / v[i].second.first;
            w = 0;
            break;
        }
    }
    return ans;
}
int main()
{

    int n = 5w = 10;

    int a[n] = {13213};
    int b[n] = {24578};

    cout << gymFreakTDS(nwab<< "\n";

    return 0;
}


No comments:

Post a Comment