Buy in minimal price – GeeksforGeeks

[ad_1]

An individual has an inventory of N gadgets to purchase. The price of the ith merchandise is represented by Pi. He additionally has M coupons. Every coupon can be utilized to buy an merchandise from the listing whose price is a minimum of Li, with a reduction of Di. Every coupon can solely be used as soon as, and a number of coupons can’t be used for a similar merchandise. Discover the minimal complete price required to buy all N gadgets from the listing, contemplating the accessible coupons.

Examples:

Enter: N = 3, M = 3, P[3] = {4, 3, 1}, L[3] = {4, 4, 2}, D[3] = {2, 3, 1}
Output: 4
Rationalization: Think about using the 2nd coupon for the 1st merchandise, and the third coupon for the 2nd merchandise. Then, he buys the 1st merchandise for 4-3 = 1, the 2nd merchandise for 3-1 = 2, and the third merchandise for 1. Thus, he should buy all of the gadgets for 1 + 2 + 1 = 4

Enter: N = 10, M = 5, P[10] = {9, 7, 1, 5, 2, 2, 5, 5, 7, 6}, L[3] = {7, 2, 7, 8, 2}, D[3] = {3, 2, 4, 1, 2}
Output: 37

Method: To resolve the issue observe the under observations:

This downside will be solved utilizing grasping method. Right here the grasping technique as follows.

  • Coupons are checked out in descending order of low cost value. If there are merchandise for which coupons can be utilized, the coupon is used for the most affordable product amongst them.

Hereafter, the answer obtained by making use of this technique is known as the optimum answer .

There are two attainable instances of non-optimal options:

  • He didn’t use a coupon that ought to have been accessible.
  • He used a coupon that ought to have been accessible, however he used the coupon on a non-cheapest merchandise.

For these two instances, It’s proven that it doesn’t harm to interchange the optimum answer. Within the following, the coupon with the most important low cost value doesn’t obey the grasping technique, and all different coupons obey the grasping technique. First, take into account the previous case. Then, within the optimum answer c0 The product that was supposed to make use of m, or for larger priced gadgets, the coupons truly used are listed in descending order of the value of the used merchandise. c1​,c2​,…,cokay​will do.

At this level, the next will be mentioned.

  • mTo c1​ If just isn’t used: mTo c1 ​Through the use of , it’s attainable to extend the variety of coupons that can be utilized whereas sustaining the utilization standing of different coupons.
  • m to c1 ​is used: m to c1 ​not, c0 ​use . By doing this, c1 ​tooth m You should utilize it for the following most cost-effective merchandise as a substitute. Nevertheless, on this case,cokay ​can not be utilized to any product. however, c0 ​The discounted value of cokay​Since it’s bigger than the discounted value of , there isn’t any general loss. Subsequent, take into account the latter case. Then, within the optimum answer c0. ​The product that was supposed to make use of m0​, the product truly used m1 ​will do. At this level, the next will be mentioned.
  • m0​ If no coupon has been used for: c0 ​is used, m1 ​not m0 ​needs to be modified to In consequence, the utilization standing of the coupon doesn’t change, and the choices for utilizing different coupons are usually not narrowed.

From the above, it was proven that in each the previous case and the latter case, there isn’t any loss by changing a non-optimal answer with an optimum answer.

  • m0 ​If the coupon is used on: m0 ​and m1 ​It’s ample to alternate the vacation spot of the coupon for m1 ​the value of m0 ​Because the method can be excessive, m0 ​The coupon used for m1 ​will also be used for Due to this fact, this alternate is all the time attainable and there’s no loss by this alternate.

Under are the steps concerned within the implementation of the code:

  • Initialization:
    • Initialize a variable ans to maintain observe of the reply.
    • Create a multiset named ms to retailer the weather of array P.
  • Inserting parts and calculating the preliminary sum:
    • Iterate from 0 to N-1 and insert every ingredient of array P into the multiset ms.
    • Additionally, add every ingredient of array P to the variable ans.
  • Making a vector of pairs:
    • Create a vector of pairs named p with dimension M.
    • For every index, i from 0 to M-1, set the primary ingredient of p[i] as D[i] and the second ingredient as L[I].
    • This creates pairs of parts the place the primary ingredient represents the down worth and the second ingredient represents the decrease worth.
  • Sorting the vector of pairs:
    • Type the vector of pairs p in descending order. The sorting relies on the primary ingredient of every pair (D[i]).
    • Sorting in descending order ensures that larger down values are thought-about first.
  • Most important logic:
    • Iterate from 0 to M-1.
    • Get the down worth and the decrease worth from the present pair in p.
    • Discover the primary ingredient in ms that isn’t lower than the decrease worth utilizing the lower_bound perform.
    • If such a component exists, take away it from ms and subtract the down worth from the variable ans.
    • If no ingredient is discovered, proceed to the following iteration.
  • Output:
    • After the loop ends, print the worth of ans.

Under is the implementation for the above method:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

typedef lengthy lengthy ll;

 

int major()

{

 

    

 

    

    

    ll N = 3, M = 3;

 

    

    vector<ll> P = { 4, 3, 1 };

 

    

    vector<ll> L = { 4, 4, 2 };

 

    

    vector<ll> D = { 2, 3, 1 };

 

    

    ll ans = 0;

 

    

    

    multiset<ll> ms;

 

    

    

    

    for (ll i = 0; i < N; i++) {

        ms.insert(P[i]);

        ans += P[i];

    }

 

    

    

    vector<pair<ll, ll> > p(M);

 

    

    

    for (ll i = 0; i < M; i++) {

 

        

        

        p[i].first = D[i];

 

        

        

        p[i].second = L[i];

    }

 

    

    

    

    kind(p.rbegin(), p.rend());

 

    

    

    for (ll i = 0; i < M; i++) {

 

        

        

        ll down = p[i].first;

 

        

        

        ll decrease = p[i].second;

 

        

        

        

        auto itr = ms.lower_bound(decrease);

 

        

        

        if (itr == ms.finish()) {

            proceed;

        }

 

        

        ms.erase(itr);

 

        

        

        ans -= down;

    }

 

    

    cout << ans << endl;

}

Time Complexity: O(NlogN + MlogM)
Auxiliary Area: O(N + M)

Final Up to date :
31 Jul, 2023

Like Article

Save Article

[ad_2]

Leave a comment