leetcode638大礼包

在 LeetCode 商店中, 有 n 件在售的物品。每件物品都有对应的价格。然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品。

给你一个整数数组 price 表示物品价格,其中 price[i] 是第 i 件物品的价格。另有一个整数数组 needs 表示购物清单,其中 needs[i] 是需要购买第 i 件物品的数量。

还有一个数组 special 表示大礼包,special[i] 的长度为 n + 1 ,其中 special[i][j] 表示第 i 个大礼包中内含第 j 件物品的数量,且 special[i][n] (也就是数组中的最后一个整数)为第 i 个大礼包的价格。

返回 确切 满足购物清单所需花费的最低价格,你可以充分利用大礼包的优惠活动。你不能购买超出购物清单指定数量的物品,即使那样会降低整体价格。任意大礼包可无限次购买。

#include <bits/stdc++.h>
using namespace std;
#define N 200005
#define ll long long int
#define inf (1 << 31) - 1
#define mod 1e9 + 7
#define lowbit(x) x&(-x)
class Solution {
public:
	map<vector<int>, int> m;
	int dfs(vector<int>& price, vector<vector<int>>& special, vector<int>& needs, int n){
		if(!m.count(needs)){
			int minprice = 0;
			for(int i = 0; i < n; i++)
				minprice += price[i] * needs[i];
			for(int i = 0; i < special.size(); i++){
				vector<int> curneeds;
				for(int j = 0; j < n; j++){
					if(special[i][j] > needs[j]) break;
					curneeds.push_back(needs[j] - special[i][j]);
				}
				if(curneeds.size() == n)
					minprice = min(minprice, dfs(price, special, curneeds, n) + special[i][n]);
			}
			m[needs] = minprice;
		}
		return m[needs];
	}
    int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
    	vector<vector<int> > v;
        int n = price.size();
    	for(int i = 0; i < special.size(); i++){
    		vector<int> temp;
    		int ans = 0, pos = 0;
    		for(int j = 0; j < n; j++){
    			ans += special[i][j] * price[j];
    			pos += special[i][j];
    			temp.push_back(special[i][j]);
    		}
    		temp.push_back(special[i][n]);
    		if(pos && ans > special[i][n]) v.push_back(temp);
    	}
    	return dfs(price, v, needs, n);
    }
};
int main(){
	vector<int> v1 = {2, 5};
	vector<vector<int> > v2 = {{3, 0, 5}, {1, 2, 10}};
	vector<int> v3 = {3, 2};
	Solution solution;
	cout << solution.shoppingOffers(v1, v2, v3) << endl;
	return 0;
}
原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/15451935.html