题目背景
在商店中,每一种商品都有一个价格(用整数表示)。例如,一朵花的价格是 2 ,而一个花瓶的价格是 5 。为了吸引更多的顾客,商店举行了促销活动。
题目描述
促销活动把一个或多个商品组合起来降价销售,例如:
三朵花的价格是 5 而不是6 ,2 个花瓶和一朵花的价格是 10 而不是 12 。 请编写一个程序,计算顾客购买一定商品的花费,尽量地利用优惠使花费最少。尽管有时候添加其他商品可以获得更少的花费,但是你不能这么做。
对于上面的商品信息,购买三朵花和两个花瓶的最少花费的方案是:以优惠价购买两个花瓶和一朵花(10),以原价购买两朵花(4)。
输入格式
输入文件包括一些商店提供的优惠信息,接着是购物清单。(最多有 5 种商品)
第一行 优惠方案的种类数s (0 <= s <= 99)
第 2行 ~ 第 s+1 行 每一行都用几个整数来表示一种优惠方式。第一个整数 n (0 <= n <= 5),表示这种优惠方式由 n 种商品组成。后面 n 对整数 c 和 k 表示 k (1 <= k <= 5)个编号为 c (1 <= c <= 999)的商品共同构成这种优惠,最后的整数 p 表示这种优惠的优惠价(1 <= p <=9,999)。优惠价总是比原价低。
第 s + 2 行 这一行有一个整数 b (0 <= b <= 5),表示需要购买 b 种不同的商品。
第 s+3 行 ~ 第 s+b+2 行 这 b 行中的每一行包括三个整数:c,k,p 。 c 表示唯一的商品编号(1 <= c <= 999),k 表示需要购买的 c 商品的数量(1 <= k <= 5)。p 表示 c 商品的原价(1 <= p <= 999)。最多购买 5 × 5 = 25 个商品。
输出格式
只有一行,输出一个整数:购买这些物品的最低价格。
样例 #1
样例输入 #1
2 1 7 3 5 2 7 1 8 2 10 2 7 3 2 8 2 5
样例输出 #1
14
对于题目进行分析我们可以知道:最多购买5种商品,每种商品最多购买5件,那么我们就可以使用一个3 * 5 = 15 个比特位来存储当前的状态。也就是一个short类型即可存储所有的状态。我们先拼接出我们要购买的商品 total,然后拼接出各种优惠方案和对应的优惠价格。通过计算原价和优惠价格我们即可得到最优结果。
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <climits>
#include <functional>
using namespace std;
class Solution
{
public:
int getTheLeastCostOfShopping(vector<vector<int>>& needs, vector<vector<int>>& offers)
{
short total = 0;
unordered_map<short, int> preferential; //存储优惠
vector<int> prices(needs.size()); //存储每件商品的单价
//对数据进行分析处理
analysis(needs, offers, total, prices, preferential);
//计算总价
int total_price = getTotalPrice(prices, total);
//用于记忆化搜索,防止选择重复的方案。
unordered_map<short, int>cnt;
//lambda表达式,用于计算各种组合的方案优惠的最大值
function<int(int )> dp = [&](short cur)->int {
int res = 0;
for (auto& [k, v] : preferential)
{
if (contain(cur, k))
{
if (!cnt.count(cur - k))
{
cnt[cur - k] = dp(cur - k);
}
res = max(res, cnt[cur - k] + v);
}
}
return res;
};
return total_price - dp(total);
};
private:
void analysis(vector<vector<int>>& needs, vector<vector<int>>& offers, short& total, vector<int>& prices, unordered_map<short, int>& preferential)
{
unordered_map<int, int> mp;
int i = 0;
for (auto& need : needs)
{
mp[need[0]] = i;
total |= need[1] << (i * 3);
prices[i] = need[2];
i++;
}
for (auto& offer : offers)
{
int solution = 0;
for (int i = 0, j = 1; i < offer[0]; i++, j += 2)
{
solution |= offer[j + 1] << (mp[offer[j]] * 3);
}
//存储优惠价格
preferential[solution] = getTotalPrice(prices, solution) - offer.back();
}
}
int getTotalPrice(vector<int>& prices, short total) {
int ans = 0;
for (int i = 0; i < prices.size(); i++) {
ans += (total >> (i * 3) & 07) * prices[i];
}
return ans;
}
//判断a是否可以分解出b
bool contain(short a, short b) {
while (a || b) {
short current = a & 07;
short val = b & 07;
if (current < val) {
return false;
}
a >>= 3;
b >>= 3;
}
return true;
}
};
int main() {
//获取数据
int n;
cin >> n;
vector<vector<int>> offers(n);
for (auto& v : offers) {
cin >> n;
v.resize((n + 1) << 1);
for (int i = 1; i < v.size(); i++) {
cin >> v[i];
}
v[0] = n;
}
cin >> n;
vector<vector<int>> needs(n, vector<int>(3));
for (auto& v : needs) {
for (auto& e : v) {
cin >> e;
}
}
//打印结果
cout << Solution().getTheLeastCostOfShopping(needs, offers) << endl;
return 0;
}