Trends

Purchase in minimal cost

Trending 1 year ago
beritaja.com

A personification has a database of N items to buy. The costs of the ith point is represented by Pi. He besides has M coupons. Each coupon Can beryllium utilized to acquisition an point from The database whose costs is astatine least Li, pinch a discount of Di. Each coupon Can only beryllium utilized once, and aggregate coupons cannot beryllium utilized for The aforesaid item. Find The minimum full costs required to acquisition each N items from The list, considering The disposable coupons.

Examples:

Input: N = 3, M = 3, P[3] = {4, 3, 1}, L[3] = {4, 4, 2}, D[3] = {2, 3, 1}
Output: 4
Explanation: Consider utilizing the 2nd coupon for the 1st item, and the 3rd coupon for the 2nd item. Then, he buys the 1st point for 4-3 = 1, the 2nd point for 3-1 = 2, and the 3rd point for 1. Thus, he Can bargain each The items for 1 + 2 + 1 = 4

Input: 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

Approach: To lick The problem travel The beneath observations:

This problem Can beryllium solved utilizing greedy approach. Here The greedy method arsenic follows.

  • Coupons are looked astatine in descending bid of discount price. If location are products for which coupons Can beryllium used, The coupon is utilized for The cheapest merchandise among them.

Hereafter, The solution obtained by applying this method is called the optimal solution .

There are 2 imaginable cases of non-optimal solutions:

  • He didn’t usage a coupon that should person been available.
  • He utilized a coupon that should person been available, but he utilized The coupon connected a non-cheapest item.

For these 2 cases, It is shown that it does not wounded to switch The optimal solution. In The following, The coupon pinch The largest discount value does not obey The greedy method, and each different coupons obey The greedy method. First, see The erstwhile case. Then, in The optimal solution c0 The merchandise that was expected to usage m, aliases for higher priced items, The coupons really utilized are listed in descending bid of The value of The utilized item. c1​,c2​,…,ck​will do.

At this point, The pursuing Can beryllium said.

  • mTo c1​ If is not used: mTo c1 ​By utilizing , it is imaginable to summation The number of coupons that Can beryllium utilized while maintaining The usage position of different coupons.
  • m to c1 ​is used: m to c1 ​not, c0 ​use . By doing this, c1 ​teeth m You Can usage it for The adjacent cheapest point instead. However, in this case,ck ​can nary longer beryllium applied to immoderate product. but, c0 ​The discounted value of ck​Since it is larger than The discounted value of , location is nary wide loss. Next, see The second case. Then, in The optimal solution c0. ​The merchandise that was expected to usage m0​, The merchandise really utilized m1 ​will do. At this point, The pursuing Can beryllium said.
  • m0​ If nary coupon has been utilized for: c0 ​is used, m1 ​not m0 ​should beryllium changed to As a result, The usage position of The coupon does not change, and The options for utilizing different coupons are not narrowed.

From The above, it was shown that in some The erstwhile lawsuit and The second case, location is nary nonaccomplishment by replacing a non-optimal solution pinch an optimal solution.

  • m0 ​If The coupon is utilized on: m0 ​and m1 ​It is capable to speech The destination of The coupon for m1 ​the value of m0 ​Since The attack is besides high, m0 ​The coupon utilized for m1 ​can besides beryllium utilized for Therefore, this speech is ever imaginable and location is nary nonaccomplishment by this exchange.

Below are The steps progressive in The implementation of The code:

  • Initialization:
    • Initialize a adaptable ans to support way of The answer.
    • Create a multiset named ms to shop The elements of array P.
  • Inserting elements and calculating The first sum:
    • Iterate from 0 to N-1 and insert each constituent of array P into The multiset ms.
    • Also, adhd each constituent of array P to The adaptable ans.
  • Creating a vector of pairs:
    • Create a vector of pairs named p pinch size M.
    • For each index, one from 0 to M-1, group The first constituent of p[i] arsenic D[i] and The 2nd constituent arsenic L[I].
    • This creates pairs of elements wherever The first constituent represents The down worth and The 2nd constituent represents The little value.
  • Sorting The vector of pairs:
    • Sort The vector of pairs p in descending order. The sorting is based connected The first constituent of each brace (D[i]).
    • Sorting in descending bid ensures that higher down values are considered first.
  • Main logic:
    • Iterate from 0 to M-1.
    • Get The down worth and The little worth from The existent brace in p.
    • Find The first constituent in ms that is not little than The little worth utilizing The lower_bound function.
    • If specified an constituent exists, region it from ms and subtract The down worth from The adaptable ans.
    • If nary constituent is found, proceed to The adjacent iteration.
  • Output:
    • After The loop ends, people The worth of ans.

Below is The implementation for The supra approach:

C++

#include <bits/stdc++.h>

using namespace std;

typedef agelong long ll;

int main()

{

    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 one = 0; one < N; i++) {

        ms.insert(P[i]);

        ans += P[i];

    }

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

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

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

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

    }

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

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

        ll down = p[i].first;

        ll little = p[i].second;

        auto itr = ms.lower_bound(lower);

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

            continue;

        }

        ms.erase(itr);

        ans -= down;

    }

    cout << ans << endl;

}

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


Last Updated : 31 Jul, 2023

Like Article

Save Article

Editor: Naga



Read other contents from Beritaja.com at
More Source
close